Skip to content Skip to sidebar Skip to footer

In Javascript How To Send Data To Php File Using Post Method

In javascript i want to know how to send data to php file using post method. I have tried diffewrent method. But i didn't get any output so please help me. My code is shown below.

Solution 1:

Do something like this and pass your values like this and you can check in your profile.php page....Here you cannot check if(isset($_POST['submit'])), but you you can check if(isset($_POST['name']))

<formmethod="post"name="form"enctype="multipart/form-data"><inputtype="text"name="name"id="name"placeholder="Name"required/><inputtype="email"name="email"id="email"placeholder="Email"required/><inputtype="password"name="pass"id="pass"placeholder="Password"required/><inputtype="submit"name="submit"value="Send"onclick="myFunction()"/></form><script>functionmyFunction() {
    var name = document.getElementById("name").value;
    var email = document.getElementById("email").value;
    var password = document.getElementById("password").value;
    $.ajax({
            type : "POST",  //type of method
            url  : "profile.php",  //your page
            data : { name : name, email : email, password : password },// passing the valuessuccess: function(res){  
                                    //do what you want here...
                    }
        });
    }
    </script>

Solution 2:

Do some thing like this. Send ajax request on profile.php file.

ON profile.php file print_r($_REQUEST) you will get all your form indexes.

$(document).ready(function() {
  $("form").on("submit", function(event) {
    $.ajax({
      type: 'POST',
      url: 'profile.php',
      data: $( this ).serialize(),
      success: function(data) {
        //success code
      }
    });
  });
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formmethod="post"name="form"enctype="multipart/form-data"onSubmit="return false;"><inputtype="text"name="name"id="name"placeholder="Name"required/><inputtype="email"name="email"id="email"placeholder="Email"required/><inputtype="password"name="pass"id="pass"placeholder="Password"required/><inputtype="submit"name="submit"value="Send" /></form>

profile.php

print_r($_REQUEST);

Solution 3:

First I need to say that, you can post your data by ajax (without page reloading) or by direct post (reload your page).As your question doesn't tag jquery, so i'm leaving ajax. Here is the example for post a form data, I mean reload the current page and post the form data in profile.php Hence your enctype will be enctype="multipart/mixed" instead of enctype="multipart/form-data" because you didn't want to send any file input in your form.

<formmethod="post"action="profile.php"name="form"id="your_form_id"enctype="multipart/mixed"><inputtype="text"name="name"id="name"placeholder="Name"required/><inputtype="email"name="email"id="email"placeholder="Email"required/><inputtype="password"name="pass"id="pass"placeholder="Password"required/><inputtype="button"name="btnsubmit"value="Send"onclick="myFunction()"/></form><script>functionmyFunction() {
//you didn't need to get the data by `document.getElementById()`. just submit your form//var name = document.getElementById("name").value;//var email = document.getElementById("email").value;//var password = document.getElementById("password").value;document.getElementById('your_form_id').submit();

}

</script>

Post a Comment for "In Javascript How To Send Data To Php File Using Post Method"