0

我有以下简单的形式:

在此处输入图像描述

这是我在表单后面的代码:

<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.";
}
?>
4

2 回答 2

3

查找 的论据$.ajax。你没有POSTing... 你也没有POSTing 数据。

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);
    });
});
于 2013-07-04T22:41:27.927 回答
0

您似乎没有发布您的数据..这样做。

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

  //rest of your code
于 2013-07-04T22:43:11.303 回答