简体   繁体   English

带有PHP和AJAX请求的表单

[英]Form with PHP and AJAX Request

I have the following simple form: 我有以下简单形式:

在此处输入图片说明

Here is my code behind the form: 这是表格后面的代码:

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

And finally here is how I am making the AJAX request on the same page where this form sits: 最后,这是我如何在此表单所在的同一页面上提出AJAX请求:

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

I am fairly new to PHP and I'm having an issue with the form submission. 我对PHP相当陌生,并且表单提交存在问题。 When I make a selection, my selection is not sent to the userLogic.php file. 当我做出选择时,我的选择不会发送到userLogic.php文件。 I get a print out of: 我打印出以下内容:

Sorry, You have not made a selection. 抱歉,您尚未选择。

This is coming from the PHP code that sits inside the userLogic.php file which is this: 这来自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.";
}
?>

Look up the arguments for $.ajax . 查找$.ajax的参数。 You're not POST ing... And you're not POST ing data, either. 你不是POST ING ......你也不是POST ING的数据,无论是。

type: "POST"

and

data: str

needs to be in: 需要在:

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

Doesn't seem you are posting your data..do it like this. 看来您不是要发布数据。

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

  //rest of your code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM