dtrb96410 2017-04-30 23:53
浏览 30
已采纳

从PHP返回字符串到Ajax

I have an Ajax script that relays form information to PHP upon user submission. The data is inserted into the table and then returns a string back to Ajax. For some reason, instead of getting the string I am getting the full HTML code of the page. How do I go about getting the string instead of the HTML code?

HTML

  <form id='orderForm' action='' method='post'>

                        <h2>Choose your pizza size</h2><hr>
                            <select name='size'>
                            <option value='' disabled selected>Choose a size</option>
                            <option value='small'>Small</option>
                            <option value='medium'>Medium</option>
                            <option value='large'>Large</option></select>

                            <h2>Choose your toppings</h2><hr><label><input type='checkbox' name='check_list[]' value='beef'>Beef</label>
                            <label><input type='checkbox' name='check_list[]' value='pepperoni'>Pepperoni</label>
                            <label><input type='checkbox' name='check_list[]' value='chicken'>Chicken</label>
                            <label><input type='checkbox' name='check_list[]' value='sausage'>Sausage</label>


                 <h2>Enter your details</h2><hr><input type='text' name='name' placeholder='Full Name'>
                        <input type='email' name='email' placeholder='Email'>
                        <input type='text' name='phone' placeholder='Phone number'>


                    <input type='text' name='address' placeholder='Address'>
                        <input id='zip' type='text' name='zip' placeholder='Zip Code'>
                        <p id='message'></p>



                      <input id='submitBtn' type='submit' name='submitBtn' value='Place Order'>
                        </form>

AJAX

$.ajax({
             type: 'post',
             url: 'validation.php',
             data: $("#orderForm").serialize(),
             datatype: "html",
             success: function(data){
                  window.location = data;
             }
       })

PHP

if (isset($_POST["email"])){

$orderId = time() + mt_rand(1,10);

$toppings = implode(', ', $_POST['check_list']);
$result = $db->prepare("INSERT INTO orders (order_id, type, pizza_type, size, toppings, name, address, email, number) VALUES (?, ?,?,?,?,?,?,?,?)");

$result->bind_param("sssssssss", $orderId, $_GET['type'], $_GET['order'], $_POST['size'], $toppings, $_POST['name'], $_POST['address'], $_POST['email'], $_POST['phone']);

$result->execute();
$db->close();

echo "?success=true&orderid=' . $orderId . '&toppings=' . $toppings";
}
  • 写回答

2条回答 默认 最新

  • doubeiji2602 2017-05-01 01:37
    关注

    Hi I don't how you get $_GET['type'] and $_GET['order']

    if (isset($_POST["email"])){
    
        $orderId = time() + mt_rand(1,10);
    
        $toppings = trim(implode(',', $_POST['check_list']));
        $type = $_POST['type'];
        $order = $_POST['order'];
        $result = $db->prepare("INSERT INTO orders (order_id, type, pizza_type, size, toppings, name, address, email, number) VALUES (?, ?,?,?,?,?,?,?,?)");
    
        $result->bind_param("issssssss", $orderId, $type, $order, $_POST['size'], $toppings, $_POST['name'], $_POST['address'], $_POST['email'], $_POST['phone']);
        try {
            $result->execute();
        }catch (Exception $e) {
            echo $e->getMessage();
        }
        $db->close();
        echo "?success=true&orderid=" . $orderId . "&toppings=" . $toppings;
    }
    

    form page

    <head>
        <link href='index.css?<?php echo time(); ?>' rel='stylesheet' type='text/css'>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    </head>
    <body>
    <div class='container'>
        <div class='startContainer'>
            <?php
            if (!isset($_GET['type'])) {
                if (!isset($_GET['success'])) {
                    echo "<img src='order_bg'>
                            <a href='?type=delivery'><div class='orderOptions'>
                                <i class='material-icons'>directions_car</i>                            
                                Delivery
                            </div></a>
    
                            <a href='?type=carryout'><div class='orderOptions'>
                                <i class='material-icons''>store</i>
                                Carry out
                            </div></a>
                        </div>";
                } else {
                    echo "<h1>Success!</h1><p>Your order id is: " . $_GET['orderid'] . "</p><br>
                        <p>Toppings: " . $_GET['toppings'] . "</p>
                        <h1>Your pizza will be ready in:</h1>
                        <h2 id='counter' style='text-align: center'></h2>";
                }
            } else {
                if (!isset($_GET['order'])) {
                    echo "<img src='make_pizza'> &nbsp<h2>Choose your pizza.</h1>
                        <a href='?type=$_GET[type]&order=custom'><div class='pizzaType'>
                            <img src='custom_pizza'>
                            <i class='material-icons'>apps</i>
                            Custom made
                        </div></a>
                        <a href='?type=$_GET[type]&order=grandma'><div class='pizzaType'>
                            <img src='grandma_pizza'>
                            <i class='material-icons'>local_pizza</i>
                            Grandma's Pizza
                        </div></a>";
                } else {
                    echo "<img src='order_pizza'>
    
                        <form id='orderForm' action='' method='post'>";
    
                    if ($_GET['order'] == 'custom') {
                        echo "<h2>Choose your pizza size</h2><hr>
                                <select name='size'>
                                <option value='' disabled selected>Choose a size</option>
                                <option value='small'>Small</option>
                                <option value='medium'>Medium</option>
                                <option value='large'>Large</option></select>
    
                                <h2>Choose your toppings</h2><hr><label><input type='checkbox' name='check_list[]' value='beef'>Beef</label>
                                <label><input type='checkbox' name='check_list[]' value='pepperoni'>Pepperoni</label>
                                <label><input type='checkbox' name='check_list[]' value='chicken'>Chicken</label>
                                <label><input type='checkbox' name='check_list[]' value='sausage'>Sausage</label>";
                    }
    
                    echo "<h2>Enter your details</h2><hr><input type='text' name='name' placeholder='Full Name'>
                            <input type='email' name='email' placeholder='Email'>
                            <input type='text' name='phone' placeholder='Phone number'>
                            <input name='type' hidden value='" . $_GET['type'] . "' type='text'> 
    <input type='text' name='order' hidden value='" . $_GET['order'] . "'>";
    
                    if ($_GET['type'] == 'delivery') {
                        echo "<input type='text' name='address' placeholder='Address'>
                            <input id='zip' type='text' name='zip' placeholder='Zip Code'>
                            <p id='message'></p>";
                    }
    
    
                    echo "<input id='submitBtn' type='submit' name='submitBtn' value='Place Order'>
                            </form>";
                }
            }
            ?>
        </div>
    </div>
    <script>
        var count = 60,
            timer = setInterval(function () {
                $("#counter").html(count-- + " seconds");
                if (count == 0) {
                    $("#counter").html("Order complete!");
                    clearInterval(timer);
                }
            }, 1000);
    
        var zipCodes = ["30060", "30069", "30090", "30065", "30063", "30061", "30006", "30007", "30008", "30081", "30067", "30064", "30082", '30080', "30339", "30068", "30062", "30066", "30152", "30126", "30160", "30156", "30327", "30144", "30106", "30328", "30111", "30127", "30342"];
    
        $("#zip").on("focus", function () {
            $("#submitBtn").prop("disabled", false);
            $("#submitBtn").removeAttr("style");
            $("#message").text("");
        })
    
        $('#orderForm').submit(function (e) {
            e.preventDefault();
            e.stopImmediatePropagation();
    
            var zip = $("#zip").val();
            if (zipCodes.indexOf(zip) == -1) {
                $("#submitBtn").prop("disabled", true);
                $("#submitBtn").css("background-color", "gray");
                $("#message").text("We're sorry, but we do not deliver to the following zip code.");
            }
            else {
                $.ajax({
                    type: 'post',
                    url: 'file.php', // change this to php file.
                    dataType: "text",
                    data: $("#orderForm").serialize(),
                    success: function (data) {
                       window.location = data;
                    }
                })
            }
        })
    
    </script>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线