简体   繁体   English

在example.php中获取Ajax发送的数据错误undefine index

[英]fetch Ajax sent data in example.php error undefine index

I have a ajax code to sent form data ajax code is working fine but I am receiving error in php page where ajax request is sent I have also used var_dump($_POST) and in response its showing all array values and its perfect but when I declare variable and try to get post like $var=$_POST['something'] it says undefined index for all please some one sort out this problem my code is below我有一个 ajax 代码来发送表单数据 ajax 代码工作正常,但我在发送 ajax 请求的 php 页面中收到错误我也使用了var_dump($_POST)并作为响应它显示了所有数组值及其完美但是当我声明变量并尝试获取类似$var=$_POST['something']帖子,它说所有undefined索引请有人解决这个问题,我的代码如下

ajax code:阿贾克斯代码:

function register()
{
  jQuery('.help-block').html('');
  var register_for=jQuery("#register_for").val();
  var departure=jQuery("#departure").val();
  var custom_departure_date=jQuery("#custom_departure_date").val();
  var name=jQuery("#name").val();
  var email=jQuery("#email").val();
  var contact=jQuery("#contact").val();
  var passport=jQuery("#passport").val();
  var error='';

   if (register_for=="") {
    jQuery('#register_for_error').html('This Field Must Not Be Empty');
    error='yes';
  }
   if (departure=="") {
    jQuery('#departure_error').html('Please Select  Departure');
    error='yes';
  }

 
   if (name=="") {
    jQuery('#name_error').html('Please Enter Your Name');
    error='yes';
  }

  if (email=="") {
    jQuery('#email_error').html('Please Enter Your Email');
    error='yes';
  }
if (contact=="") {

  jQuery('#contact_error').html('Please Enter Your Contact');
  error='yes';

}


  if(error==''){
        jQuery.ajax({
        url:'register_process.php',
         type:'post',
          data:'register_for=' + register_for + '&departure=' + departure + '&custom_departure_date=' + custom_departure_date + '&name='+ name + '&email=' + email + '&contact=' + contact +  '&passport=' + passport,
          success:function(result){
         
          jQuery('#registered_msg').html('Registration Successfull');
          jQuery('#registered_msg2').html('We will Contact You Soon For Confirmation');

         
       }


    });
      }
     
    }

url page:网址页面:

<?php
include ("includes/conn.php");
?>

<?php

var_dump($_POST);

    $register_for =$_POST['register_for'];
     $departure =$_POST['departure'];
     $custom_departure_date=$_POST['custom_departure_date'];
    $name = $_POST['name'];
    $email = $_POST['email'];
     $contact =$_POST['contact'];
     $passport =$_POST['passport'];
    $added_on=date('Y-m-d h:i:s');


$added_on=date('Y-m-d h:i:s');
$sql="insert into register(register_for,departure,departure_custom_date,name,email,contact,passport,date) values('$register_for','$departure','$custom_departure_date','$name','$email','$contact','$passport','$added_on')";
$query=mysqli_query($conn,$sql);
?>

register_process.php when opened this page from inspect /network/response从检查/网络/响应打开此页面时的 register_process.php

array(0) { }
Notice: Undefined index: register_for in C:\xampp\htdocs\project_adventure\register_process.php on line 9

Notice: Undefined index: departure in C:\xampp\htdocs\project_adventure\register_process.php on line 10

Notice: Undefined index: custom_departure_date in C:\xampp\htdocs\project_adventure\register_process.php on line 11

Notice: Undefined index: name in C:\xampp\htdocs\project_adventure\register_process.php on line 12

Notice: Undefined index: email in C:\xampp\htdocs\project_adventure\register_process.php on line 13

Notice: Undefined index: contact in C:\xampp\htdocs\project_adventure\register_process.php on line 14

Notice: Undefined index: passport in C:\xampp\htdocs\project_adventure\register_process.php on line 15```

when all S_POST variables are removed keeping only var_dump($_POST) network/response当删除所有S_POST变量时,只保留var_dump($_POST)网络/响应

array(7) {
  ["register_for"]=>
  string(10) "10/19/2020"
  ["departure"]=>
  string(10) "departure3"
  ["custom_departure_date"]=>
  string(0) ""
  ["name"]=>
  string(4) "jklj"
  ["email"]=>
  string(7) "ljkljlk"
  ["contact"]=>
  string(7) "jjkljlk"
  ["passport"]=>
  string(7) "jkljklj"
}

I don't understand why you sent variables like that but this should help you out.我不明白你为什么发送这样的变量,但这应该可以帮助你。

jQuery.ajax({
url:'register_process.php',
 type:'post',
  data:{
    register_for: register_for,
    email: email,
    departure: departure,
    custom_departure_date:custom_departure_date,
    contact:contact,
    passport:passport,
    name:name
  },
  success:function(result){
 
    jQuery('#registered_msg').html('Registration Successfull');
    jQuery('#registered_msg2').html('We will Contact You Soon For Confirmation'); 
  }

Read this for more information: http://api.jquery.com/jQuery.ajax/阅读更多信息: http : //api.jquery.com/jQuery.ajax/

So if that doesn't work, I would alert those variables to make sure they have values因此,如果这不起作用,我会提醒这些变量以确保它们具有值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM