简体   繁体   中英

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:

<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. When I make a selection, my selection is not sent to the userLogic.php file. 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:

<?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 . You're not POST ing... And you're not POST ing data, either.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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