Need help working with AJAX

LouFfweew

New Member
Overview of what I want page to do:Create a dynamic table pulling values from MySQL dB, and display them to user. Each item grabbed will have a radio button to turn that item on or off. When user clicks on radio button JS will automatically reload page and send new values to PHP, which will then update the MySQL dB. I'm currently stuck at the second part, reading around I learned that AJAX would be a good solution to this problem but I have no AJAX experience and my JS experience is limited. I feel that I'm really close to a solution but the details evade me. Part where I need the help is outlined in the code: \[code\] <?php require_once("includes/db_connect.php"); ?> <?php require_once("includes/functions.php"); ?> <?php print '<a href="http://stackoverflow.com/questions/10002769/edit_site.php">Back to Main</a>' . '<br>' . '<br>' ?> <?php echo "<center>"; ?> <?php //Grab information from JS //NEED HELP here ****** if (isset($_POST['visible']) && !empty($_POST['visible'])) { //Should grab the value of radio buttion ON/OFF $visible3 = my_real_escape_string($_POST['visible']); $id = intval($_GET['name']); //Should grab the value of $button_num depending on radio clicked $sql = "UPDATE gallery_1 SET visible='{$visible3}' WHERE id='{$id}'"; #Run the update $result = mysql_query($sql); // test to see if the update occurred if (mysql_affected_rows() == 1) { #Debug only // Success! $message = "<font color=\"green\" size=\"8pt\">The page was successfully updated.</font>"; } else { $message = "<font color=\"red\" size=\"8pt\">The page could not be updated."; $message .= "<br />" . mysql_error() . "</font>"; } } ?> <script type="text/javascript"> //Reload the page //Submit information to PHP function submit() { //NEED HELP here ****** //Need stuff that goes in here? //Should grab the name of the radio and the value ON/OFF document.forms['gallery'] submit(); } </script> <?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} #Display messages Debug only?> <?php $max_cols = 6; $i = 0; $visible = ""; $button_num = ""; echo "<form name=\"gallery\" action=\"edit_gallery.php\" method=\"post\">"; echo "<table border=\"1\" cellpadding=\"0\" cellspacing=\"6\" width=\"100%\">"; echo "<tr>"; $sql = mysql_query("SELECT * FROM gallery_1"); while($row = mysql_fetch_array($sql)) { $button_num = $row['id']; $visible1 = ""; //Establishes varibles $visible2 = ""; //Establishes varibles $visible = $row['visible']; if ($row['visible'] == '1') //makes sure the correct radio is checked on load {$visible1 = "checked /";} if ($row['visible'] == '0') {$visible2 = "checked /";} if ($i == $max_cols) { $i = 0; echo "</tr><tr>"; } //Creates the dynamic table print '<td>' . '<center><div><img src=http://stackoverflow.com/questions/10002769/website/' . $row['path'] . '>' . '<br>' . 'Product ID: ' . $row['id'] . '<br>' . 'Size: ' . $row['size'] . '<br>' . 'Visible: ' . $row['visible'] . '<br>' . '<input type="radio" name="' . $button_num .'"' . ' value="http://stackoverflow.com/questions/10002769/1" onClick="submit()" ' . $visible1 .'>' . 'ON' . '<input type="radio" name="' . $button_num .'"' . ' value="http://stackoverflow.com/questions/10002769/0" onClick="submit()" ' . $visible2 .'>' . 'OFF' . '<br>' . '</td></div></center>'; $i++; } //Add empty <td>'s to even up the amount of cells in a row while ($i <= $max_cols) { echo "<td></td>"; $i++; } //Close the table row and the table echo "</tr>"; echo "</table>"; echo "</form>"; ?>\[/code\]
 
Top