我正在创建一个新的移动表单,用作按钮单击时的弹出窗口。
我有这个PHP和Javascript,我通过JQuery将值发布到另一个页面:
<form action = "#" method = "POST" id = "newFrm" name = "newFrm" >
Please select Make:
<Select name = "comboA" id="comboA">
<option value = "1">Samsung</option>
<option value = "2">iPhone</option>
<option value = "3">Sony Ericsson</option>
</Select>
Please specify Model and version:
<input type = "text" required id = "model" name = "model"></input>
<input type = "submit" value = "Enter" id = "enter" name = "enter"> </input>
<script>
$( "#enter" ).click(function() {
var model= document.getElementById('model').value;
var make= document.getElementById('comboA').value;
var dataString = 'make='+make+'&model='+model;
if($.trim(make).length>0 && $.trim(model).length>0)
{
$.ajax({
type: "POST",
url: "newMobile.php",
data: dataString,
cache: false,
beforeSend: function(){ $("#enter").val('Connecting...');},
success: function(data){
if(data == 1)
{
alert("Information stored successfully!");
}
else{}
if (data == 0)
{
alert("Some error occured! Please try again.");
}
else{}
}
});
return false;
});
</script>
</form>
newMobile.PHP
<?php
$model = $_POST['model'];
$make = $_POST['comboA'];
echo $make.$model;
?>
问题是我只获得$model
值而不是$make
。有人可以帮忙吗?
答案 0 :(得分:4)
更改代码如下。
在ajax调用中,您使用&#39; make =&#39; + make +&#39;传递comboA
值。因此,如果不传递数据,您将无法获得comboA
。
var obj = document.getElementById("comboA");
var make= obj.options[obj.selectedIndex].value;
var dataString = 'make='+make+'&model='+model;
PHP
<?php
$model = $_POST['model'];
$make = $_POST['make']; // use make not comboA
echo $make.$model;
?>
选项2
或更改您的数据字符串,如下所示。将make
替换为comboA
var dataString = 'comboA='+make+'&model='+model;
PHP
<?php
$model = $_POST['model'];
$make = $_POST['comboA'];
echo $make.$model;
?>
因为你正在使用jquery库,你可以改变你的代码,如下所示。
var model= $('#model').val();
var make= $('#comboA').val();
答案 1 :(得分:1)
像这样使用
$make = $_POST['make'];
答案 2 :(得分:1)
试试此代码
var e = document.getElementById("comboA");
var make = e.options[e.selectedIndex].value;
如果您使用的是jQuery
var make = $('#comboA').val();