[英]Undefined index in PHP post
我采取一種形式,並在jquery中進行變量檢查,然后將其傳遞給ajax中的php文件,但我收到此通知
注意:未定義的索引:第3行的C:\\ xampp \\ htdocs \\ process.php中的your_name這里是錯誤的注意:未定義的索引:第7行的C:\\ xampp \\ htdocs \\ process.php中的your_email
這是我的jQuery代碼在這里
$(".button").click(function(){
$('.error').hide();
var your_email=$("input#your_email").val();
if(your_email ==""){
$("label#youremail_error").show();
$("input#your_email").focus();
return false;
}
var your_name=$("input#your_name").val();
if(your_name==""){
$("label#yourname_error").show();
$("input#your_name").focus();
return false;
}
var friends_email=$("input#friends_email").val();
if(friends_email==""){
$("label#friendsemail_error").show();
$("input#friends_email").focus();
return false;
}
var friends_name=$("input#friends_name").val();
if(friends_email==""){
$("label#friendsname_error").show();
$("input#friends_name").focus();
return false;
}
var dataString = 'your_email=' + your_email + '&friends_email=' + friends_email + '&your_name=' + your_name + '&friends_name=' + friends_name;
//alert(dataString);
$.ajax({
type: "POST",
url:"process.php",
data: dataString,
success: function(ret) {
alert(ret);
//alert("thank you for signing up");
},
這是我的PHP
<?php
include 'inc/class.phpmailer.php';
if(isset($_POST['your_name'])){
$your_name=$_POST['your_name'];
}
else{
echo "something is wrong with your name having:";
var_dump($_POST['your_name']);
echo "<br/>";
}
if(isset($_POST['your_email'])){
$your_email=$_POST['your_email'];
}
else{
echo "something is wrong with your email having:";
var_dump($_POST['your_email']);
echo "<br/>";
}
if(isset($_POST['friends_name'])){
$friends_name=$_POST['friends_name'];
}
else{
echo "something is wrong with friends name having:";
var_dump($_POST['friends_name']);
echo "<br/>";
}
我不確定為什么會收到此通知,顯然我的$ _POST值未設置。
我對此一無所知。 您知道為什么/何時未設置$ _POST嗎?
首先要獲取your_name的值,然后檢查它是否存在
$your_name=$_POST["your_name"]; <--- this line should be inside an if isset
if(!isset($_POST['your_name'])){
做類似以下的事情
if (isset($_POST['your_name'])) {
$your_name = $_POST['your_name'];
// do whatever here
}
$ _POST ['your_name']不能分配給變量$ your_name時,因為它不存在,所以沒有發布.y,您必須先檢查它是否存在,然后再將其分配給變量.. the代碼應如下所示:
if (isset($_POST['your_name'])) {
$your_name = $_POST['your_name'];
}
else {
echo "something is wrong here";
}
在您的js代碼中未看到任何問題,但您的php代碼有lil缺陷
<?php
include 'inc/class.phpmailer.php';
//it can be like that or
if(!isset($_POST['your_name'])){
echo "something is wrong here";
}
else{
$your_name=$_POST["your_name"];
}
注意:應該正確完成PHP錯誤處理,或者您可以禁用php警告
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.