Solved

Making AJAX call to PHP program: How to get input parameters??

Posted on 2013-06-12
11
623 Views
Last Modified: 2013-06-12
Hey all.

I've tried using Google but I can't get this to drop.

I am using the following call to a PHP program. All I want to do is simply extract the two fields in order to use them inside the PHP program.

var postData = {
                "user": "test",
                "passwd": "9999"
                };

      php_url="/phptarget.php";
   
    reqGroups = $.ajax({url: php_url, type: "POST", data: postData, error: err, timeout: 50000, dataType: 'json'});

The PHP program is a new program so any example of getting the parms would be helpful. Here is a starting point.

<?php

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

$w_user = $_POST['w_user'];
$w_passwd = $_POST['w_passwd'];


......

Another question is whether or not this is needed: header('Content-type: application/json');

If so, do I just put it at the top of the program?
0
Comment
Question by:Webspeeder
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39241155
Why are you passing json data to a php page? Just use a normal querystring, using Json you then have to parse it out again in the php page.

$.ajax({
    type: 'POST',
    url: 'http://phptarget.php/',
    data: { 
         "user": "test",
          "passwd": "9999"
    },
    success: function(msg){
        // Add actions to perform if the post was successful
    }
});

Open in new window


Then in your php page you can just use
$user = $_POST['user']
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39241156
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39241162
$w_user = $_POST['user'];
$w_passwd = $_POST['passwd'];

Open in new window


If so, do I just put it at the top of the program?
Put it before any output (echo, print, die "reason",...)
and be sure your JSON object is valid (double quotes, not simple  quote for example) :
http://jsonlint.com/
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

Expert Comment

by:Gary
ID: 39241166
url: 'http://phptarget.php/',

Should be
url: '/phptarget.php',
0
 

Author Comment

by:Webspeeder
ID: 39241254
Gary123, you're right, there is no need to be using JSON. But when I change it over, I'm still not able to see the two parameters in the PHP file.
0
 

Author Comment

by:Webspeeder
ID: 39241266
This is what I'm echoing out from PHP.

$userid = $_POST['user'];
$pass = $_POST['passwd'];

echo '<br>'.'ID: '.$userid.'<br>'.'Pass: '.$pass.'<br>';

I get blank for both.
0
 

Author Comment

by:Webspeeder
ID: 39241277
I am getting an error, but is is "undefined index" which can be resolved with the ISSET() but that would only allow a bypass to the error, doesn't resolve the variables being blank.
0
 

Author Comment

by:Webspeeder
ID: 39241280
I removed the w_ from the names.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39241293
Paste your ajax code
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39241301
Try this :
var postData = { "user": "test", "passwd": "9999" };
php_url = "/phptarget.php";
$.post(php_url, postData, function(){ alert("ok"); }, "json");

Open in new window

0
 

Author Comment

by:Webspeeder
ID: 39241332
I started completely over. I don't know where the code was jacked up, but it works now. Thanks.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question