将PHP URL变量传递给jquery / ajax表单

时间:2012-11-27 00:10:23

标签: php javascript jquery mysql ajax

我正在使用jquery / ajax提交表单而不刷新页面。

这是我的表单代码:

<div id="event_form">
    <form name="event_submit" method="post" action="">
        <label for="what" id="what_label">What is the event?</label>
        <input type="text" name="what" id="what" class="entry_field" />
        <br />
        <label class="error" for="what" id="what_error">This field is required.</label>
        <br />
        <label for="where" id="where_label">Where is the event?</label>
        <input type="text" name="where" id="where" class="entry_field" />
        <br />
        <label class="error" for="where" id="where_error">This field is required.</label>
        <br />
        <label for="when" id="when_label">When is the event?</label>
        <input type="text" name="when" id="when" class="entry_field" />
        <br />
        <label class="error" for="when" id="when_error">This field is required.</label>
        <br />
        <label for="details" id="details_label">Additional details</label>
        <textarea rows="8" cols="30" name="details"></textarea>            
        <br />
        <input type="submit" value="Post Event" name="submit" class="submitButton" title="Click to post event" />
    </form>
</div>

这是我的jquery / ajax:

  $(function() {
    $('.error').hide();

    $(".submitButton").click(function() {
      // validate and process form
      // first hide any error messages
      $('.error').hide();

      var what = $("input#what").val();
      if (what == "") {
        $("label#what_error").show();
        $("input#what").focus();
        return false;
      }
      var where = $("input#where").val();
      if (where == "") {
        $("label#where_error").show();
        $("input#where").focus();
        return false;
      }
      var when = $("input#when").val();
      if (when == "") {
        $("label#when_error").show();
        $("input#when").focus();
        return false;
      }

      var dataString = 'what='+ what + '&where=' + where + '&when=' + when;
      //alert (dataString);return false;

      $.ajax({
        type: "POST",
        url: "event_submit.php",
        data: dataString,
        success: function() {
          $('#event_form').html("<div id='message'></div>");
          $('#message').html("<h2>Event Submitted!</h2>");
        }
       });
      return false;
    });
  });

这是php:

<?php

include "conf.php";

if ((isset($_POST['what'])) && (strlen(trim($_POST['what'])) > 0)) {
    $what = stripslashes(strip_tags($_POST['what']));
} else {$what = 'No description of the event entered.';}
if ((isset($_POST['where'])) && (strlen(trim($_POST['where'])) > 0)) {
    $where = stripslashes(strip_tags($_POST['where']));
} else {$where = 'No location entered for the event.';}
if ((isset($_POST['when'])) && (strlen(trim($_POST['when'])) > 0)) {
    $when = stripslashes(strip_tags($_POST['when']));
} else {$when = 'No time entered for the event.';}
if ((isset($_POST['details'])) && (strlen(trim($_POST['details'])) > 0)) {
    $details = stripslashes(strip_tags($_POST['details']));
}

if(isset($details)){
    mysql_query("INSERT INTO `events` (`school_id`, `what`, `where`, `when`, `details`) VALUES ('$school_id', '$what', '$where', '$when', '$details')") or die(mysql_error());
}
else {
    mysql_query("INSERT INTO `events` (`school_id`, `what`, `where`, `when`) VALUES ('$school_id', '$what', '$where', '$when')") or die(mysql_error());
}

exit;
?>

在表单所在的页面上,网址中提供的“$ school_id”为“school = 1687”或其他。我可以使用$ _GET在表单页面上检索它,但是如何将此变量传递给ajax / php?我需要将它作为变量插入表中。

4 个答案:

答案 0 :(得分:1)

这样的东西
<input type="hidden" name="schoolid" value="<?php echo $_GET['school_id']; ?>">

在你的表格中? 请理解,回应未经过简化的GET参数仅用于说明目的 - 它让您对CSS大加开放!

答案 1 :(得分:0)

您可以使用隐藏的输入。类似的东西:

<input type="hidden" id="school_id" value="<?php echo $school_id" />

然后将其添加到您的数据变量中:

var dataString = 'school_id='+$('#school_id').val()+'&what='+ what + '&where=' + where + '&when=' + when;

然后在你的php页面上检索它:

$school_id = $_POST['school_id'];

答案 2 :(得分:0)

尝试:

//Retrieving $_GET['school_id']  and passing as JSON object
var school_id = <?php echo $_GET['school_id']; ?>
var dataString = { 'what': what, 'where' : where, 'when': when, 'school_id': school_id }
$.ajax({
    type: "POST",
    url: "event_submit.php",
    data: dataString,
    type: json,
    success: function() {
      $('#event_form').html("<div id='message'></div>");
      $('#message').html("<h2>Event Submitted!</h2>");
    }
   });

您可以$_POST['school_id']上的event_submit.php访问它。

答案 3 :(得分:0)

在表单中添加隐藏字段,如下所示

 <input type="hidden" id="school_id" value="<?php echo $_REQUEST['school']; ?>" />

现在在jQuery脚本中,您可以通过单行代码

简单地获取所有表单元素值
var dataString = $('#form-id-here').serialize();

然后通过ajax发送它

$.ajax({
    type: "POST",
    url: "event_submit.php",
    data: dataString,
    success: function() {
      $('#event_form').html("<div id='message'></div>");
      $('#message').html("<h2>Event Submitted!</h2>");
    }
   });

最后将mysql插入查询更改为followig

   mysql_query("INSERT INTO `events` (`school_id`, `what`, `where`, `when`, `details`) VALUES ('".$_POST['school_id']."', '$what', '$where', '$when', '$details')") or die(mysql_error());

我想现在你已经拥有了所有需要的东西。享受:)