我遇到了jQuery AJAX post
和PHP的问题。
我尝试使用.serialize()
发送数据并添加一些其他数据。
但是在PHP中,我收到了.serialize()
个数据,但没有收到其他数据。
在我的代码jquery ajax中,
function load() {
var form_data = $('#frm2').serialize();
form_data.push({name:'year',value:'2016'});
alert(form_data);
$.ajax({
type : "POST",
url : "insert.php",
cache : false,
async : false,
data : form_data,
success : function(html) {
alert(form_data);
}
});
}
和frm2(表单的id)是:
<form name='frm2' action="./BOMinsert.php" id = 'frm2' method='post'>
<table width="30%" id="keytable" style="border: 1px gray solid; table-layout: fixed">
<thead style="background: #EAEAEA">
<th>year</th>
<th>brand</th>
<th>season</th>
<th>Styleno</th>
</thead>
<tbody style="text-align: center">
<?php
for($i =1;$i<5;$i++)
echo "<td>".$_POST['name'.$i]."</td>";
?>
</tbody>
</table>
<BR>
<BR>
<table id="bomtable" style="width: 100%;table-layout: fixed">
<thead style="background: #EAEAEA">
<th>item1</th>
<th>item2</th>
<th>item3</th>
<th>item4</th>
<th>item5</th>
<th>item6</th>
<th>item7</th>
<th>item8</th>
<th>item9</th>
<th>item10</th>
<th>item11</th>
<th>item12</th>
</thead>
<tbody style="text-align: center">
<?php echo "<td>".$_POST['name5']."</td>" ?>
<?php echo "<td>".$_POST['name6']."</td>"?>
<?php echo "<td>".$_POST['name7']."</td>"?>
<td><input type="text" class="ui-widget ui-corner-all" name='item' size="5" maxlength="10"/></td>
<td><input type="text" class="ui-widget ui-corner-all" name='description' size="8" maxlength="30"/></td>
<td><input type="text" class="ui-widget ui-corner-all" name='supplycompany' size="5" maxlength="10"/></td>
<td><input type="text" class="ui-widget ui-corner-all" name='colorway' size="5" maxlength="10"/></td>
<td><input type="text" class="ui-widget ui-corner-all" name='unit' size="5" maxlength="10"/></td>
<td><input type="text" class="ui-widget ui-corner-all" name='size' size="5" maxlength="10"/></td>
<td><input type="text" class="ui-widget ui-corner-all" name='consume' size="5" maxlength="10"/></td>
<td><input type="text" class="ui-widget ui-corner-all" name='unit' size="5" maxlength="10"/></td>
<td><input type="text" class="ui-widget ui-corner-all" name='etc'/></td>
</tbody>
<input type='submit' value='save' class="ui-button ui-widget ui-corner-all" style="background: #3498DB; color: white;float: right" onclick="load();">
</form>
最后是我的PHP代码:
if(isset($_POST['year'])){
echo $_POST['year'];
}else{
var_dump($_POST);
}
PHP结果:
array(8) { ["item"]=> string(0) "" ["description"]=> string(0) "" ["supplycompany"]=> string(0) "" ["colorway"]=> string(0) "" ["unit"]=> string(0) "" ["size"]=> string(0) "" ["consume"]=> string(0) "" ["etc"]=> string(0) "" }
Notice: Undefined index: year in C:\Apache24\htdocs\ptest\BOMinsert.php on line 19
P.S。我还尝试了.serializeArray()
,.push()
,.serialize()+"&year=2016"
。
上面的代码经过测试但无效。
答案 0 :(得分:1)
首先,服务器端的PHP代码位于以下..
<?php
if(isset($_POST['year'])){
#Now the POST['year'] Request is available to be checked;
#Go ahead and try it..
$data=$_POST['year'];
echo $data;
exit;
}
?>
使用网址
的简单蠢事<script>
function load() {
var form_data = $('#frm2').serialize();
var year='2016';
//As you can see this will work perfectly and is already tested!
//Just append the required values to the url ((in Url form like below)) and it will send everything to the same page
$.ajax({
type : "POST",
url : "demo.php?year="+year,
cache : false,
async : true,//not false for the best user experience...
data : form_data,
success : function(html) {
console.log(html)
}
});
}
//remember to bind an event on an element to call this function to work.
//I hope it helps...
</script>
答案 1 :(得分:0)
为什么不在表单中添加隐藏字段,并使用您需要传递的名称和值?
<input type="hidden" name="year" value="2016" />
更新为使用动态年份值:
<input type="hidden" name="year" value="<?php echo $_POST['name1']; ?>" />