I m developing a web site for my hobby project. I m new to PHP, JavaScript, and HTML. So far everything works correctly i.e the javascript functions communicate with my php code but the problem is the browser loads the php file too. I don't want to load the php file. I just want to pass the value of the input tag on clicking a button. This value is stored in the value attribute of the input tag as shown below.
<input name="switch" class="button" type="button" value="ON" onclick="change(this)" />
I know this problem can be solved by AJAX. Now I m trying to implement the AJAX methods as it is given in w3schools website.
Below I have attached the two codes to understand what i m trying to tell.
function change( el)
{
if ( el.value == "ON" )
{
el.value = "OFF";
el.style.backgroundColor = "#ff3333"; /* Red */
}
else
{
el.value = "ON";
el.style.backgroundColor = "#4CAF50"; /* Green*/
} // works till here like expected.
// this proves that HTML code communicates with this function
// and also there's a value in 'el.value' which is received from the
// value attribute of the input tag.
var xmlhttp = new XMLHttpRequest(); // create a xmlhttp object
xmlhttp.onreadystatechange = function() // create a function to
// execute when the server response is ready
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{ // empty
}
}
//I just want to send the value in 'el.value' to postform.php file
//In 'postforn.php' file i want to display the received value.
xmlhttp.open("GET", "postform.php?switch=" + el.value, true)
xmlhttp.send();
}
Here's the php code i m trying to execute.
<?php
$value = $_GET["switch"];
if (empty($value))
{
echo "Name is empty\n";
}
else
{
echo $value;
}
?>
I want the variable $value to be displayed somewhere so that i can see what is received.