使用PHP和AJAX请求表单

时间:2013-07-04 22:38:05

标签: php jquery ajax forms

我有以下简单形式:

enter image description here

以下是表单背后的代码:

<form action="javascript:void(0);" method="post">
<fieldset>
    <legend>ROOM EQUIPMENT</legend>

    <div class="inline_inputs">
        <div class="input_box">
            <input type="checkbox" name="equipment" value="computer" id="computer">
            <label for="computer">Computer</label>
        </div><!-- .input_box -->

        <div class="input_box">
            <input type="checkbox" name="equipment" value="projector" id="projector">
            <label for="projector">Projector</label>
        </div><!-- .input_box -->

        <div class="input_box">
            <input type="checkbox" name="equipment" value="whiteboard" id="whiteboard">
            <label for="whiteboard">Whiteboard</label>
        </div><!-- .input_box -->

        <div class="input_box">
            <input type="checkbox" name="equipment" value="visualiser" id="visualiser">
            <label for="visualiser">Visualiser</label>
        </div><!-- .input_box -->

        <div class="input_box">
            <input type="checkbox" name="equipment" value="desk" id="desk">
            <label for="desk">Desk</label>
        </div><!-- .input_box -->
    </div>
</fieldset>

<div class="buttons">
    <input type="submit" class="reg_button" value="GET ROOMS" />
</div><!-- .buttons -->

最后,我将如何在此表单所在的同一页面上发出AJAX请求:

<script>
$('form').submit(function(){
    var str = $(this).serialize();
    $.ajax({
      url: "userLogic.php",
      cache: false
    }).done(function( html ) {
      $("#rooms_wrap").append(html);
    });
});

我对PHP很新,我遇到表单提交问题。当我做出选择时,我的选择不会发送到userLogic.php文件。我得到了一份印刷品:

  

抱歉,您尚未做出选择。

这来自位于userLogic.php文件中的PHP代码:

<?php
include("connect.php");

$items = array_key_exists('equipment', $_POST) ? $_POST['equipment'] : '';

if(!empty($items))
{
    if ($_POST["equipment"] == "computer") {
        echo "checked computer!";
    } else if($_POST["equipment"] == "projector")
    {
        echo "checked projector!";

        $sql = "SELECT room_name, day_avail, from_time, to_time, equip_name
        FROM rooms
        JOIN equipment ON (equipment.room_id = rooms.room_id)
        JOIN room_availability ON (room_availability.room_id = rooms.room_id)
        WHERE equip_name='Projector'
        GROUP BY day_avail";

        $myData = mysql_query($sql,$conn) or die(mysql_error());

    } else if($_POST["equipment"] == "whiteboard")
    {
        echo "checked whiteboard!";
    } else if($_POST["equipment"] == "visualiser")
    {
        echo "checked visualiser!";
    } else if($_POST["equipment"] == "desk")
    {
        echo "checked desk!";
    }
} else {
    echo "> Sorry, You have not made a selection.";
}

&GT;

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

查找$.ajax的参数。你不是POST ...而且你也不是POST数据。

type: "POST"

data: str

需要在:

$('form').submit(function(){
    var str = $(this).serialize();
    $.ajax({
      url: "userLogic.php",
      cache: false,
      type: "POST",
      data: str
    }).done(function( html ) {
      $("#rooms_wrap").append(html);
    });
});

答案 1 :(得分:0)

似乎你没有发布你的数据......就像这样。

 $.ajax({
  url: "userLogic.php",
  cache: false,
  type: "POST",
  data: str,

  //rest of your code