solarisuser Posted April 2, 2007 Share Posted April 2, 2007 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! Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted April 2, 2007 Share Posted April 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
solarisuser Posted April 2, 2007 Author Share Posted April 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted April 3, 2007 Share Posted April 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.