Solved

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

Posted on 2013-06-12
11
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

636 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