0

I need some advice in the following code.

var count = 1;
$(function(){
    $('#addmore').click(function(){
        count += 1;
        $('ul').append("<li><input type='text' id='hd"+count+"' name='hd"+count+"' value='heading' onClick='this.select();'> <input type='text' id='amount"+count+"' name='amount"+count+"' value='amount' onClick='this.select();'></li>");
//      return false;
    });
});

what exact php code should i use to submit all the input into my test table of mysql.

<button id="addmore">+</button>
<form id="myForm" name="myForm" action="" method="post" onSubmit="return false;">
<ul>
    <li><input type="text" id="hd1" name="hd1" value="heading" onClick="this.select();">
    <input type="text" id="amount1" name="amount1" value="amount" onClick="this.select();"></li>
</ul>
<input type="submit" value="Submit" id="submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])){

echo "h";
};
?>

keeping in mind that i've test table with two fields 'heading' and 'amount'.


if(isset($_POST['submit'])){
    foreach($_POST['hd'] as $hds){
        echo $hds;
        mysqli_query($con,"INSERT INTO test (hdg) VALUES ($hds)");
    };
};

and its not adding data into my table

8
  • This is a bit ambiguous - the PHP script that puts data into the table doesn't need to care about your JS function, it can just check to see what data it is given. But to give "exact php code" you would need to tell us something about your MySQL table's structure. Also remember that you can see if a form value is set with something like isset($_POST['hd1']). Commented May 28, 2013 at 3:18
  • What have you tried? Can you post your php code that you have so far? What are you using for your database connection - mysqli or PDO? Commented May 28, 2013 at 3:19
  • my mysql table structure is simple Id(int), hdg(varchar), amt(varchar) and table name is test. Commented May 28, 2013 at 3:22
  • JSON.stringify the values and send them to php. There run a loop for each set of value and insert into the db Commented May 28, 2013 at 3:23
  • // Create connection $con=mysqli_connect("localhost","root","","transport"); // Check connection if (mysqli_connect_errno($con)){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } Commented May 28, 2013 at 3:23

1 Answer 1

2

Use [] for the name attributes instead of the numbers:

<li><input type="text" id="hd1" name="hd[]" value="heading" onClick="this.select();">

On the server-side you will have an array of values.

$hd = $_POST['hd']; // Returns an array
Sign up to request clarification or add additional context in comments.

1 Comment

if(isset($_POST['submit'])){ $hd = $_POST['hd']; echo $hd; };

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.