iCeR Posted March 20, 2011 Share Posted March 20, 2011 User clicks on a url, ie: example.com/AEQ438J 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['form']), however during the if($_POST['form']) $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 <?php require "includes/connect.php"; //var_dump($_GET);die; function gen_code($codeLen = 7) { $code = ''; for ($i=0; $i<$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->query("UPDATE coming_soon_emails SET code='" . $code ."' WHERE email_id='" . $email_id . "'"); if($mysqli->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->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'); try{ if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){ 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 = "http://www.example.com/" . $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 ."'"); } if($ajax){ die(json_encode(array('msg' => $msg))); } } catch (Exception $e){ if($ajax){ die(json_encode(array('error'=>$e->getMessage()))); } $msg = $e->getMessage(); } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link rel="stylesheet" type="text/css" href="css/styles.css" /> </head> <body> <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" /> </form> <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;"> </span></p> </div> </div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> <script src="js/script.js"></script> </body> </html> script.js $(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); // not needed but gets hidden anyways... $('#launch form').hide(); $("#code").html(r.msg); $("#invite").fadeIn('slow'); } 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(); } htaccess RewriteEngine on RewriteCond %{HTTP_HOST} ^my-url.com RewriteRule (.*) http://www.my-url.com/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-z0-9]+)$ /index.php?_url=$1 [NC,L,QSA] table.sql 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; Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/ Share on other sites More sharing options...
iCeR Posted March 20, 2011 Author Share Posted March 20, 2011 Still trying to work through this one anyone have a clue as to why it won't display the $_GET var during the $_POST? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189920 Share on other sites More sharing options...
KevinM1 Posted March 20, 2011 Share Posted March 20, 2011 When your page first loads, the GET value is set. When you attempt to send POST data, that's a new request, even if it's done asynchronously. At that point, $_GET may be empty, since nothing was set for it in the new request. Try modifying your form's action to re-send the query string (e.g., $_SERVER['SCRIPT_FILENAME'] . "?url=" . $_GET['url']) so the second stage of the process can have it as well. Not 100% sure on this, but it seems likely. Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189924 Share on other sites More sharing options...
iCeR Posted March 20, 2011 Author Share Posted March 20, 2011 Nightslyr, thank you for responding. Ok, so the update is this.. In JS if I comment out the ajax call of ,'json') and in index.php comment out the line $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); it then updates the DB correctly 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? Thank you again! Very, very much appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189937 Share on other sites More sharing options...
KevinM1 Posted March 20, 2011 Share Posted March 20, 2011 What are you supposed to be returning if the function works properly? I see that you return json_encoded error messages when an error/exception occurs, but am having some difficulty seeing what you're sending back when things are actually right. Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189942 Share on other sites More sharing options...
iCeR Posted March 20, 2011 Author Share Posted March 20, 2011 If the function works properly.. $('#launch form').hide(); $("#code").html(r.msg); $("#invite").fadeIn('slow'); If it doesn't work correctly, depending on if the email was invalid or it already exists, it will display in the text box and not submit. Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189945 Share on other sites More sharing options...
KevinM1 Posted March 20, 2011 Share Posted March 20, 2011 Ah, I meant from your PHP - where is your "Everything is A-OK" return value? Also, are you getting any JS errors when you uncomment the lines you commented out? I'm just wondering if 'r' is actually being sent back from the PHP when things are working. Like I said, I can see where the error condition messages are being encoded and sent back, but not the success condition message. Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189949 Share on other sites More sharing options...
iCeR Posted March 20, 2011 Author Share Posted March 20, 2011 Hmm I don't have anything being returned as a success.. as you can see from my above code.. This issue has been bugging me for 2 days and can't get my head around it to move on to the next possible issue. Any recommendations or tests I could run to get this working with ajax? Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189952 Share on other sites More sharing options...
KevinM1 Posted March 20, 2011 Share Posted March 20, 2011 Actually, I found where the 'good' response is being set. It's another die() function call (die(json_encode(array('msg' => $msg)))). Something else to test - have you checked whether or not $ajax is being set properly? That it's actually being set to true? Or, can you try actually returning both the error messages and 'good' message rather than relying on die()? Nothing really jumps out as me as a reason why the div isn't fading in. The only thing I can think of right now is that you're not getting a response back, so the function is failing. Either the Ajax isn't working because $ajax isn't actually true, so your entire function is being skipped over on the server side, or your PHP isn't returning the messages in a way jQuery/Ajax expects to receive them, and you're getting a silent error which is stopping you on the client side. Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189955 Share on other sites More sharing options...
iCeR Posted March 20, 2011 Author Share Posted March 20, 2011 Thanks again for your help Nightslyr. How can I return both error messages AND 'good' ones? Knew to this, sorry. Initially, I wasn't actually looking for why the div isn't fading in.. it does. The bit that doesn't work is; //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 ."'"); } When I comment out the, 'json') in JS and the line $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); in PHP, it updates the signup column (as per above code) brilliantly, however the fading div's in JS no longer work. It seems it's one or the other at the moment, however I want both to work Quote Link to comment https://forums.phpfreaks.com/topic/231159-one-drop-of-code-bugging-me-during-_post-using-_get-variable-please-help/#findComment-1189965 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.