簡體   English   中英

動態創建的文本框不向php表單提交值

[英]Dynamically created text boxes not submitting values to php form

我有一種形式,其中我通過Jquery(動態地)添加了一些元素,但是問題是,除了那些變量之外,其他所有東西都被發布到了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 />

注意:當我設置手動值時,查詢工作正常

有同樣的問題。 通過將php forloop輸出存儲到串聯字符串中來解決該問題,並在需要的地方執行了“ echo”操作。

<?php
$omenu="";
$h= $_SESSION['days']+1;
for($i=1; $i<$h;$i++){
    $omenu .="<option>"."Day$i"."</option>";
}
?>

在需要的地方回顯$ omenu,它可以工作。 問題是我猜想頁面渲染元素的速度。

您可以創建數組輸入(例如<input name='stopover[]' ...

然后,在PHP中,您可以將for替換for foreach

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM