ashique
ashique

Reputation: 155

How to receive form NAME with Ajax post?

In my site, I have different pages with different types of form. I want to submit all the forms to 'process.php' using Ajax and process data at 'process.php'. But, I am unable to catch the form name or form submit name at 'process.php' while posting data through Ajax.

Could anyone please tell me how could I know which form has been submitted from 'process.php'?

I tried the following steps that failed:

CHANGE PASSWORD.PHP

<form id="change_password_form" action="process.php" method="POST">
  <input type="password" id="current_password" name="current_password" />
  <input type="password" id="new_password" name="new_password" />
  <input type="password" id="confirm_password" name="confirm_password" />
  <input type="submit" value="change password" id="submit" name="change_password" />
</form>

AJAX.JS

$("#submit").click(function(e){
    e.preventDefault();
    $.ajax({
        url:"process.php",
        type:"POST",
        data:$("#change_password_form").serialize(),
        success: function(data){
            $("#msg").html(data);
        },
    });

});

PROCESS.PHP

if(isset($_POST['change_password'])){
    echo("<p>".$_POST['current_password']."</p>");  
    echo("<p>".$_POST['new_password']."</p>");  
    echo("<p>".$_POST['confirm_password']."</p>");  
}

The above code does not echo anything because Ajax did not post INPUT >TYPE >SUBMIT . Here is what it posted:

Array
(
    [current_password] => 12345
    [new_password] => 123
    [confirm_password] => 12
)

How would I make Ajax post the input>type>submit ? or else, if I am posting multiple form data to same PHP file for processing, how would I make PHP understand which form has been submitted?

Your reply would be highly appreciated. Did I detail too much? My apology is before you.

Upvotes: 0

Views: 3839

Answers (4)

Anoop Sharma
Anoop Sharma

Reputation: 193

I would use a hidden field with desired name and that will be definitely posted and capture the same on PHP page and process my conditions.

It is easiest/quickest approach, I think.

Upvotes: 1

Gil
Gil

Reputation: 1804

A good and more reliable way do pass extra info in a form is by using the hidden input type. If you add to your form something like

<input type="hidden" name="form_type" value="change_password" />

And change your PHP into

if(isset($_POST['form_type']) && $_POST['form_type'] == 'change_password')

Then you can get your data and any other information you might want to pass along with other hidden fields.

Upvotes: 1

Abhishek Goyal
Abhishek Goyal

Reputation: 877

You can assign a name to each form. For example, in the above form,

<form name="change_password_form" id="change_password_form" action="process.php" method="POST">
  <input type="password" id="current_password" name="current_password" />
  <input type="password" id="new_password" name="new_password" />
  <input type="password" id="confirm_password" name="confirm_password" />
  <input type="submit" value="change password" id="submit" name="change_password" />
</form>

Then in your javascript,

$("#submit").click(function(e){
    e.preventDefault();
    $.ajax({
        url:"process.php",
        type:"POST",
        data:$("#change_password_form").serialize()+'&form_name='+$("#change_password_form").attr("name"),
        success: function(data){
            $("#msg").html(data);
        },
    });

});

And in your PHP file,

if(isset($_POST['form_name'])){
    if($_POST['form_name']=="change_password_form")
    {
        echo("<p>".$_POST['current_password']."</p>");  
        echo("<p>".$_POST['new_password']."</p>");  
        echo("<p>".$_POST['confirm_password']."</p>");  
    }
    //Similarly, add your other form processing code.
}

Note : You should never trust user input. $_POST elements can be easily manipulated. Always escape the input before inserting it into a database, or use PDO.

Upvotes: 1

Medda86
Medda86

Reputation: 1630

I would use something like this:

url:"process.php?form=change_password",

Then in process you know which form was submitted with $_GET['form'];

Upvotes: 0

Related Questions