Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-06-12
11
Medium Priority
?
641 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 2000 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
How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

 
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

721 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