我有一个表单,其中有一些元素是我通过Jquery(动态)添加的,但问题是除了这些变量之外,所有的东西都被POSTED到PHP操作文件中。
我想在php文件中获得这些文本框的值。
代码:
Jquery代码:
var temp = $('#waypoints').val();
var i=1;
var inputButtons = "<b>Stopover:</b><input type='text' class='stopovers' name='stopover"+i+"'><br>";
var length = '<input type="hidden" value="'+temp+'" name="length" />';
inputButtons+= length;
if(temp==0){
$('div#stopovers').html("");
}
else if(temp==1){ //if a single way point
$('div#stopovers').html(inputButtons);
}
else{
while(i<temp){
i++;
inputButtons += "<b>Stopover:</b><input type='text' class='stopovers' name='stopover"+i+"'><br>";
$('div#stopovers').html(inputButtons);
}
}
PHP代码:
$start = $_POST['start'];
$end = $_POST['end'];
$length= $_POST['length'];
echo $length;
echo "<br><br>".$start;
echo "<br><br>".$end;
//inserting into the 'locations' table
$query = mysql_query("INSERT INTO `locations` (`pid`, `location`, `type`) VALUES ('$package_id', '$start', 'start');");
$query = mysql_query("INSERT INTO `locations` (`pid`, `location`, `type`) VALUES ('$package_id', '$end', 'destination');");
$j=0;
for($i=1; $i<=$length; $i++){
$stopover='stopover'.$i;
$stopovers[j]=$_POST[$stopover];
//echo 'stopover'.$i;
$query = mysql_query("INSERT INTO `blankand_pts`.`locations` (`pid`, `location`, `type`) VALUES ('$package_id', '$stopovers[j]', 'stopover') ");
echo "<br><br>blah: ".$stopovers[j];
$j++;
}
HTML/表单代码:
<b>Starting city:</b>
<input type="text" id="start" name="start"><div class="undertext">Format: New Delhi, India</div>
<b>Number of stop overs:</b>
<select id="waypoints">
<option selected="selected">Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select><br>
<div id="stopovers">
</div><br>
<b>Destination:</b>
<input type="text" id="end" name="end"><div class="undertext">Format: New Delhi, India</div>
<br />
注意:当我设置手动值时,查询工作得非常好
您可以创建数组输入(例如<input name='stopover[]' ...
然后,在PHP中,可以用foreach
替换for
foreach($_POST['stopover'] as $stopover){
$query = mysql_query("INSERT INTO `blankand_pts`.`locations` (`pid`, `location`, `type`) VALUES ('$package_id', '$stopover', 'stopover') ");
echo "<br><br>blah: ".$stopover;
}
具有相同的issue。通过将php forloop输出存储到一个连接字符串来解决此问题,并在需要的地方执行"echo"。
<?php
$omenu="";
$h= $_SESSION['days']+1;
for($i=1; $i<$h;$i++){
$omenu .="<option>"."Day$i"."</option>";
}
?>
在任何需要的地方echo$omenu,它都能工作。问题出在页面呈现元素的速度上。