1

I want to Post Multiple values for foreach function because i have multiple dynamic textbox then how should i send the values in database?? how to write foreach function for dat.. the code below display 2 multiple textbox but didnt submit values in databse

<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>
3
  • did you try to print $values inside your foreach loop? Commented Mar 28, 2014 at 7:42
  • no i want to add values in db as single row Commented Mar 28, 2014 at 7:45
  • You can refer to this: stackoverflow.com/questions/20607405/… or check this fiddle: jsfiddle.net/ehx6M/1 Commented Mar 28, 2014 at 7:46

2 Answers 2

2

try this

       <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>
Sign up to request clarification or add additional context in comments.

6 Comments

remove comment from print_r statement check its printing or not values
no bt how to send both values $_POST['dynfields'] and $_POST['holireason'] in databse in foreach function
by print_r it only gives one textbox value
whats your actual requirement you want to insert both data in hobbies filed
ya i want insert data bt in 2 columns in one row of two textbox values
|
1

Use array_combine as follows.

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% works fine.

4 Comments

Explain what you're doing and why it fits user question
For every loop count, $dyn and $holi take $_POST['dynfields'] value and $_POST['holireason'] value respectively till the loop counter says it is enough.
He says .. "no i want to add values in db as single row"
That block of code inserts values in db as a single row depending on the size of your array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.