Solved

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

Posted on 2013-06-12
11
614 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
Comment Utility
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
Comment Utility
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
$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
Comment Utility
url: 'http://phptarget.php/',

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

Author Comment

by:Webspeeder
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Webspeeder
Comment Utility
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
Comment Utility
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
Comment Utility
I removed the w_ from the names.
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Paste your ajax code
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
I started completely over. I don't know where the code was jacked up, but it works now. Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

7 Experts available now in Live!

Get 1:1 Help Now