Welcome to Dream.In.Code
Become an Expert!

Join 149,532 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,529 people online right now. Registration is fast and FREE... Join Now!




using HTML selects to change PHP vars through AJAX

 
Reply to this topicStart new topic

using HTML selects to change PHP vars through AJAX, the title says it all...

strykerhorse
28 Jun, 2007 - 02:59 PM
Post #1

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 38


My Contributions
Hello all,

I'm trying to get a page changed over from synchronous form submits to asynchronous (AJAXified) ones. I am very new to AJAX, but from the numerous tutorials I've read, this should be a very easy task.

The following is a select that I've prepopulated with values through simple PHP code:

CODE

<form name="fixform">
.
.
.
    <select name="select1">
        <option value="<?php $range[0] = 0; echo $range[0];?>" title="One day">One Day</option>
        <option value="<?php $range[1] = 1; echo $range[1];?>" title="Three days">Three days</option>
        <option value="<?php $range[2] = 2; echo $range[2];?>" title="One week">One week</option>
        <option value="<?php $range[3] = 3; echo $range[3];?>" title="Two weeks">Two weeks</option>
    </select>
.
.
.



The PHP this code will be acting on:

CODE



$biggestIndex = "";
switch($_GET['select1'])
{
    case '0':
        $biggestIndex = 2;
        break;
    case '1':
        $biggestIndex = 8;
        break;
    case '2':
        $biggestIndex = 20;
        break;
    case '3':
        $biggestIndex = 41;
        break;
    default:
        break;
}


My objective is to use the $_GET['select1'] variable to change the switch() statement shown in the second block of code in this manner:
-page loads, brings up something basically blank excepting the select.
-I choose a value of the select, and as soon as I click that value, a picture corresponding to the value of $biggestIndex (chosen through the switch() statement) posts in a div that I've specified.

My XMLHTTPRequest is taken directly from the w3schools.com AJAX tutorial and modified to reflect my variables. I would hope (by virtue of it being on a well-known site like that) that it is correct.

The XMLHTTPRequest:

CODE

function ajaxFunction()//stuff to start XMLHTTPRequest object for the AJAX'd RIPE0 KCore changes
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
        xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        document.fixform.select1.value=xmlHttp.responseText;
        }
      }
    xmlHttp.open("GET","index.php",true);
    xmlHttp.send(null);
  }


To this end, I've tried troubleshooting this by setting variables to check and see if $_GET is being populated...to no avail.

Any ideas on what could be the problem? Thanks for all your help.

NLC
User is offlineProfile CardPM
+Quote Post

ahmad_511
RE: Using HTML Selects To Change PHP Vars Through AJAX
28 Jun, 2007 - 09:16 PM
Post #2

D.I.C Regular
Group Icon

Joined: 28 Apr, 2007
Posts: 351



Thanked: 8 times
Dream Kudos: 400
My Contributions
Hi
First I would like to ask something
What is the benefit of using something like "<?php $range[0] = 0; echo $range[0];?>"
when you can directly add your values

Anyway

I think when you want to send data using GET through ajax you need code like this
CODE

xmlHttp.open("GET","index.php?select1="+ fixform.select1.value,true);

And don't forget to call your ajax instead of submiting the form
CODE

<input type="button" onclick="ajaxFunction()">

This way ajax can send select1's value
User is offlineProfile CardPM
+Quote Post

strykerhorse
RE: Using HTML Selects To Change PHP Vars Through AJAX
29 Jun, 2007 - 08:10 AM
Post #3

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 38


My Contributions
QUOTE(ahmad_511 @ 28 Jun, 2007 - 11:16 PM) *

Hi
First I would like to ask something
What is the benefit of using something like "<?php $range[0] = 0; echo $range[0];?>"
when you can directly add your values

Anyway

I think when you want to send data using GET through ajax you need code like this
CODE

xmlHttp.open("GET","index.php?select1="+ fixform.select1.value,true);

And don't forget to call your ajax instead of submiting the form
CODE

<input type="button" onclick="ajaxFunction()">

This way ajax can send select1's value



ahmad_511,

Thanks for your reply. To answer your question about the PHP, I'm using the $range[] array to control other elements in the page that I didn't mention in my previous post.

I'll try your suggestions out, and hopefully they'll give me some clue as to what's working and what isn't in my code.

_NLC
User is offlineProfile CardPM
+Quote Post

strykerhorse
RE: Using HTML Selects To Change PHP Vars Through AJAX
2 Jul, 2007 - 12:07 PM
Post #4

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 38


My Contributions
No luck with those suggestions, at best, they returned the first line of the HTML document (the <DOCTYPE> declaration). Weird, I know, but that's what happened.
User is offlineProfile CardPM
+Quote Post

ahmad_511
RE: Using HTML Selects To Change PHP Vars Through AJAX
2 Jul, 2007 - 01:35 PM
Post #5

D.I.C Regular
Group Icon

Joined: 28 Apr, 2007
Posts: 351



Thanked: 8 times
Dream Kudos: 400
My Contributions
Can you send me your lastest update of (html + javascript + php) codes and i'll try to fix it for you

by the way there is a snnipet called Ajax
Try to use it and if you have any problem to use send me a reply

This post has been edited by ahmad_511: 2 Jul, 2007 - 01:36 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:01PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month