Skip to content Skip to sidebar Skip to footer

Ajax: How To Change A Value On Client Side As Well As Server Side On Button Click?

In the following SSCCE, I have a string which contains the HTML for three divs. I add a style='display:none;' attribute to all the divs except the first one. I add a button to all

Solution 1:

Zarah, a couple of ideas that might help you:

As I said in my comment, try to change this bit:

xmlhttp.open("GET","C:/xampp/htdocs/testing.php?pass_back="+"pass_back",true);

for something like:

xmlhttp.open("GET","testing.php?pass_back="+"pass_back",true);

taking into consideration that that's a valid route to a file called testing.php in your web server. The url parameter of the open() method, must be an address to a file on a server and you must use a valid URL that points to that file.

Another idea. You can send post information using this approach:

xmlhttp.open("POST","testing.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("passback=passback");

so you could try to send it using POST instead of GET to see what happens. It might throw some light into the matter.

More things.

It is possible that due to your php configuration, $_REQUEST doesn't contain anything whereas $_GET does. This could be a good reason to check $_GET instead of $_REQUEST. However, if you do want to use $_REQUEST, here you can find more info about the topic.

EDIT

The following code (based on yours) works for me (debian APACHE/php 5.4). I've put all the code on the same page. I don't like it very much but it's only to point out that it works. The AJAX part sends the data to main.php and main.php simply sends back what it receives. Then the AJAX part simply alerts the answer from the server.

main.php

<?php
//**********************************************
//IF $_REQUEST CONTAINS pass_back this is an AJAX call, just send back and die.
if (array_key_exists('pass_back',$_REQUEST)) {
    echo $_REQUEST["pass_back"];
    die();
}
//***********************************************

echo '<html><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><script>functionclickButton(currentId, nextId) {
            //alert(currentId+","+nextId); //check/*document.getElementById(currentId).style.display = "none";
            document.getElementById(nextId).style.display = "block";
            document.getElementById(nextId).style.border = "5px solid red";//check*///**************************var xmlhttp;
            if (window.XMLHttpRequest) { xmlhttp=newXMLHttpRequest(); }
            else { xmlhttp=newActiveXObject("Microsoft.XMLHTTP"); }

            xmlhttp.onreadystatechange=function() {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                alert(xmlhttp.responseText);
                }
            }

            xmlhttp.open("GET","testing.php?pass_back=pass_back",true);
            xmlhttp.send();
            //**************************

        }
    </script></head><body>';


//String of all the div's
$haystack = '<divid="div1"style="width:500px; height:250px; background-color:#fd77ba">Div1</div><divid="div2"style="width:500px; height:250px; background-color:#7781fd">Div2</div><divid="div3"style="width:500px; height:250px; background-color:#77fd9b">Div3</div>';

//Print all the divs
echo '<formmethod="post"enctype="multipart/form-data"accept-charset="utf-8">';
echo $haystack;
echo '<buttontype="button"onClick="clickButton(1,2)"style="font-family:Oxygen,sans-serif; font-style: normal;font-variant: normal;font-weight: normal;font-size: 99%;line-height: normal;font-size-adjust: none;font-stretch: normal; background-color: #494f50; color: #ffffff; padding-top: 5px;padding-right: 10px;padding-bottom: 5px; padding-left: 10px;margin-top: 50px;margin-right: 10px;margin-bottom: 50px;margin-left: 10px; text-decoration:none;">Submit</button>';
echo '</form>';
echo '</body></html>';
?>

Good luck.

Post a Comment for "Ajax: How To Change A Value On Client Side As Well As Server Side On Button Click?"