-2

Helo friends, my code:

<form>
  <input type="text" placeholder="name">
  <input type="text" placeholder="email">
  <input type="password" placeholder="pw">
  <button type="button" id="insert">Insert</button>
</form>

My JavaScript file:

$('#CadAdmin').click(function(){
    $('input').each(function(){ 
        $.post('/require/jp/insert.php',{
            dataUser:$(this).val()
        },function(res){
            alert(res);
        })
    })
});

My insert.php file:

extract($_POST); 
    print dataUser;

Here, its show all data, ok but, I want to get separete data, for ex:

//print dataUser
print $name.' '.$email.' '.$pw;
Alexandre Neukirchen
  • 2,713
  • 7
  • 26
  • 36
Rafael Moura
  • 1,247
  • 2
  • 14
  • 26

3 Answers3

1

You need to define the name (not the placeholder) of each input, then serialize the form data before posting it to your insert.php script.

JustBaron
  • 2,319
  • 7
  • 25
  • 37
1

The $_POST variable is an array (see $_POST on php.net). This means you can access the inner content like this $_POST['index'].

For you this would mean that you have to use the form input names with $_POST. This gives you $_POST['name'], $_POST['email'] and $_POST['pw']. These variables contain the input from the form.

Note that just printing/echoing these variables to your website you might have some problems with XSS, you can check this answer or this one (both from other questions) for on how to prevent something like that from happening.


EDIT (after comment):

I suggest you to change (or even remove) your javascript code. Because you are using $('input').each() you are currently sending 3 separate POST requests to /require/jp/insert.php, this means the data is handled separately. If you were to change that, your inputs do not have a name, that is why extract() doesn't work, the data within $_POSTisn't set to a specific 'variable' to use after extract().

I suggest you change your code to something like this:

(somehtmlpage.html?):

<form method="POST" action="/require/jp/insert.php">
  <input type="text" name="name" placeholder="name">
  <input type="text" name="email" placeholder="email">
  <input type="password" name="pw" placeholder="pw">
  <input type="submit" id="insert">Insert</input>
</form>

(insert.php):

extract($_POST); 
print $name.' '.$email.' '.$pw;

What this does is exact the same, without javascript (and the 3 POSTs). Since your data is now within the 'same' $_POST you can use the names (from the input fields, 'name=....') after extraction.

Community
  • 1
  • 1
Tom Udding
  • 2,264
  • 3
  • 20
  • 30
  • thks for answer, I'm trying to make one CRUD on js but I dont know to do it, with the extract($_POST) I would dont have to do just that, create many variables – Rafael Moura Dec 21 '16 at 16:00
0

First, add name attribute to your elements to catch their values after form submit:

<input type="text" placeholder="name" name="name">

then you can get those values like this:

$name = $_POST['name'];
barbq
  • 11
  • 2
  • thks for your help with or without name's attributes in my file js I have ........dataUser:$(this).val()......so the insert.php is waiting this variable 'dataUser', using extract($_POST) I dont need to declare many variables ex $name = $_POST['name'], etc....my file insert.php is received one array of js – Rafael Moura Dec 21 '16 at 16:06