weixin_33724059 2014-04-14 16:13 采纳率: 0%
浏览 22

AJAX加载和PHP错误

I have created a simple form which submits data to the database.

I want to use Ajax so that when submit is pressed, a loading bar shows and then takes you to the success! page after a few seconds. How do i go about this?

Also the following code gives me the following error, what do i need to fix - SOLVED:

Notice: Undefined index: command in F:\xamppnew\htdocs\web\billing.php on line 5

Code:

    <?php
include "storescripts/connect_to_mysql.php"; 
session_start(); 

if ($_REQUEST['command']=='update'){
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $address=$_REQUEST['address'];
        $phone=$_REQUEST['phone'];
        $item_id = $_SESSION['item_id'];
        $quantityorder = $each_item['quantity'] = $_SESSION['quantity1'];
        $cartTotal = $_SESSION['cartTotal'];

    // Add this product into the database now
    $sql = mysqli_query($link,"insert into transactions values('','$item_id','$quantityorder','$cartTotal','$name','$address','$email','$phone')");

        die('Thank You! your order has been placed!');
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
<title>Billing Info</title>
<script language="javascript">
function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
</script>
<script language="javascript">
    function validate(){
        var f=document.form1;
        if(f.name.value=='' || f.email.value=='' || f.address.value=='' || f.phone.value=='' ){
            alert('You have not filled in all fields');
            f.name.focus();
            return false;
        }
        f.command.value='update';
        f.submit();
    }
</script>
</head>



<body>
<div align="center" id="mainWrapper">
  <?php include_once("template_header.php");?>
  <div id="pageContent">
    <div style="margin:24px; text-align:left;">

    <br />
<form name="form1" onsubmit="return validate()">
    <input type="hidden" name="command" />
    <div align="center">
        <h1 align="center">Billing Info</h1>
        <table border="0" cellpadding="2px">
             <tr><td>Product ID:</td><td><?php echo $item_id = $_SESSION['item_id'];?></td></tr>
            <tr><td>Total Quantity:</td><td><?php echo $each_item['quantity'] = $_SESSION['quantity1']; ?></td></tr>
            <tr><td>Order Total:</td><td>£<?php echo $cartTotal = $_SESSION['cartTotal'];?></td></tr>
            <tr><td>Your Name:</td><td><input type="text" name="name" /></td></tr>
            <tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
            <tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
            <tr><td>Phone:</td><td><input type="text" name="phone" onkeypress='return isNumberKey(event)' /></td></tr>
            <tr><td>&nbsp;</td><td><input type="submit" value="Place Order" /></td></tr>
        </table>
    </div>
    </div>


      <?php include_once("template_footer.php");?>

</form>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • 衫裤跑路 2014-04-15 13:50
    关注
    <!--================================== code for "stack_ajax.php" start here ==================================-->
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="css/external_css.css">
    <script type="text/javascript" src="jquery_src/jquery-1.11.0.min.js"></script>
    <script type="text/javascript">
      function validate(){
    
        var f=document.form1;
        if(f.name.value=='' || f.email.value=='' || f.address.value=='' || f.phone.value=='' ){
            alert('You have not filled in all fields');
            f.name.focus();
            return false;
        }
    
         f.command.value='update';
    
         postData = $("#form1").serializeArray( );
    
         $.ajax({
           type       : "POST",
           data       : postData,
           dataType   : "html",
           url        : "ajax_response.php",
           async      : true,
           beforeSend : function( ) {
                          $("#ajax_image_div").show();
                        },
           success    : function(data, textStatus, xhr) {
                         ajax_response_status = parseInt(data, 10);
    
                         if(ajax_response_status)
                          window.location.href='success_message.php';
                           else
                            window.location.href='failed_message.php';
                        },
           error      : function(xhr, textStatus, errorThrown) {
                         alert(textStatus);
                        },
           complete   : function(jqXHR, textStatus) {
                         $("#ajax_image_div").hide();
                        }
          });
    
         return true;
      }
    
      function isNumberKey(evt){
       var charCode = (evt.which) ? evt.which : evt.keyCode
       if (charCode > 31 && (charCode < 48 || charCode > 57))
           return false;
    
       return true;
      }
    </script>
    <link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
    
    
    
    </head>
    
    <title>Billing Info</title>
    
    <body>
    
    <div id='ajax_image_div' style="display:none;">
      <img id="ajax_loader_image" style="vertical-align:middle" src='image/ajax_loader.gif'>
    </div>
    <div align="center" id="mainWrapper">
      <div id="pageContent">
        <div style="margin:24px; text-align:left;">
          <br/>
          <div align="center">
            <form id="form1" name="form1" method="POST" action="" autocomplete="off" enctype="multipart/form-data">
             <input type="hidden" name="command" /> 
              <h1 align="center">Billing Info</h1>
              <table border="0" cellpadding="2px">
                 <tr><td>Your Name:</td><td><input type="text" name="name" id="user_name" /></td></tr>
                 <tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
                 <tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
                 <tr><td>Phone:</td><td><input type="text" name="phone" onkeypress='return isNumberKey(event)' /></td></tr>
                 <tr><td>&nbsp;</td><td><input type="button" name="submit" value="Place Order" onClick="return validate()"/></td></tr>
              </table>
            </form>
          </div>
        </div>
      </div>
    </div>
    
    
    
    </body>
    </html>
    <!--================================== code for "stack_ajax.php" end here ==================================-->
    
    
    
    <!--============================== code for "ajax_response.php" start here ==============================-->
     <?php
      /*
       Note : I made changes in your HTML form ( You can add session variable by yourself )  and delete following lines
         1. <?php include_once("template_header.php");?>
         2. <?php include_once("template_footer.php");?>
      */
    
      sleep(3); /* Wait for 3 seconds before executing below code, by doing so
                   you will be able to see "AJAX LOADER" on the page.
                */
    
    /* print_r($_POST); // You will get all the FORM input data in this array. */
    
      $name          = $_REQUEST['name'];
      $email         = $_REQUEST['email'];
      $address       = $_REQUEST['address'];
      $phone         = $_REQUEST['phone'];
      $item_id       = $_SESSION['item_id'];
      $quantityorder = $each_item['quantity'] = $_SESSION['quantity1'];
      $cartTotal     = $_SESSION['cartTotal'];
    
       // Add this product into the database now
       $sql = mysqli_query(
                           $link,
                                 "insert 
                                  into
                                     transactions
    
                                  values
                                     ('','$item_id','$quantityorder','$cartTotal','$name','$address','$email','$phone')"
                          );
        if( $sql )
         echo 1; /* When SQL query will execute successfully. */
           else
            echo 0; /* When SQL query will not execute successfully. */
        exit;
    ?>
    <!--============================== code for "ajax_response.php" end here ==============================-->
    
    
    
    /* ================================== code for "external_css.css" start here ==================================*/
    #ajax_image_div {
    top:30%;
    left:30%;
    width:50%;
    height:50%;
    position:absolute;
    z-index:999;
    }
    
    #mainWrapper {
    width:100%;
    height:100%;
    position:absolute;
    }
    
    #ajax_loader_image {
     position:relative;
     top:35%;
    left:35%;
    }
    
    /* ================================== code for "external_css.css" end here ==================================*/
    
    
    
    <!-- ============================= code for "success_message.php" start here ============================= -->
    <?php
     echo 'Thank You! your order has been placed!';
    ?>
    <!-- ============================= code for "success_message.php" end here ============================= -->
    
    
    
    <!-- ============================= code for "failed_message.php" start here ============================= -->
    <?php
     echo 'An error occurred, please try after some time ';
    ?>
    <!-- ============================= code for "failed_message.php" end here ============================= -->
    
    
    <!--
    Note : make sure your directory structure must be same as below
    
    php_ajax ( create a folder with "php_ajax" and put all the code inside it )
     1. css [ create a folder with "css" name ]
        A. external_css.css
     2. image [create a folder with "image" name]
        A. ajax_loader.gif [ You may download it using internet :-) ]
     3. jquery_src [ [create a folder with "jquery_src" name]]
        A. jquery-1.11.0.min.js [ jQuery framework, you may download it using internet ]
     4. ajax_response.php
     5. failed_message.php
     6. stack_ajax.php
     7. success_message.php
    
     Let me know if you have any problem after implementing this. I put all the codes in separate files to
     make DEBUG task very easy.
    -->
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本