Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2007, 03:31 AM   PM User | #1
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
Passing Javascript var to PHP without redirection

I'm pretty sure this is possible with AJAX.

Also, please respond to this, even if its just a link to a tut. This would solve a lot of problems for me!

How do I pass a javascript variable to PHP WITHOUT REDIRECTION

Is there an AJAX solution?

ty!

~binaryWeapon
binaryWeapon is offline   Reply With Quote
Old 11-05-2007, 04:10 AM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
That's exactly what AJAX is, an HTTP Request sent to the server via Javascript without the browser reloading the page. Google ajax +tutorial to learn the basics.
__________________
Fumigator is offline   Reply With Quote
Old 11-05-2007, 01:53 PM   PM User | #3
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
Oh ... so thats what its for .... ;-)

Code:
function callback(serverData, serverStatus) {       // Called automatically when we get data back from server
   alert(serverData);                               // Display an alert box with the recieved data
}

function ajaxRequest() {
   var AJAX = null;                                 // Initialize the AJAX variable.
   if (window.XMLHttpRequest) {                     // Does this browser have an XMLHttpRequest object?
      AJAX=new XMLHttpRequest();                    // Yes -- initialize it.
   } else {                                         // No, try to initialize it IE style
      AJAX=new ActiveXObject("Microsoft.XMLHTTP");  //  Wheee, ActiveX, how do we format c: again?
   }                                                // End setup Ajax.
   if (AJAX==null) {                                // If we couldn't initialize Ajax...
      alert("Your browser doesn't support AJAX.");  // Sorry msg.                                               
      return false                                  // Return false, couldn't set up ajax
   }
   AJAX.onreadystatechange = function() {                      // When the browser has the request info..
      if (AJAX.readyState==4 || AJAX.readyState=="complete") { //  see if the complete flag is set.
         callback(AJAX.responseText, AJAX.status);             // Pass the response to our processing function
      }                                                        // End Ajax readystate check.
   }
   var url='http://somedomain.com/getdata.php'; // This is the URL we will call.
   AJAX.open("GET", url, true);                                  // Open the url this object was set-up with.
   AJAX.send(myVariable);                                              // Send the request.
}
Here's what I've found (on a tutorial). Would this work? And what PHP would I need?

Last edited by binaryWeapon; 11-05-2007 at 02:22 PM..
binaryWeapon is offline   Reply With Quote
Old 11-05-2007, 04:37 PM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Yeah, just add the variables to the URL.
Code:
var url = 'http://somedomain.com/getdata.php?variable=1&variable2=2';
And you would access them in your PHP page by using the $_GET superglobal array:
PHP Code:
$var $_GET['variable'];
$var2 $_GET['variable2']; 
Inigoesdr is offline   Reply With Quote
Old 11-06-2007, 03:31 AM   PM User | #5
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
OK so where it says
Code:
   AJAX.send(myVariable);
I can just send null? (That's what it had in the tutorial, I just changed it 'cause I thought that's what would send the var.)

So it would be
Code:
   AJAX.send(null);
binaryWeapon is offline   Reply With Quote
Old 11-06-2007, 03:39 AM   PM User | #6
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Yes, I believe you can. Why don't you try it and find out for yourself?
Inigoesdr is offline   Reply With Quote
Old 11-06-2007, 03:43 AM   PM User | #7
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
willdo! thx for ur help!
binaryWeapon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:53 PM.


Advertisement
Log in to turn off these ads.