如何在 PHP 中获取/获取 HTML5 Range Slider 的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11788005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:05:44  来源:igfitidea点击:

How to get/fetch HTML5 Range Slider's value in PHP?

phpformshtml

提问by isbjorn

How do I get the value of my range slider that sits in a form?

如何获取位于表单中的范围滑块的值?

I cant get a value from it by just asking from the name.

仅从名称中询问我无法从中获得价值。

I use this JavaScript function to type out the value in the form beside the slider

我使用这个 JavaScript 函数在滑块旁边的表单中输入值

    function showValue(newValue)    {
    document.getElementById("range").innerHTML=newValue;    }

Could I call from the div or something like that?

我可以从 div 或类似的地方打电话吗?

Edit!All right here we go, I'm sorry if I'm confusing you all, but English is not my native language. I will try and explain.

编辑!好的,我们开始吧,如果我让你们感到困惑,我很抱歉,但英语不是我的母语。我会试着解释。

I have a contact form in HTML5, that submits the data to mail.php.

我有一个 HTML5 格式的联系表单,可将​​数据提交到mail.php.

<input type="text" name="name"/>
<input type="email" name="email"/>

In mail.phpI fetch the value from the input boxes by

mail.php我从输入框中获取值

$name = $_POST['name'];
$email = $_POST['email'];

And then processing further to send the actual mail.

然后进一步处理以发送实际邮件。

So far, so good?

到现在为止还挺好?

I have a Range Slider that determines how many passengers user want to book.

我有一个范围滑块,用于确定用户想要预订的乘客数量。

<input type="range" min="1" max="12" step="1" value="1" name="passengers" onchange="showValue(this.value)" />
<span id="range">1</span></p></div>

I use JavaScript function because I want to show the current selected value of the slider to the user as he/she progresses through the form.

我使用 JavaScript 函数是因为我想在用户浏览表单时向他/她显示滑块的当前选定值。

But I myself of course want to get this value in mail.phpto the send mail as well. I can`t request value from the range name. So I was wondering if maybe I could access the value from the div id range or something?

但我自己当然也想将此值放入mail.php发送邮件中。我无法从范围名称中请求值。所以我想知道我是否可以从 div id 范围或其他东西访问值?

回答by DavChana

So, You want a slider, which displays its current value alongside itself and when form is submitted, your action file (mail.php) gets/retrieves Slider's selected value to process further. Right??

因此,您需要一个滑块,它会在其自身旁边显示其当前值,并且在提交表单时,您的操作文件 (mail.php) 会获取/检索 Slider 的选定值以进行进一步处理。对??

I believe that because:

我相信是因为:

You titled your question Send HTML 5 range slider value to mail via php, I interpret it to How to get the value of this range-slider in mail.php.

你为你的问题命名Send HTML 5 range slider value to mail via php,我将其解释为 How to get the value of this range-slider in mail.php.

You said that you get the Input text boxes' value by doing a $_POST["foo"] on them.

你说你通过对它们执行 $_POST["foo"] 来获得输入文本框的值。

So, in the same way, Range Slider's value is also available in $_POSTarray. I assume that you are using action=POST, because you mentioned $name = $_POST['name']

因此,同理,Range Slider 的值也可以在$_POST数组中使用。我假设您正在使用action=POST,因为您提到$name = $_POST['name']

So, to get the value of this particular Range Slider, use:

因此,要获取此特定范围滑块的值,请使用:

$range_slider_value = $_POST["passengers"];

Of course, you should look into sanitizing form input and all other types of foreign input.

当然,您应该考虑清理表单输入和所有其他类型的外部输入。

A fully functional snippet, which displays the value on User's computer, as he/she changes the slider and on Form-submit, gives it to your mail.php is:

一个功能齐全的代码段,它在用户的计算机上显示值,当他/她更改滑块和表单提交时,将其提供给您的 mail.php 是:

<form action="" method="POST">
    <input type="range" min="1" max="12" step="1" value="1" id="foo" name="passengers" onchange='document.getElementById("bar").value = "Slider Value = " + document.getElementById("foo").value;'/>
<input type="text" name="bar" id="bar" value="Slider Value = 1" disabled />
<br />
<input type=submit value=Submit />
</form>

<?php
if(isset($_POST["passengers"])){
    echo "Number of selected passengers are:".$_POST["passengers"];
    // Your Slider value is here, do what you want with it. Mail/Print anything..
} else{
Echo "Please slide the Slider Bar and Press Submit.";
}
?>

Live Demo at Codepad.viper-7.com

Codepad.viper-7.com 上的现场演示

To quote http://www.html5tutorial.info/html5-range.php,
Value is a common attribute of "Input" element. The value can be any valid floating point number, not necessary must be an integer number. Default value is minimum value plus half of the maximum value.

引用http://www.html5tutorial.info/html5-range.php
Value is a common attribute of "Input" element. The value can be any valid floating point number, not necessary must be an integer number. Default value is minimum value plus half of the maximum value.

Do not confuse Javascript functionality with PHP functionality. PHP Works Server-side and can't alter anything once a page is generated. Javascript works on Client-side, and can alter the page even after it is rendered.

不要将 Javascript 功能与 PHP 功能混淆。PHP Works 服务器端,一旦生成页面就不能更改任何内容。Javascript 在客户端工作,即使在页面呈现后也可以更改页面。

You said if maybe I could access the value from the div id range or something..
In jQuery, it is possible to fetch the value of a Divand can submit along with the Form data, but you don't need to go that route. A simple $_POST["passengers"];will do.

你说if maybe I could access the value from the div id range or something..
在 jQuery 中,可以获取 a 的值Div并可以与表单数据一起提交,但你不需要走那条路。一个简单的$_POST["passengers"];就行了。

回答by mariusnn

document.getElementById("range").valueshould get the current value of the slider.

document.getElementById("range").value应该获取滑块的当前值。

To make changes to the value this will then be the javascript.code;

要对值进行更改,这将是 javascript.code;

function showValue(newValue) {
    document.getElementById("range").value = newValue; }

If you render the slider by PHP you could simply insert the value directly:

如果您通过 PHP 渲染滑块,您可以直接插入值:

<input id="range" type="range" value="<?$theValue?>"/>