User clicks on a url, ie:

When I perform this in the code below:

$referrer = $_GET['_url'];
    // echo $referrer displays the referrer ID contents correctly as "AEQ438J"
    if ( ! empty($referrer))
    $mysqli->query("UPDATE coming_soon_emails SET clicks = clicks + 1 WHERE code='" . $referrer ."'");
    // this also updates the database correctly as it should

    if (!empty($_POST['email'])){

   // echo $referrer displays the referrer ID contents as BLANK. It should display "AEQ438J"!

$referrer displays correctly BEFORE if($_POST), however during the if($_POST) $referrer is empty.
How can I fix my code so that $referrer is not empty during the time the user posts their email address in the form?

Thank you!

Complete PHP and HTML

    require "includes/connect.php";
    function gen_code($codeLen = 7) {     
    $code = '';     
    for ($i=0; $i<$codeLen; $i++) {         
     $code .= $d ? chr(rand(65,90)) : chr(rand(48,57));      }  
     return $code; 
     function add_code($email_id) {
     global $mysqli;
     $code = gen_code(7); 
     $mysqli->query("UPDATE coming_soon_emails SET code='" . $code ."' WHERE email_id='" . $email_id . "'");  
     if($mysqli->affected_rows != 1) {   
     } else return $code; } 
    $msg = '';
    $referrer = $_GET['_url'];
    // echo $referrer displays the referrer ID contents correctly
    if ( ! empty($referrer))
    $mysqli->query("UPDATE coming_soon_emails SET clicks = clicks + 1 WHERE code='" . $referrer ."'");
    if (!empty($_POST['email'])){
    // echo $referrer displays the referrer ID contents as BLANK
        // Requested with AJAX:
        $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');
                throw new Exception('Invalid Email!');
            $mysqli->query("INSERT INTO coming_soon_emails
                            SET email='".$mysqli->real_escape_string($_POST['email'])."'");
            if($mysqli->affected_rows != 1){
                throw new Exception('This email already exists in the database.');
            } else {   
              $email_code = add_code($mysqli->insert_id); 
    		$msg = "" . $email_code;
    		//the following doesn't work as referrer is now empty
    		if ( ! empty($referrer))
    		$mysqli->query("UPDATE coming_soon_emails SET signup = signup + 1 WHERE code='" . $referrer ."'");
    			die(json_encode(array('msg' => $msg)));
        catch (Exception $e){
            $msg = $e->getMessage();        
    <!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="css/styles.css" />
    <div id="launch">
        <form id="form" method="post" action="">
        	<input type="text" id="email" name="email" value="<?php echo $msg;?>" />
            <input type="submit" value="Submit" id="submitButton" />
        <div id="invite">
        <p style="margin-top:20px;">The ID of who referred you: <?php echo $referrer; //this displays correctly?>)</p>
        <p style="margin-top:20px;"><span id="code" style="font-weight:bold;">&nbsp;</span></p>
    <script src=""></script>
    <script src="js/script.js"></script>

Javascript (script.js)

    	// Binding event listeners for the form on document ready
    	$('#email').defaultText('Your Email Address');
    	// 'working' prevents multiple submissions
    	var working = false;
    			return false;
    		working = true;
    						} else {
    			    $('#email').val(r.msg); // not needed but gets hidden anyways...
    				$('#launch form').hide(); 
    			working = false;
    		return false;
    // A custom jQuery method for placeholder text:
    $.fn.defaultText = function(value){
    	var element = this.eq(0);'defaultText',value);
    		if(element.val() == value){
    		if(element.val() == '' || element.val() == value){
    	return element.blur();


RewriteEngine on
    RewriteCond %{HTTP_HOST} ^
    RewriteRule (.*)$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-z0-9]+)$ /index.php?_url=$1 [NC,L,QSA]


CREATE TABLE IF NOT EXISTS `coming_soon_emails` (   
      `email_id` int(11) NOT NULL auto_increment,   
      `email` varchar(64) collate utf8_unicode_ci NOT NULL,   
      `code` char(7) collate utf8_unicode_ci DEFAULT NULL,
      `clicks` int(64) collate utf8_unicode_ci DEFAULT 0,
      `signup` int(64) collate utf8_unicode_ci DEFAULT 0,
      `ts` timestamp NOT NULL default CURRENT_TIMESTAMP,   
      PRIMARY KEY  (`email_id`),   
      UNIQUE KEY `email` (`email`),
      UNIQUE KEY `code` (`code`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


In JS if I comment out the ajax call of


and in index.php comment out the line

$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

it then updates the DB correctly (the original issue is now fixed) and all works well!

However, now ajax no longer hides and shows the divs.. so knowing what the issue is, what can I do to sort it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.