简体   繁体   中英

Dynamically created text boxes not submitting values to php form

I've a form in which there are a few elements that i have added through Jquery(dynamically) but the problem is that apart from those variable everything is getting POSTED into the PHP action-file.

I want to get the values of these text boxes in the php file.

CODE:
Jquery Code:

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 Code:

 $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/Form code:

    <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 />

NOTE: The queries work perfectly fine, when I set manual values

Had the same isssue. Solved it by storing the php forloop output to an concatenate string and did an "echo" where it was required.

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

echo $omenu wherever required, and it works. the problem is with the speed of page rendering elements i guess.

You could create array inputs (eg <input name='stopover[]' ...

Then, in PHP, you could replace the for with a 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM