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

$ _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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵