Jump to content

Checkbox executes MySQL Query


solarisuser

Recommended Posts

Hi All,

 

I want to have a checkbox that says "Do not show me this popup anymore" that when clicked, it automatically sends a MySQL query like "UPDATE .... SET no_more_popup = '1'".  This is so the user can select the checkbox and then close the popup, without having to submit a form that executes the MySQL query.

 

I am using PHP but I think this is purely a JS thing.

 

Does anyone happen to know of a tutorial or script that does this, or should I look at some other technology?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/45327-checkbox-executes-mysql-query/
Share on other sites

Javascript:

function no_more_popup()
{
var httpRequest;

if(window.XMLHttpRequest)
{
	httpRequest = new XMLHttpRequest();
	if(httpRequest.overrideMimeType)
	{
		httpRequest.overrideMimeType("text/xml");
	}
}
else if(window.ActiveXObject)
{
	try {
		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
		}
	}
}

if(!httpRequest)
{
	alert("Could not save setting...");
	return false;
}

httpRequest.onreadystatechange = function() { alertResults(httpRequest); };
httpRequest.open("GET", "http://www.example.com/no_more_popup.php", true); // <------ change this url
httpRequest.send(null);
}

function alertResults(httpRequest)
{
if(httpRequest.readyState == 4)
{
	if(httpRequest.status == 200)
	{
		alert("Setting saved. You will no longer see this popup...");
	}
	else {
		alert("Could not save setting...");
	}
}
}

 

Then have PHP do the MySQL work in no_more_popup.php

It looks nice, but I can't seem to get it to work with this test page...

 

Could you please correct me...

 

I have <html><script> , your code, </script>, then

<FORM NAME="myform" ACTION="" METHOD="GET">
Enter something in the box: <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="no_more_popup()">
</FORM>
</html>

 

I figured that'd be how I'd send the "inputbox" using $_GET to the webpage I put in your code.  In that page that AJAX sends the GET, I have:

<?
print_r($_GET);
?>

so I can see if the inputbox was sent, but it was not.

 

Thanks

 

 

It doesn't send contents of the form to the page. It only opens a connection to the page. To send the contents of inputbox it would be something like this:

<script type="text/javascript">
function no_more_popup()
{
var httpRequest;
var input = document.getElementById("inputbox");

if(window.XMLHttpRequest)
{
	httpRequest = new XMLHttpRequest();
	if(httpRequest.overrideMimeType)
	{
		httpRequest.overrideMimeType("text/xml");
	}
}
else if(window.ActiveXObject)
{
	try {
		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
		}
	}
}

if(!httpRequest)
{
	alert("Could not save setting...");
	return false;
}

httpRequest.onreadystatechange = function() { alertResults(httpRequest); };
httpRequest.open("GET", "http://www.example.com/no_more_popup.php?input="+input.value, true); // <------ change this url
httpRequest.send(null);
}

function alertResults(httpRequest)
{
if(httpRequest.readyState == 4)
{
	if(httpRequest.status == 200)
	{
		alert("Setting saved. You will no longer see this popup...");
	}
	else {
		alert("Could not save setting...");
	}
}
}
</script>

<form name="myform">
Enter something in the box: <br />
<input type="text" name="inputbox" id="inputbox" /> <input type="button" name="button" value="Click" onclick="no_more_popup()" />
</form>

 

Note: I haven't tested any of the posted code, so it might not work. What you are using is popularly called AJAX, but it is really just taking use of XmlHttpRequest in Javascript.

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.