dsnhalq37505 2014-11-05 19:31
浏览 76
已采纳

$ _POST数组为空,我得到了未定义的变量错误

Sorry to ask it again, but I checked almost all the q&a's on the topic and still couldn't solve it. Here is my form.php and creditform.php

Edit1.Changed the .get to .post

Edit2.I'm getting errors of undefined variable "name" "email" "address" "income" etc (in short for all of them) in creditform.php What I want to do is just insert all the inputs into the table in db.

HTML

<!-- End crumbs-->
            <div class="container wrap wow fadeInUp">
            <div class="row">
            <div class="col-sm-5 col-md-6 left-app">
            <form id="form" action="php/creditform.php" method="post">
                <input type="text" placeholder="Name" name="name" required>
                <input type="email" placeholder="Email"  name="email" required>
                <input type="text" placeholder="Address"  name="address" required>
                <input type="number" placeholder="Monthly income before taxes"  name="income" required>
                <input type="number" placeholder="Amount Needed"  name="amount_needed" required>
                <input type="number" placeholder="Phone"  name="phone" required>
             <div class="row">
             <div class="container">
                <input type="submit" name="submit" value="Submit" class="button"></div></div>
             <div id="result"></div>  
            </form> 
            </div>
                <script>
                $(document).ready(function($) {
                   'use strict';

                $('#form').submit(function(event) {  
                   event.preventDefault();  
                   var url = $(this).attr('action');  
                   var datos = $(this).serialize();  
                   $.post(url, datos, function(resultado) {  
                   $('#result').html(resultado);  
                   });  
               });
                </script>

FORM.PHP

<?php

include('db.config.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   $name = $email = $address = $income = $amount_needed = $phone = '';

if(isset($_POST['submit'])){

   $name = addslashes($_POST['name']);
   $email = addslashes($_POST['email']);
   $address = addslashes($_POST['address']);
   $income = addslashes($_POST['income']);
   $amount_needed = addslashes($_POST['amount_needed']);
   $phone = addslashes($_POST['phone']);

// check form fields
if(empty($name)){
    $error .= 'Enter name <br />';
}
if(empty($email)){
    $error .= 'Enter email <br />';
}
// check if errors exist
if(!empty($error)){
    echo $error;    
} else {
    // process form as normal   

    $sql = "INSERT INTO `" . DBN . "`.`creditapp` (`name`, `email`, `address`, `income`, `amount_needed`, `phone`) VALUES ('$name', '$email', '$address', '$income', '$amount_needed', '$phone')";
    $db->Query($sql);
}
}
} 
print_r($_POST);

?>

CREDITFORM.PHP

<?php
if(isset($_POST['submit'])){

   $name = $_POST['name'];
   $email = $_POST['email'];
   $address = $_POST['address'];
   $income = $_POST['income'];
   $amount_needed = $_POST['amount_needed'];
   $phone = $_POST['phone'];
}
print_r($_POST);


?>

It's obvious that I'm missing something, please correct me... Thanks for your time

  • 写回答

4条回答 默认 最新

  • doujiao9426 2014-11-07 20:17
    关注

    Okay, I practiced more on ajax - json - php and in the end this was my solution and my latest code.

    Thanks for all answers and suggestions. Hope this also helps to someone.

    Still open to any kind of advice to improve.

    <?php
    
    include('db.config.php');
    
    if(isset($_POST['submit'])){
    
    
    $name = addslashes($_POST['name']);
    $email = addslashes($_POST['email']);
    $address = addslashes($_POST['address']);
    $income = addslashes($_POST['income']);
    $amount_needed = addslashes($_POST['amount_needed']);
    $phone = addslashes($_POST['phone']);
    
    // process form as normal   
    
    $sql = "INSERT INTO `" . DBN . "`.`creditapp` (`name`, `email`, `address`, `income`, `amount_needed`, `phone`) VALUES ('$name', '$email', '$address', '$income', '$amount_needed', '$phone')";
    $db->Query($sql);
    
    die();
    
    <div class="container wrap wow fadeInUp">
         <div class="row">
         <div class="col-sm-5 col-md-6 left-app">
            <form id="form" action="creditapp.php" method="post">
              <input type="text" placeholder="Name" name="name" id="name" required>
              <input type="email" placeholder="Email"  name="email" id="email" required>
              <input type="text" placeholder="Address"  name="address" id="address" required>
              <input type="text" placeholder="Monthly income before taxes" id="income" name="income" required>
              <input type="text" placeholder="Amount Needed"  name="amount_needed" id="amount_needed" required>
              <input type="text" placeholder="Phone"  name="phone" id="phone" required>
            <div class="row">
            <div class="container">
              <input type="submit" name="submit" value="Submit" class="button sub"></div>
            </div>
            </form> 
            </div>
    
       <script>
       $(document).ready(function() {
    
       $('.sub').click(function(e){
           e.preventDefault();
            var name = $('#name').val(),
                email = $('#email').val(),
                address = $('#address').val(),
                income = $('#income').val(),
                amount_needed = $('#amount_needed').val(),
                phone = $('#phone').val();
    
    
            $.ajax({
                url: "creditapp.php",
                type: "POST",
                data: {
                    name: name,
                    email: email,
                    address: address,
                    income: income,
                    amount_needed: amount_needed,
                    phone: phone,
                    submit: "submit"
                }
    
                }).done(function(msg){
                  $('.right-info').html('<pre>' + msg + '</pre>');
                })
           })
       });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入
  • ¥15 关于液体厚度提取,和铺展轨迹数据化处理
  • ¥20 对方微信是wxid如何添加成好友?或者转换成微信号?
  • ¥15 yolov5实验数据好,但是自己拍摄的照片检测结果差
  • ¥15 做过dali2.0认证的技术,
  • ¥15 一道python难题
  • ¥15 matlab 免疫算法
  • ¥50 可视化界面构建,实现信息模块功能
  • ¥15 基于bert-base-chinese的中文文本处理
  • ¥15 高价求抖音抓包技术,抖音抓包无网络问题