LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2021, 01:48 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 384

Rep: Reputation: 9
how to use variable in AJAX => data:


Using Ubuntu 20.04

Code:
  $("#doctor").focus(function(){
    	alert("focus is on #doctor");
    	indata="doctor";
}) 

    var request = $.ajax({
    url: "dropList.v_final.php",
    type: "POST",
    data: {"doc":indata},
    dataType: "json"          
});
I cannot get this AJAX call to pass the data to the URL. print_r($_POST) shows an empty array.

If I use data: {"doc":"doctor"} print_r($_POST) shows [doc] doctor as it should.

I have tried various combinations with quotes but no joy.
 
Old 07-21-2021, 03:45 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,117

Rep: Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687
Can you format that better? It looks like indata isn’t initialized at the time of the Ajax call, but it’s hard to tell when the formatting is that poor.
 
Old 07-21-2021, 05:32 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 384

Original Poster
Rep: Reputation: 9
Here is the whole block which I changed;

Code:
script tag type="text/javascript" 

$(document).ready(function(){

var indata={};

  $("#doctor").focus(function(){
    	alert("focus is on #doctor");
    	indata={doc: "doctor"};
}) 

    var request = $.ajax({
    url: "dropList.v_final.php",
    type: "POST",
    data: indata,
    dataType: "json"          
});

}); // doc ready

/script tag
Here is the result from the URL;

Quote:
Array
(
)

Last edited by pizzipie; 07-21-2021 at 06:10 PM.
 
Old 07-21-2021, 06:25 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,117

Rep: Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687
It's still too badly indented to bother trying to read.
 
Old 07-21-2021, 08:25 PM   #5
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 384

Original Poster
Rep: Reputation: 9
Thanks for your time and effort to help me.

I can't make it any plainer than I have already!

Anyway, if anybody else is interested here is the solution that I discovered and it works.

Code:
script tag type="text/javascript" 

$(document).ready(function(){

  $("#doctor").focus(function(){
	   indata={doc: 'doctor'};
	   myAjaxCall1(indata);
   
}) 

}); // doc ready

/script tag

  // ============== myAjaxcall1  ========
  
function myAjaxCall1(x) {
		 request = $.ajax({
    url: "dropList.v_final.php",
    type: "POST",
    data: x,
    dataType: "json"          
});

    request.done(function() {
    	//under construction 
    });
}
 
Old 07-22-2021, 06:23 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,380

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Hi

When you send a POST request to PHP, and a variable is an array, PHP will not get the variable as an array. The same variable get written to again and again, and you usually end up with the last value. It doesn't matter if it's ajax or not.

There are two solutions.

One is to add [] after the variable name. This only works if it's a "flat" array. A nested array will not work.

The other is to JSON encode it in Javascript, and decode it in PHP.

Javascript:
Code:
data: {"doc": JSON.stringify(indata)},
PHP:
Code:
$doc = json_decode($_POST['doc']);
 
Old 07-22-2021, 10:24 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,291
Blog Entries: 4

Rep: Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318
Use a web browser's diagnostic tools to look at the actual POST payload that is received. Looks like an uninitialized server-side variable to me ... the content is probably going to turn out to look like: { "doc": NULL }
 
Old 07-22-2021, 11:38 AM   #8
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 384

Original Poster
Rep: Reputation: 9
Thanks for your replys. I don't know how this fits with your answers but I'll tuck this info away for use.


Quote:
Use a web browser's diagnostic tools to look at the actual POST payload that is received. Looks like an uninitialized server-side variable to me ... the content is probably going to turn out to look like: { "doc": NULL }
In my URL print_r($_POST); produces this: Array ( [doc] => doctor ). Which, of course is what I want.

Quote:
When you send a POST request to PHP, and a variable is an array, PHP will not get the variable as an array. The same variable get written to again and again, and you usually end up with the last value. It doesn't matter if it's ajax or not.
I used this:

Code:
 $("#doctor").focus(function(){
	   indata={doc: 'doctor'};
	   myAjaxCall1(indata);
   
}) 

//  ================  function myAjaxCall1  ===========

function myAjaxCall1(x) {
    var request = $.ajax({
    url: "dropList.v_final.php",
    type: "POST",
    data: x,
    dataType: "json"          
});
 
Old 07-22-2021, 01:38 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,291
Blog Entries: 4

Rep: Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318
What I very-specifically recommended, pizzipie, is that you go to the web browser network diagnostic screens, and use that to look at the actual text of the POST-reply payload that was sent by the host and finally received by the client. It is not the same thing to print that on the host.

Also, check the same console to ensure that no client-side error messages were logged. JavaScript won't complain to the user, in any visible way, when an error happens ... it just keeps going.

Last edited by sundialsvcs; 07-22-2021 at 01:40 PM.
 
Old 07-22-2021, 08:08 PM   #10
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 384

Original Poster
Rep: Reputation: 9
Quote:
What I very-specifically recommended, pizzipie, is that you go to the web browser network diagnostic screens, and use that to look at
Maybe a dumb question but how do I do that?
 
Old 07-23-2021, 11:07 AM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,618

Rep: Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315

The answer is provided within https://developer.mozilla.org/en-US/docs/Learn along with plenty more essential knowledge.

 
1 members found this post helpful.
Old 07-23-2021, 11:51 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,291
Blog Entries: 4

Rep: Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318
Every web browser has "developer screens" somewhere. (You might have to enable some "preferences" setting to see them.)

These will let you watch the actual network exchanges: what was sent, and what the server's reply was. The trickiest thing about resolving AJAX issues is to simply see them. Once you can see what was sent and how it is wrong, you can begin to figure out why. It's also important to be sure that JavaScript isn't throwing an error without telling you.
 
Old 07-23-2021, 01:15 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,715
Blog Entries: 11

Rep: Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747
Different browsers will have their own ways of accessing their web debugging tools. The link provided by boughtonp above is indeed essential reading!

To get you quickly into the available options with Firefox, click on the Tools menu item (assumes the menu bar is visible), then explore the options under Web Developer. From there you can see javascript errors, AJAX requests and responses, the network activity mentioned by sundialsvcs, and much more!

Last edited by astrogeek; 07-23-2021 at 01:17 PM.
 
Old 07-25-2021, 04:50 PM   #14
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 384

Original Poster
Rep: Reputation: 9
Sorry, I'm losing it!! I use Firefox: Web Developer Tools all the time. Couldn't get along without it!!

`R
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending data from AJAX call to PHP - can't read data pizzipie Programming 18 06-18-2016 12:08 PM
PHP - in-data from AJAX call will not work in SYSTEM() - using bash same data works pizzipie Programming 6 06-28-2014 12:30 AM
LXer: Solid Ajax applications, Part 2: Building Ajax back ends LXer Syndicated Linux News 0 01-22-2008 12:30 PM
LXer: Eclipse Ajax Toolkit Framework and Ajax tools LXer Syndicated Linux News 0 05-12-2006 12:21 PM
LXer: Mastering Ajax, Part 3: Advanced requests and responses in Ajax LXer Syndicated Linux News 0 02-16-2006 06:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration