improve my PHP user interaction – Education Career Blog

I have a situation where a user fills out 1 of 2 forms on a registration page and is sent to a software download page. If they sign up as a new user, form is processed inserted into a MySQL database and they go to the page no problem.

Here is my issue. If they are a returning user and enter a license key, the processor script checks to see if its valid against the database and if it is it sends them to the software download page. If it is NOT a valid license key (heres what I dont like) the screen goes to the url of the script, page is white, an alert pops down telling them its not a valid license key and they are returned to the registration page to try again. I hate this. I need to figure out a way to either pop the alert on the registration page w/o leaving it or better yet display some kind of message on the page. One drawback is that the script is and always will be on a different server than my forms. Ive tried curl and had success with other situations but can’t close the MySQL connection on this one. Is there another way to achieve some semblance of “cross domain AJAX” I would really like it to not go to the script url/white page/alert then return them. I would like it to happen all on one page. Here is that part of my script:

if ($_POST'license_code' != "")
{
    $result = mysql_query("(//mysql stuff here)");



    if (($row = mysql_fetch_assoc($result)))
    {
        header("Location: http://" . $redirect);
    }
    //here is the part I dont like
    else        
    {
        echo "<html>\n";
        echo "<body>\n";
        echo "<script language=\"Javascript\">\n";
        echo "alert (\"The license ID you entered was not correct.\");\n";
        echo "window.location=\"http://www.registrationpageURL.php\";\n";
        echo "</script>\n";
        echo "</html>\n";
        echo "</body>\n";
    }
    mysql_close($link); 
}
//I use jquery valiadate.js for CS validation, but realize this is necessary and would like it to behave like the desired result for the above
else
{
  if (strpos($_POST'email1', '@') === false)
{
        echo "<html>\n";
        echo "<body>\n";
        echo "<script language=\"Javascript\">\n";
        echo "alert (\"The email address you entered was not correct.\");\n";
        echo "window.location=\"http://www.registrationpageURL.php\";\n";
        echo "</script>\n";
        echo "</html>\n";
        echo "</body>\n";    
  return;
}

thx

,

Is it possible to remove the alert and when you redirect to registrationpage.php also send a parameter using the redirect url and popup an alert or error message after the redirect ?

,

Look into using AJAX. jQuery has a great API for this:

EDITIED – For cross-domain

You could do something like this:

<div id="results"></div>
<script type="text/javascript">
$("#the_form").submit(function() {
    $.getJSON("http://remote.domain/script/to/validate.php?data=" + escape($(this).serialize()) + "&callback=?", function(data) {
        $("#results").html(data);
    });
    return false;
});
</script>

This will (once the IDs are pointed at the correct elements) intercept the form submission, pull together the values from the form (through the serialize() function), and shoot it out to the validation script via AJAX. The output of the script is displayed in the #results div.

Hope this helps!

Leave a Comment