php 如何在不提交的情况下从 FORM 中获取 VALUE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15676903/
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
How to get VALUE from FORM without Submitting it?
提问by user2082882
I would like to get value from Form without submitting it, because the client have to choose the right type of house model to get the right form that fits the selected house model, without submitting, just selecting the house model and it for example continues the rest of form after that.
我想在不提交的情况下从 Form 中获取价值,因为客户必须选择正确类型的房屋模型才能获得适合所选房屋模型的正确表格,无需提交,只需选择房屋模型,例如继续之后的其余形式。
I have so far tried with this:
到目前为止,我已经尝试过:
<form method="GET" action="foo.php">
<select name="house_model">
<option value="">------</option>
<option value="<?php echo $model1;?>">Model 1</option>
<option value="<?php echo $model2;?>">Model 2</option>
<option value="<?php echo $model3;?>">Model 3</option>
</select>
</form>
<?php
$a = $_GET["housemodel"];
if($a<>'')
{
if($a == $model1)
{
echo "<input type=\"text\" name=\"a\" value=\"something model1\">";
}
else if($a == $model2)
{
echo "<input type=\"text\" name=\"b\" value=\"something model2\">";
}
else if($a == $model3)
{
echo "<input type=\"text\" name=\"c\" value=\"something model3\">";
}
}
?>
回答by UserProg
If you don't want to submit the form, use JavaScript to get the element. Or you can also use jquery to access the value. Make sure you put id = "something"
and retrieve it using $('#something').val();
如果您不想提交表单,请使用 JavaScript 获取元素。或者您也可以使用 jquery 来访问该值。确保你放置id = "something"
和检索它使用$('#something').val();
if you want to use JavaScript,
如果你想使用 JavaScript,
<select name="house_model" id="model">
<option value="">------</option>
<option value="<?php echo $model1;?>">Model 1</option>
<option value="<?php echo $model2;?>">Model 2</option>
<option value="<?php echo $model3;?>">Model 3</option>
</select>
<script type="text/javascript">
var model= document.getElementById('model');
alert("You entered: " + model);
</script>
AJAX codes here
AJAX 代码在这里
$("#model").live("change", function () {
alert("You choose " + $('#model').val());
});
回答by Bhushan
I think, if you dont want page
to be refreshed when user selects value from your drop down list, then you can use ajax
. AJAX
is used for this purpose. if you google, then you will find lots of tutorials related to AJAX
.
我认为,如果您不想page
在用户从下拉列表中选择值时刷新,那么您可以使用ajax
. AJAX
用于此目的。如果你谷歌,那么你会发现很多与AJAX
.
回答by Mahendra
IF you don't want to submit your form, you can get element using AJAX/jQuery.
如果您不想提交表单,可以使用 AJAX/jQuery 获取元素。
<form method="GET" action="foo.php" onChange="getHouseModel">
<select name="house_model" id="house_model">
<option value="">------</option>
<option value="<?php echo $model1;?>">Model 1</option>
<option value="<?php echo $model2;?>">Model 2</option>
<option value="<?php echo $model3;?>">Model 3</option>
</select>
</form>
<script type='text/javascript'>
function getHouseModel(){
var model=$('#house_model').val();
alert(model);
}
</script>
or you can write jquery like this. so you dont have to call function in tag
或者你可以这样写jquery。所以你不必在标签中调用函数
<script type="text/javascript">
$('#house_model').select(function() {
var model=$('#house_model').val();
alert(model);
});
</script>
回答by Jozef Legény
There is no way of getting data from a form before submitting in in PHP! This is because before submitting the form the PHP script simply is not called.
在 PHP 中提交之前,无法从表单中获取数据!这是因为在提交表单之前,根本没有调用 PHP 脚本。
If you want to do some validation before sending data you must use javascript which is run on the client browser. Have a look at the jQuery library which makes this very easy: http://docs.jquery.com/Plugins/Validation/validate
如果您想在发送数据之前进行一些验证,您必须使用在客户端浏览器上运行的 javascript。看看 jQuery 库,这使得这很容易:http: //docs.jquery.com/Plugins/Validation/validate
If you want to use PHP only then you will need to separate the form into two pages.
如果您只想使用 PHP,那么您需要将表单分成两个页面。
回答by Deepu
You have to use ajax for achiving this.For example
你必须使用 ajax 来实现这一点。例如
<form method="GET" action="foo.php">
<select name="house_model" id="house_model">
<option value="">------</option>
<option value="<?php echo $model1;?>">Model 1</option>
<option value="<?php echo $model2;?>">Model 2</option>
<option value="<?php echo $model3;?>">Model 3</option>
</select>
</form>
$("#house_model").live("change", function () {
//Write the ajax and post the value to server side
});