• 0

JavaScript (jQuery) + PHP + MySQL - need some help :)


Question

Hi everyone,

I'm writing my first CRM app in PHP and I have some problems with it. So I hope someone will help. :)

I have database with users, and I want to create a new event (meeting or something). I created form to add new event and now I want to take existing user from database and insert it to the event page. I added some jQuery and when i click "add existing user to this event" the popup div pops up with all users in the database. Now I want to click on user and take info about him and insert it to an event page without any page reload so that i won't loose any data inserted in other fields. And here is the question -> How to do that? :)

I guess i have to use some POST method but I don't know how to do that with jQuery.

Any ideas?

P.S. Sorry for my English. ;)

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Something like;

$.ajax({
  type: 'POST',
  url: 'http://www..../script-that-adds-user-to-event.php',
  data: ["an", "array", "of", "user", "ids", "or", "something"],
  success: successFunction
  dataType: 'json'
});

jQuery.ajax. There's a jQuery.post method too that's shorthand of the above code IIRC.

Link to comment
Share on other sites

  • 0

Something like;

$.ajax({
  type: 'POST',
  url: 'http://www..../script-that-adds-user-to-event.php',
  data: ["an", "array", "of", "user", "ids", "or", "something"],
  success: successFunction
  dataType: 'json'
});

jQuery.ajax. There's a jQuery.post method too that's shorthand of the above code IIRC.

Sorry, still can't wrap my head around this. :( I basically know what that $.ajax stuff will do but still don't know where to go from here. :(

Link to comment
Share on other sites

  • 0

That code will send the parameters defined in 'data' to the 'url' using POST. If the request is made successfully, the successFunction() will be called with the server's response as the parameter. dataType is the type of data you're expecting from the server. So in this example, you could get the server to reply with {status: 1} or something similar to represent the users were added to the event. The successFunction could then validate the response to see if the users were added to the event or if there was an error. You'd probably have it provide some sort of user feedback depending on if the users were successfully added.

The script-that-adds-users-to-event.php is the server-side script that will take the `data` passed to it and add those users to the event. I guess a better example for the data parameter would be something like:

data: {users : [12,13,111,14,35], event: 3}

Where users is an array of user IDs who you want to add to the event and event is the event ID to add the users to.

I just re-read your first post, there's probably a better way of doing it than making an ajax request. Am I right in assuming that the event's info and user list should all be saved in one go at the end? If so, once the user selects which users to add to the event, you could dynamically add input elements inside the <form> for the selected user IDs. For example, if the user selects users with IDs 3 and 4, add these elements to the event creation form;

<input type='hidden' name='uids[]' value='3' />

<input type='hidden' name='uids[]' value='4' />

This way will send the selected user IDs to the server along with all the form info needed to create the event and can be handled at the same time.

Link to comment
Share on other sites

  • 0

It sounds like you are just posting form data. jQuery makes it easy to grab an entire form. Just do this!


$.ajax({
  type: 'POST',
  url: 'http://www..../script-that-adds-user-to-event.php',
  data: $('#FormIDName').serialize(),
  success: successFunction
});

Then on your PHP side you can just grab the post variables ($_POST['varName'])

Then back in jQuery in your successFunction you can do something to say the data has been posted successfully

Hope that makes sense :)

Link to comment
Share on other sites

  • 0

I would suggest using JSON to achieve this (jQuery JSON) I'm at work at the moment, but when I get home I'll take a look in more depth for you

Thank you. I would appreciate it. :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.