Hello,
I'm a bit confused by how jQuery ajax works. I'm trying to create a customer management system (CMS) with jQuery and php.
I want to give the user the opportunity to create a car. Let's say he must define a "car name" and check some options in a checkbox list.
At the same page i give the user the opportunity to add or delete some options. This adding
and deleting happens with jQuery and ajax. When a user add an option, i call a php page (addOption.php) that processes the option and add's it to the database. If the ajax call is successful, i reload the options in a div using the ajax load() function. Who calls a php page (reloadOptions.php) that reads out the options from the database and constructs them into a list of check-boxes.
All this works fine. The problem occurs when i try to check what check-boxes are checked.
(difficult sentence here

)
It seems that the data don't exist in the page source. I think this happens because of the asynchronous working of ajax. And i really need the options to create the car!
Finally, my question is how can i access that options data? And if you can't, is there still
a way where you can access that data?
What do you think about my approach to handle the cms system? Good / Bad / omg / ...
Below are some code blocks i use:
The page index.php : jQuery/ajax that processes the options and the html code
- //At the start i load the options in
- $('.options').load('./php/OptionsReload.php);
- $("#FormOptions").submit(function(e)
- {
- $.ajax({
- type: "POST",
- url: "./php/addOption.php",
- data: $(e.target).serialize(),
- dataType: "json",
- success: function(data)
- {
- $('.options').load('./php/reloadOptions.php);
- }
- });
- return false;
- });
- ..................
- //Form where you can add the option
- <form id="FormOptions">
- <input type="text" name="optionName" />
- <input type="submit" name="submitOption" value="Add option" />
- </form>
- ......................
- //Div where i show the options to the user
- <form name="formChecks" method="post" enctype="multipart/form-data">
- <div class="options">
- //Content loaded with jQuery
- </div>
- </form>
The page addOption.php : code that adds the option to the database
- //Database connect
- ...
- return mysql_query("INSERT INTO options(option) VALUES('". $POST_['optionName'] ."');");
The page reloadOptions.php : code that constructs the html with the options
- <select name="mydropdown">
- <?php
- $options = ...... connect with DB and select all the options
- while($row = mysql_fetch_assoc($options))
- {
- echo ("<option value='" . $row['Id'] . "'>" . $row['optionName'] . "</option>");
- }
- ?>
- </select>