Reputation: 85
I take a form and do variable checking in jquery then pass it to a php file in ajax but I am getting this notice
Notice: Undefined index: your_name in C:\xampp\htdocs\process.php on line 3 something is wrong here Notice: Undefined index: your_email in C:\xampp\htdocs\process.php on line 7
Here is my jquery code here
$(".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");
},
and here is my 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/>";
}
I am not sure why I am getting this notice
Apparently my $_POST values are not set.
I am at my wits end with this one. Do you know why/when a $_POST is not set?
Upvotes: 0
Views: 6886
Reputation: 118
$_POST['your_name'] cannot be assigned to the variable $your_name when the "your_name" isn't posted cause it doesn't exist .. you have to check if it exists first and then assign it to the variable .. the code should be like the following :
if (isset($_POST['your_name'])) {
$your_name = $_POST['your_name'];
}
else {
echo "something is wrong here";
}
Upvotes: 1
Reputation: 516
not seen any issue in your js code but your php code have lil flaw
<?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"];
}
Note: PHP Error handling should be properly done or you can disable the php warnings
Upvotes: 0
Reputation: 35146
You are getting value of your_name first and then checking if it exists
$your_name=$_POST["your_name"]; <--- this line should be inside an if isset
if(!isset($_POST['your_name'])){
Do something like the following
if (isset($_POST['your_name'])) {
$your_name = $_POST['your_name'];
// do whatever here
}
Upvotes: 2