tags:

views:

27

answers:

2

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

+1  A: 

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 ?

thedev
@thedev - I understand that in theory, but not in practice. How would I accomplish this, Im not that versed in PHP - yet.
Dirty Bird Design
you can send the parameter using GET method by modifying the url from http://www.registrationpageURL.php to http://www.registrationpageURL.php?myParameter=1 and in the registrationpageURL.php file you can access this using $_GET["myParameter"]
thedev
+1  A: 

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!

mattbasta
@mattbasta, I don't believe this will work with the script on another domain/server as it is subject to the "same origin policy" - per the jQuery API. It has to reside with the other scritps for our CRM to work correctly. This is why I mentioned my issues with cURL closing the MySQL connection. Am I wrong?
Dirty Bird Design
@Dirty My bad; forgot it was going to be cross-domain. I'll edit!
mattbasta
currently I have the forms submitting with validate.js. I have used the .ajax call before inside the submitHandler function...same applies here?
Dirty Bird Design
@mattbasta i can't get it to do anything, here is code http://jsbin.com/icule3/2 can you please take a look?
Dirty Bird Design
@Dirty It looks like you don't have comment tags around your scripts.
mattbasta