我在使用php post时出现了一个错误,如何解决?

问题描述 投票:0回答:1

<?php
    header("Cache-Control: no-cache, no-store, must-revalidate");
    header("Access-Control-Allow-Origin: *");

    include 'classMethods.php';
    try{
        $cu="cuzito";
    $method=new Methods(); 
    if(isset($_POST['image']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])){
        $cu="test , what have inside ->".$_POST['name'];
      if($method->SaveImageAndURL($_POST['image'])){
        $cu="worked";
      }else{
          $cu="no work";
      }
    }else{
        $cu="empty variables";
    }}catch(Exption $e){
        $cu= "error:". $error->getCode()." | mensage :". $error->getMessage();
    }
    echo $cu;
?>

当我运行它时,我得到了未定义的索引,我已经试过if(isset($_POST['image'])){}返回给我false,但if(isset($_POST)){}返回true,我使用ajax。


 Register(name:String, email:String,password:String,img:any){
    try{
    $.ajax({
      url:'http://localhost:80/home.php',
      method:'post',
      data:{
        name:name,
        email:email,
        password:password,
        image:img,
      },
      processData: false,
    contentType: false,
      beforeSend: function(){ 
        console.log("data sended, wainting for answser");

      }
    }).done(function(e){  
     console.log(e+"<- answer's php");
     if(e===true){
       alert("Register was a sucess"); 
      this.router.navigate(['/login']); //redirect  the user to the login page
     }
    }).fail(function(jqXHR, textStatus, msg){
      console.log(msg+" <- fail ajax");
      alert("Register was a fail , try again");
 })
  }catch(y){
    console.log(y+" <-have fail , error");
  }
  }

obs:我在用angular 9做一个移动应用,或者尝试做kkkkw,我的代码有什么问题?你能帮帮我吗?

php ajax post
1个回答
0
投票

问题是你试图在 else 语法上访问未定义的变量,所以可以试试这段代码修复(看我改的 else 条件,检查那个 POST var 是否被定义) 。

<?php

header("Cache-Control: no-cache, no-store, must-revalidate");
header("Access-Control-Allow-Origin: *");

include 'classMethods.php';

try{
    $cu="cuzito";
    $method=new Methods(); 
    if (isset($_POST['image']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
        $cu="tamo ai ->".$_POST['name'];
          if($method->SaveImageAndURL($_POST['image'])) {
            $cu="foi";
          } else {
              $cu="não foi";
          }
    } else {
        $cu="bagulho ta vazio  name{" . (isset($_POST['name']) ? $_POST['name'] : 'is empty' )  ."} | email{". (isset($_POST['email']) ? $_POST['email'] : 'is empty' ) ."} | password {". (isset($_POST['password']) ? $_POST['password'] : 'is empty' ) ."}";//resposta caso a requisição esteja vazia
    }

}catch(Exption $e){
    $cu= "error:". $error->getCode()." | mensage :". $error->getMessage();
}
echo $cu; ?>
© www.soinside.com 2019 - 2024. All rights reserved.