Solved

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

Posted on 2013-06-12
11
617 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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 dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now