myol
myol

Reputation: 9838

Inserting from a list via a button

I'm trying to add an item from a list into a page on a button click once a single item is selected from the list. I am using php to generate the list but I want to use jQuery to insert it into the page without reloading.

At first I was thinking of having each item in the list have a class which is 'activated' on click (while somehow deactivating any other selected items) and then the button click would be 'fairly' simple to achieve by simply searching for the item with the activated class.

However, I noticed that in a CMS like wordpress, they wrap the list of items in a form, and have each item as a hidden submit button. This leads me to believe there is some way that jQuery can intercept $_POST before it calls to refresh the page? That said, it sounds pretty complex.

Is there an easier method?

Upvotes: 0

Views: 27

Answers (1)

Michael Lorton
Michael Lorton

Reputation: 44396

This is the essence of using jQuery for doing data-input.

You render the form in some way. In your case, you use (ugh) PHP.

When jQuery loads, you find the form in some way, and add a submit listener.

$(function() {
  $("#myform").submit(function( event ) {
   event.preventDefault(); // the default being to submit the form directly
   alert( "Handler for .submit() called." );
   // some other stuff
  });
});

The some-other-stuff typically involves the $.ajax functions.

Upvotes: 1

Related Questions