1

I currently have a form with a table inside of it. I want to pass these values to a php script. What would be my best way to do so? Everything I've searched has not been applicable.

This is how I have my form formatted:

<form id="pageform" action="phpscript.php" method="post">
  <table>
    <tbody>
      <tr>
        <td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
      </tr>
      <tr>
        <td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
      </tr>
      <tr>
        <td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
      </tr>
    </tbody>
  </table>
  <input type="button" name="btnSubmit" id="btnSubmit" value="Submit">
</form>

The jQuery:

$("#btnSubmit").click(function(){

  var array = [];

  $(".nestedInput").each(function(n){
    array[n] = $(this).val();
  });

  document.forms["pageform"].submit();

});

My PHP:

<?php
  $array=json_decode($_POST['json']);
  print_r($array);    
?>

What I'd like to do is run a mysqli insert using the values from each input in each tr. Any ideas on how I could do that?

Xiaous
  • 47
  • 1
  • 7

3 Answers3

3

In phpscript.php, you can access these variables like this:

$name = $_GET['txtName']
$location = $_GET['txtLocation']
$age = $_GET['txtAge']

Variables submitted via a form will be stored in the global array $_GET or $_POST (depending on whether your form uses GET or POST. Your form doesn't have a method attribute defining this, so it defaults to GET. More on this here.).

Also of note, your submit button's id property should be id="btnSubmit", not id="#btnSubmit".

Hayden Schiff
  • 3,280
  • 19
  • 41
  • Sorry the method is post, in my php page I have array=json_decode($_POST['json']); I'd like to run a for each loop on each item in the array. Is there a way for me to do that without explicitly setting each variable? – Xiaous Aug 07 '15 at 19:01
  • Yeah totally, $_POST is an array, so you can just do something like this: `foreach ($_POST as $key => $value) { echo "$key : $value"; }` – Hayden Schiff Aug 07 '15 at 19:34
  • Now the data is outputting, however it is only throwing the data from the last TR element. Is there a way to get the data from each? I'd like to get something like $_POST[0][1] – Xiaous Aug 07 '15 at 19:57
  • Ah, the HTML in your post has changed since I wrote this answer. PHP doesn't handle it so well if you have multiple form elements with the same name; [here's some info on ways you can deal with that](http://stackoverflow.com/a/2203456/992504). Basically, you'll probably want to add `[]` to the end of each 'name' if you want to have a two-dimensional array like that. – Hayden Schiff Aug 07 '15 at 20:02
  • That has got me closer to where I want to be, however it is capturing the array of inputs. I'm trying to have the contents of the form in an array. I've marked you as the best answer as you've gotten me closer than I was before! Thank you. – Xiaous Aug 07 '15 at 20:48
1

With jQuery ajax method;

function send_form(this) {
   jQuery.ajax({
      type: 'POST',
      url: $(this).attr('action'),
      data: $(this).serialize(),
      error:function(){ console.log('error'); },
      success: function(data) { $console.log(data);}
   });
   return false;
}

Your form;

<form id="pageform" onsubmit="return send_form(this);" action="phpscript.php">
   <table>
      <tbody>
        <tr>
          <td><input type="text" class="nestedInput"name="txtName" value="John"></td>
        </tr>
        <tr>
          <td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td>
       </tr>
       <tr>
         <td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
      </tr>
   </tbody>
</table>

in phpscript.php;

$name = $_POST['txtName'];
$txtLocation= $_POST['txtLocation'];
$txtAge= $_POST['txtAge'];
Batikan
  • 69
  • 1
  • 2
  • Sorry my example wasn't accurate, I've updated it to show what I'm trying to do more accurately. – Xiaous Aug 07 '15 at 19:30
0

No need for jQuery and all that for this..Just use the default functionality of the HTML <form> tag

For this, first of all, change

<input type="button" name="btnSubmit" id="#btnSubmit" value="Submit">

to

<input type="submit" name="btnSubmit" id="#btnSubmit" value="Submit">

Then add a method for your form tag like,

<form id="pageform" action="phpscript.php" method="post">

Now, in your phpscript.php page add var_dump($_POST) in the beginning itself inside the the php tag..

The var_dump() will print an array with all the form data that is passed, after the submit button is clicked.

To retrieve the values in your php page, you can just do it like

echo $_POST['txtName'];

where txtName is the name of your input.

Similarly for the other inputs..

Lal
  • 14,726
  • 4
  • 45
  • 70