Note that there are some explanatory texts on larger screens.

plurals
  1. PO$_GET and $_POST issue
    primarykey
    data
    text
    <p>The following is my problem (example code)..</p> <p>If I perform this:</p> <pre><code>$rf = $_GET['_id']; if($_POST['form']){ echo "ref: " . $rf; } </code></pre> <p><code>$rf</code> is blank during <code>if($_POST['form'])</code> </p> <p>Now, If I perform this:</p> <pre><code>$rf = "123456"; if($_POST['form']){ echo "ref: " . $rf; } </code></pre> <p><code>$rf</code> is displayed</p> <p>Why is it blank the first time and not blank if I assign something static to the $rf var? Also how can I fix this so the first solution works?</p> <p>Thanks!</p> <p>Note: don't worry about SQL injections.. I have already stripped everything from "_id".</p> <p><strong>Complete PHP and HTML</strong></p> <pre><code>&lt;?php require "includes/connect.php"; //var_dump($_GET);die; function gen_code($codeLen = 7) { $code = ''; for ($i=0; $i&lt;$codeLen; $i++) { $d=rand(1,30)%2; $code .= $d ? chr(rand(65,90)) : chr(rand(48,57)); } return $code; } function add_code($email_id) { global $mysqli; $code = gen_code(7); $mysqli-&gt;query("UPDATE coming_soon_emails SET code='" . $code ."' WHERE email_id='" . $email_id . "'"); if($mysqli-&gt;affected_rows != 1) { add_code($email_id); } else return $code; } $msg = ''; $referrer = $_GET['_url']; // echo $referrer displays the referrer ID contents correctly if ( ! empty($referrer)) { $mysqli-&gt;query("UPDATE coming_soon_emails SET clicks = clicks + 1 WHERE code='" . $referrer ."'"); } if (!empty($_POST['email'])){ // Requested with AJAX: $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); try{ if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){ throw new Exception('Invalid Email!'); } $mysqli-&gt;query("INSERT INTO coming_soon_emails SET email='".$mysqli-&gt;real_escape_string($_POST['email'])."'"); if($mysqli-&gt;affected_rows != 1){ throw new Exception('This email already exists in the database.'); } else { $email_code = add_code($mysqli-&gt;insert_id); } $msg = "http://www.my-url/" . $email_code; //the following doesn't work as referrer is now empty :( if ( ! empty($referrer)) { $mysqli-&gt;query("UPDATE coming_soon_emails SET signup = signup + 1 WHERE code='" . $referrer ."'"); } if($ajax){ die(json_encode(array('msg' =&gt; $msg))); } } catch (Exception $e){ if($ajax){ die(json_encode(array('error'=&gt;$e-&gt;getMessage()))); } $msg = $e-&gt;getMessage(); } } ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="css/styles.css" /&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="launch"&gt; &lt;form id="form" method="post" action=""&gt; &lt;input type="text" id="email" name="email" value="&lt;?php echo $msg;?&gt;" /&gt; &lt;input type="submit" value="Submit" id="submitButton" /&gt; &lt;/form&gt; &lt;div id="invite"&gt; &lt;p style="margin-top:20px;"&gt;The ID of who referred you: &lt;?php echo $referrer; //this displays correctly?&gt;)&lt;/p&gt; &lt;p style="margin-top:20px;"&gt;&lt;span id="code" style="font-weight:bold;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="js/script.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>script.js</strong></p> <pre><code>$(document).ready(function(){ // Binding event listeners for the form on document ready $('#email').defaultText('Your Email Address'); // 'working' prevents multiple submissions var working = false; $('#form').submit(function(){ if(working){ return false; } working = true; $.post("./index.php",{email:$('#email').val()},function(r){ if(r.error){ $('#email').val(r.error); } else { $('#email').val(r.msg); } working = false; },'json'); return false; }); }); // A custom jQuery method for placeholder text: $.fn.defaultText = function(value){ var element = this.eq(0); element.data('defaultText',value); element.focus(function(){ if(element.val() == value){ element.val('').removeClass('defaultText'); } }).blur(function(){ if(element.val() == '' || element.val() == value){ element.addClass('defaultText').val(value); } }); return element.blur(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload