以下是我的问题(示例代码)..
如果我执行此操作:
$rf = $_GET['_id'];
if($_POST['form']){
echo "ref: " . $rf;
}
在$rf
期间, if($_POST['form'])
为空
现在,如果我执行此操作:
$rf = "123456";
if($_POST['form']){
echo "ref: " . $rf;
}
显示 $rf
为什么第一次是空白而不是空白如果我给$ rf var分配一些静态的东西? 另外,我如何解决这个问题,以便第一个解决方案有效?
谢谢!
注意:不要担心SQL注入..我已经从“_id”中剥离了所有内容。
完整的PHP和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;"> </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);
}
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();
}
答案 0 :(得分:1)
由于您可能是通过POST请求发送表单,因此您应该尝试通过$ _POST ['_ id']获取_id,但$_REQUEST[] array可能会对您有用。
if($_POST['form']){
echo "ref: " . $_POST['_id'];
}
答案 1 :(得分:1)
可能没有设置$ _GET,你提到它是由URL中的.htaccess文件设置的,你确定这个有效吗?
尝试:
var_dump($_GET);die;
看看会发生什么。
答案 2 :(得分:0)
仅仅因为$_POST['form']
评估为true
并不意味着$_GET['_id']
也已设置。
通常,在访问其内容之前,应始终检查是否已设置这些变量。您可以通过选中isset($_GET['_id'])
来完成此操作。所以你可以像这样替换if
:
if ( isset($_POST['form']) && isset($_GET['_id']) )
{
$rf = $_GET['_id'];
echo "ref: " . $rf;
}
答案 3 :(得分:0)
$ _ GET ['_ id']在你的代码中显然是空的。
您没有加载example.com/5ABH67L
网址
您正在加载/index.php
,因为它可以从script.js
清楚地看到。
你必须
我不是JS专业人士,但试试这个:
$.post("",{email:$('#email').val()},function(r)