开发者

$_GET and $_POST issue

The following is my problem (example code)..

If I perform this:

$rf = $_GET['_id'];

if($_POST['form']){
echo "ref: " . $rf;
}

$rf is blank during if($_POST['form'])

Now, If I perform this:

$rf = "123456";

if($_POST['form']){
echo "ref: " . $rf;
}

$rf is displayed

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?

Thanks!

Note: don't worry about SQL injections.. I have already stripped everything from "_id".

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'])){


    // 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.my-url/" . $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;">&nbsp;</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)开发者_JS百科{
                $('#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();
}


As you're probobly sending form by POST request you should try to get _id by $_POST['_id'], however $_REQUEST[] array may by of more use to you.

if($_POST['form']){
    echo "ref: " . $_POST['_id'];
}


Chances are that $_GET is not been set, you mention that it is set by the .htaccess file from the URL are you sure that this is working?

try:

var_dump($_GET);die;

and see what happens.


$_GET['_id'] is apparently empty in your code.

you are loading no example.com/5ABH67L url.
you are loading /index.php as it's clearly seen from script.js.

you have to either

  • add current url to POST data using javascript.
  • or make POST action not index.php but current location.

I am not a JS pro, but try this:

$.post("",{email:$('#email').val()},function(r)


Just because $_POST['form'] evaluates to true that doesn't mean that $_GET['_id'] is set as well.

In general you should always check, if those variables are set, before you access their content. You can do that by checking isset($_GET['_id']). So you could replace your if like this:

if ( isset($_POST['form']) && isset($_GET['_id']) )
{
    $rf = $_GET['_id'];
    echo "ref: " . $rf;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜