1

I'm new to php and ajax. I want to run simple code of ajax with php but the code is not working as intended. I want load some text in the page when onchange happen

source code ajax.php

<select id='mySelect' onChange="MyFunction()">
    <option value='option1'>Option1</option>
    <option value='option2'>Option2</option>
    <option value='option3'>Option3</option>
</select>




<script>

function MyFunction()
{
    var params="myName=2";
    var xmlHttp;
    if(window.XMLHttpRequest)
        xmlHttp=new XMLHttpRequest();
    else
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    xmlHttp.open("GET","edittest.php",true);
    if(xmlHttp)
    {

    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4 && xmlHttp.status==200)
        {

            alert(document.getElementById('mySelect').value);
            }
        }
    }

    xmlHttp.send(params);
    }

</script>

edittest.php source code

<?php

if(isset($_GET))
{
    for($i=0;$i<1000;$i++)
    {
        echo($_GET['myName']);
        }
    }

?>
IS_ELKADY
  • 21
  • 4

2 Answers2

1

For a much simpler experience, I suggest you use jQuery AJAX. Here is the code. Check the Documentation for more details.

function MyFunction()
{
    var myName=2;
$.ajax({
    url: "/edittest.php",
    data: {
        myName: myName
    },
    type: "GET",
    success: function(response){
        console.log(response);
    }
});
}
Parthapratim Neog
  • 4,352
  • 6
  • 43
  • 79
  • Did u include jqeury? – Parthapratim Neog Nov 26 '15 at 09:56
  • use Firebug in Firefox, Press F12 to view the console, then click onChange, and see the output in Console. It would be helpful if you could post the response from ur console. – Parthapratim Neog Nov 26 '15 at 09:58
  • do I have to install any plugins for ajax!!! I installed apache 2.4 and php I used dreamweaver and all in one zend studio and php designer 8 I tried everything but the same result. – IS_ELKADY Nov 26 '15 at 19:23
  • No u dont need any extra plugin for Ajax with simple javascript ... but if u r using jquery then u will need to include the jquery plugin. – Parthapratim Neog Nov 26 '15 at 19:25
0

Your problem is that you declare var params="myName=2";, but you never use it. Your script works, but since it is just requesting the edittest.php, it wont show you anything, since your PHP script only prints out the myName variable.

You should add the parameters at the end of your request like this:

<select id='mySelect' onChange="MyFunction()">
    <option value='option1'>Option1</option>
    <option value='option2'>Option2</option>
    <option value='option3'>Option3</option>
</select>

<script>
    function MyFunction() {
        var params = "myName=2";
        var xmlHttp;
        if (window.XMLHttpRequest)
            xmlHttp = new XMLHttpRequest();
        else
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

        xmlHttp.open("GET", "edittest.php?" + params, true);
        if (xmlHttp) {
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    alert(document.getElementById('mySelect').value);
                }
            }
        }
        xmlHttp.send(params);
    }
</script>

(Notice the xmlHttp.open("GET", "edittest.php?" + params, true); part.)

Ivar
  • 6,138
  • 12
  • 49
  • 61
  • thanks for your effort but it seems the same result. also I tried to use post with xmlHttp.send(params); and the result the same – IS_ELKADY Nov 26 '15 at 09:08
  • @IS_ELKADY If you use Google Chrome, press the F12 key, go to the network tab, and change your `mySelect` box, to see what happens. That can narrow down your problem. (Check if the request is sent, and if so what status you get back) – Ivar Nov 26 '15 at 09:10