2014-03-28 75 views
1

我想發佈foreach函數的多個值,因爲我有多個動態文本框,那麼我應該如何發送數據庫中的值? 怎麼寫的foreach功能DAT .. 下方顯示2多個文本框代碼但沒有在DATABSE提交值動態添加輸入字段並提交到數據庫與jQuery和PHP

<html> 
<head> 
<title></title> 
<script type="text/javascript" src="jquery-1.9.1.js"></script> 
<script type="text/javascript"> 
var counter = 0; 
$(function(){ 
$('p#add_field').click(function(){ 
counter += 1; 
$('#container').append(
'<strong>Hobby No. ' + counter + '</strong><br />' 
+ '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />' 

+'<strong>HolidayReason ' + counter + '</strong>&nbsp;' 
+ '<input id="holidayreason_' + counter + '" name="holireason[]' + '" type="text" />' 
); 

}); 
}); 
</script> 

<body> 

<?php 
if (isset($_POST['submit_val'])) { 
if (($_POST['dynfields'])&& ($_POST['holireason'])) { 
//$aaa=array($_POST['dynfields']); 
foreach ($_POST['dynfields'] as $key=>$value) 
{ 
$values = mysql_real_escape_string($value); 
//$holireasons = mysql_real_escape_string($holireason); 

$query = mysql_query("INSERT INTO my_hobbies (hobbies) VALUES ('$values')"); 


} 
} 

echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>"; 

mysql_close(); 
} 
?> 
<?php if (!isset($_POST['submit_val'])) { ?> 
<h1>Add your Hobbies</h1> 
<form method="post" action=""> 

<div id="container"> 
<p id="add_field"><a href="#"><span>Click To Add Hobbies</span></a></p> 
</div> 

<input type="submit" name="submit_val" value="Submit" /> 
</form> 
<?php } ?> 

</body> 
</html> 
+0

你嘗試在你的foreach循環中打印'$ values'嗎? – mesutozer

+0

不,我想在db中添加值作爲單行 – user3359065

+0

你可以參考這個:http://stackoverflow.com/questions/20607405/how-to-insert-dynamically-added-text-boxes-into-mysql-database或檢查這個小提琴: http://jsfiddle.net/ehx6M/1/ –

回答

0

試試這個

 <html> 
      <head> 
      <title></title> 
      <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
      <script type="text/javascript"> 
      var counter = 0; 
      $(function(){ 
      $('p#add_field').click(function(){ 
      counter += 1; 
      $('#container').append(
      '<strong>Hobby No. ' + counter + '</strong><br />' 
      + '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />' 

      +'<strong>HolidayReason ' + counter + '</strong>&nbsp;' 
      + '<input id="holidayreason_' + counter + '" name="holireason[]' + '" type="text" />' 
      ); 

      }); 
      }); 
      </script> 

      <body> 

      <?php 
      if (isset($_POST['submit_val'])) { 
      if (($_POST['dynfields'])&& ($_POST['holireason'])) { 
      $no = count($_POST['dynfields']); 
      for ($i=0; $i <$no ; $i++) { 
      echo $_POST['dynfields'][$i]."<br>"; 
      echo $_POST['holireason'][$i]."<br>"; 
     $abc = mysql_real_escape_string($_POST['dynfields'][$i]); 
     $xyz = mysql_real_escape_string($_POST['holireason'][$i]); 
     $sql = "INSERT INTO my_hobbies (hobbies,Holidayreason) VALUES ('$abc','$xyz')"; 
mysql_query($sql); 
     } 

      } 

      echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>"; 

      mysql_close(); 
      } 
      ?> 
      <?php if (!isset($_POST['submit_val'])) { ?> 
      <h1>Add your Hobbies</h1> 
      <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> 

      <div id="container"> 
      <p id="add_field"><a href="#"><span>Click To Add Hobbies</span></a></p> 
      </div> 

      <input type="submit" name="submit_val" value="Submit" /> 
      </form> 
      <?php } ?> 

      </body> 
      </html> 
+0

沒有添加到數據庫中的值 – user3359065

+0

是否有任何錯誤 –

+0

從print_r語句中刪除評論檢查其打印或不值 –

1

使用array_combine如下。

foreach(array_combine($_POST['dynfields'] , $_POST['holireason']) as $dyn => $holi) { 

    $abc = mysql_real_escape_string($dyn); 
    $xyz = mysql_real_escape_string($holi); 

    $sql = mysql_query ("INSERT INTO my_hobbies (hobbies,Holidayreason) VALUES ('".$abc."','".$xyz."')"); 

} 

100%正常工作。

+1

解釋你在做什麼以及爲什麼它適合用戶問題 – dbmitch

+0

對於每個循環計數,$ dyn和$ holi分別取$ _POST ['dynfields']值和$ _POST ['holireason']的值,直到循環計數器顯示它爲止足夠。 – vnie89

+0

他說..「不,我想在db中將值作爲單行添加」 – dbmitch