Jump to content

Need Help Checking $_GET Variable - Reference Code Included


bgearig

Recommended Posts

I am using AJAX to send form information to a PHP script for a database transaction. The transaction is successful, but now I am "proof-reading" the information sent to prevent certain information from being sent to the script, such as empty strings or strings with JUST SPACES.

 

The Javascript is as follows:

function foo(form_name)
{
if (window.XMLHttpRequest)
{
	xmlhttp=new XMLHttpRequest();
}
else
{
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","./bar.php?action=add&value="+escape(document.forms[form_name].some_select_menu.value)+"|"+escape(document.forms[form_name].some_text_box.value)+"&rand="+Math.floor(Math.random()*1001),false);
xmlhttp.send(null);
if (xmlhttp.responseText == "0")
{
	alert("Error inserting into Database. Please check your entry!");
}
else
{
	alert("Success! "+xmlhttp.responseText);
	document.forms[form_name].some_text_box.value = "";
	reload_select_menu(form_name);
}
}

 

The bar.php file contains the following relevant function:

elseif ($_GET['action'] == "add")
{
$temp = explode("|",urldecode($_GET['value']));
if (trim($temp[1]) != "" || $temp[1] != NULL)
{
	some_sql_query();
	echo("1");
}
else
{
	echo("0");
}
}

 

So when I send the information with nothing in the textbox, I get the proper error message, "Error inserting into Database. Please check your entry!".  However, when I just place a space in the textbox and submit that, I receive the message "Success! 1". What should I be checking in the if statement differently?

Oh dear... Maybe someone should add to the guidelines to drink your morning coffee before asking dumb questions.  I will answer my own question.

 

Hey bgearig!  You should try changing || to && and finish your morning coffee!

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.