Search code examples
phpformsisset

PHP if (isset($_POST['uname'])) not doing anything?


I have two pages. 1st page has two text forms like so:

<form name="NameForm">
Name: <input type = "text" name = "uname">
Location: <input type = "text" name = "ulocation">

<button type="button" onClick="MakeRequest()"">Save</button>
</form>

It pushes the information into page number two using javascript like so (note this is on the same page as the code above):

<script>
function MakeRequest()
{
// get values
var uname = document.NameForm.uname.value;
var ulocation = document.NameForm.ulocation.value;

// validation stuff here that detects browser

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("result").innerHTML=xmlhttp.responseText;
    }
  }
var url = "location.php?uname=" + uname + "&ulocation=" + ulocation;
xmlhttp.open("POST", url, true);
xmlhttp.send();
}
</script>

So the problem is this, the php scripts on the page that does all server communication is not reading and storing the variables from this post request into my database.(my other get methods that view items in the database works fine)

if (isset($_POST['uname']))
{
    $name = $_POST['uname'];
$location = $_POST['ulocation']
}

then the query goes somehting like

//$table and the other undefined variables are the names of my table & columns
$query = "INSERT INTO $table ($tablename, $tablelocation) VALUES ('$name', '$location')";

Basically I'm trying to get that query to work. If i remove the If statement, it stores $name to the database but not $location.

EDIT: I forgot to add

<div id="result">
</div>

Solution

  • You are sending a GET. to send a POST try:

    [edited] perform the functions that order

    function XHR(){
        if(typeof XMLHttpRequest !=="undefined"){
            try{ return new XMLHttpRequest(); } catch (e){}
        }
        if(typeof ActiveXObject !=="undefined"){
            try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){}
            try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
        }
        return false;
    }
    
    function MakeRequest()
    {
        // get values
        var uname = document.NameForm.uname.value;
        var ulocation = document.NameForm.ulocation.value;
    
        // validation stuff here that detects browser
    
        var url = "location.php";
    
        xmlhttp = XHR();//Fixed
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//Fixed
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4) {
                if(xmlhttp.status==200){
                    document.getElementById("result").innerHTML = xmlhttp.responseText;
                } else {
                    document.getElementById("result").innerHTML = "ERROR:"+xmlhttp.status;
                }
            }
        };
        xmlhttp.send("uname=" + uname + "&ulocation=" + ulocation);
    }