0

I have this form on my project and I want to enable the user to input as many data as they want and then save this data to mysql table. I dont know how to process this on my php code. Can someone help me ?

<form name="frm9" action="<?php echo $_SERVER['PHP_SELF'];?>" onsubmit="return checknull3();"  method="post" >
  Operator Name            <input type="text" name="msoname">   <br><br>
  Number of households    <input type="number" name="msohhs">   <br><br>
  <input type="submit"  value="Add more"> 
</form>

I was thinking to stay on the same page and pass the data on the table when the users clicks on Add more button. Then clear the form and add some more data?

How can I implement this?

PS: My checknull function just checks for null input (all data must be filled).

2
  • When the user decides to add another field, what kind of data will the field contain? And in what way will that arbitrary field be stored in the MySQL table? Commented Jun 14, 2013 at 15:38
  • first input is a text and second a number. i have to process it somehow in php to add every input(text and number) to new line in the table Commented Jun 14, 2013 at 15:46

2 Answers 2

3

The usual way of doing this is :

  1. Have a javascript function add fields dynamically on click on the "add more" button
  2. Register your input names with brackets (i.e. name="msoname[]")
  3. Submit your form as you'd submit a normal form
  4. Iterate over $_POST['msoname'] (which becomes an array) in your PHP to find out all filled fields
Sign up to request clarification or add additional context in comments.

4 Comments

on submitting my form how the array will pass to php?
It will automagically regroup all your inputs named something[] under the same $_POST['something'] array in PHP.
do i have to include jquery?
It makes it easier to do it with jQuery (especially about the cloning part). That's a peek of how i would do it : jsfiddle.net/29L4a . Hope it can help you as a kick start.
0
<form id="frm9" name="frm9" action="<?php echo $_SERVER['PHP_SELF'];?>" onsubmit="return checknull3();"  method="post" >
  Operator Name <input type="text" name="msoname" id="msoname">   <br><br>
  Number of households    <input type="number" name="msohhs" id="msohhs">   <br><br>
  <input type="submit"  value="Add more"> 
</form>

<script type="text/javascript">
       function checknull3(){
           if($("#msoname").val() == '' || $("#msoname").val() == ''){
                 alert("Enter all values");
                 return false;
           }else{
              $("#fm9").ajaxForm();
              $("#msoname").val() = '';
              $("#msoname").val() = '';
        }
</script>   

1 Comment

Thanks twitch! i noticed you are using ajaxForm() ? . Unfortunately i know nothing about ajax/json/jquery :S

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.