2
$("#submit_login").click(function(){
       var username=$('input[name=user_email]');
       var password=$('input[name=user_password]');
       var data;
       data: "name="+username+"&pwd="+password,
         $.ajax({
                type: "POST",
                dataType: "json",
                url: "newExam.php", 
                data: data,
                success: function(data) {
                     alert("Form submitted successfully");
                }
              });     

   }); 

How to give the data variable so that we can fetch it in PHP using $_REQUEST? The above representation of the data variable shows an error.

1
  • 1
    Could you add the error? Commented Jun 10, 2015 at 8:22

5 Answers 5

4

You can pass the data as json,

$.ajax({
    type: "POST",
    dataType: "json",
    url: "newExam.php",
    data:  {
        name: username,
        pwd: password
    },
    success: function(data) {
        alert("Form submitted successfully");
    }
});

Also, names should match the parameters in the function.

Sign up to request clarification or add additional context in comments.

Comments

1

Client Side

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

       var username=$("input[name='user_email']").val();
       var password=$("input[name='user_password']").val();

         $.ajax({
                type: "POST",
                dataType: "json",
                url: "newExam.php", 
                data: {name : username, pwd : password },
                success: function(data) {
                     alert("Form submitted successfully");
                }
              });     

   }); 

Server Side

<?php

// file : newExam.php     

// to view  post array
if(!empty($_POST))
{
   print_r($_POST);
}

// access individual element
if(isset($_POST['name']))
{
   echo $_POST['name'];
}

?>

Comments

1

The above representation of the data variable shows an error.

Absolutely. That is correct because there is an error. You have a variable and you are assigning a query string with : where it should be =:

data= "name="+username+"&pwd="+password;

But this is not a good idea because you have to post the values not the input objects. username is an html input element, instead you should post an object like:

$("#submit_login").click(function(){
   var username=$('input[name=user_email]').val();
   var password=$('input[name=user_password]').val();
   var data = {name:username, pwd:password};
   $.ajax({
     type: "POST",
     dataType: "json", // <---make sure you return json from the php side.
     url: "newExam.php", 
     data: data,
     success: function(data) {
         alert("Form submitted successfully");
     }
   });     
}); 

Comments

0

Your selectors dont look right to me.

   var username=$('input[name="user_email"]');
   var password=$('input[name="user_password"]');

Note the double quotes around the input name attributes

Comments

0

Can you try the below code format:

data: {name : username, pwd : password }

Comments

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.