Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using jquery/ajax to submit a form without refreshing the page.

Here is my form code:

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

Here's my 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;
    });
  });

And here's the 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;
?>

On the page that the form is on, there is a "$school_id" given in the url as ?school=1687 or whatever. I can retrieve that on the form page with $_GET, but how can I pass this variable to the ajax/php? I need to insert it in the table as a variable.

share|improve this question

4 Answers

up vote 0 down vote accepted

You could just use a hidden input. Something like:

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

then tack it onto your data variable:

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

then retrieve it on your php page:

$school_id = $_POST['school_id'];
share|improve this answer
 
Can't believe I didn't think of that. Thanks! –  Jakemmarsh Nov 27 '12 at 1:53

What about something like

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

in your form? Please understand, that echoing the unsanitized GET parameter is for illustration purposes only - it makes you wide open to CSS!

share|improve this answer

Try:

//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>");
    }
   });

You can access it as $_POST['school_id'] on event_submit.php.

share|improve this answer

Add a hidden field in the form like the following

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

Now in jQuery script you can simply get all the form elements values by a single line code

var dataString = $('#form-id-here').serialize();

Then send it though 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>");
    }
   });

Finally change the mysql insert query as the followig

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

I think now you have all the things needed. Enjoy :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.