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
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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.