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

报告相同问题?

悬赏问题

  • ¥15 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的