Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery ajax success function works only once
    primarykey
    data
    text
    <p>I'm trying to implement a form that utilizes jquery's post feature to dynamically update the database. What I'm realizing is that after the user clicks the "update" button, the success function is called back just fine with a "Update successful" message. </p> <p>The issue I have for the stackoverflow world is why on subsequent clicks (w/o refreshing the page) I'm not getting this same success message. Also, ironically my database is being updated, so I know the AJAX call is going through.</p> <p>I've posted my code below:</p> <p>JS</p> <pre><code>var TEAM = { update: function() { var form_data = $('form').serialize(); $.ajax({ type: "POST", url: "../manager/edit_team.php", data: form_data, error: function() { $('#status').text('Update failed. Try again.').slideDown('slow'); }, success: function() { $('#status').text('Update successful!'); }, complete: function() { setTimeout(function() { $('#status').slideUp('slow'); }, 3000); }, cache: false }); } } // jQuery Code for when page is loaded $(document).ready(function() { $("#update").on("click", function() { TEAM.update(); }); }); </code></pre> <p>PHP (I welcome any other comments as well) <pre><code>require '../includes/config.php'; include '../includes/header.html'; // autoloading of classes function __autoload($class) { require_once('../classes/' . $class . '.php'); } // Site access level -&gt; Manager $lvl = 'M'; // Assign user object from session variable if (isset($_SESSION['userObj'])) { $manager = $_SESSION['userObj']; } else { session_unset(); session_destroy(); $url = BASE_URL . 'index.php'; ob_end_clean(); header("Location: $url"); exit(); } // Establish database connection require_once MYSQL2; // Assign Database Resource to object $manager-&gt;setDB($db); // Authorized Login Check if (!$manager-&gt;valid($lvl)) { session_unset(); session_destroy(); $url = BASE_URL . 'index.php'; ob_end_clean(); header("Location: $url"); exit(); } // Check for a valid game sch ID, through GET or POST: if ( (isset($_GET['z'])) &amp;&amp; (is_numeric($_GET['z'])) ) { // Point A in Code Flow // Assign variable from myteams-m.php using GET method $id = $_GET['z']; } elseif ( (isset($_POST['z'])) &amp;&amp; (is_numeric($_POST['z'])) ) { // Point C in Code Flow // Assign variable from edit_team.php FORM submission (hidden id field) $id = $_POST['z']; } else { // No valid ID, kill the script. echo '&lt;p class="error"&gt;This page has been accessed in error.&lt;/p&gt;'; include '../includes/footer.html'; exit(); } $team = new ManagerTeam(); $team-&gt;setDB($db); $team-&gt;setTeamID($id); $team-&gt;pullTeamData(); $flag = 0; echo $flag . "&lt;br /&gt;"; // Confirmation that form has been submitted: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Point D in Code Flow // Assume invalid values: $tname = FALSE; // Validate team name if ($_POST['tname']) { $tname = $_POST['tname']; } else { echo '&lt;p class="error"&gt; Please enter a team name.&lt;/p&gt;'; } // Validate about team information if ($_POST['abouttm']) { $abtm = trim($_POST['abouttm']); } else { $abtm = ''; } // Check if user entered information is valid before continuing to edit game if ($tname) { if($team-&gt;editTeam($tname, $abtm) == True) { echo '&lt;p&gt;Team was successfully updated&lt;/p&gt;'; $flag = 1; } else { echo '&lt;p&gt;No changes were made&lt;/p&gt;'; $flag = 2; } } else { // Errors in the user entered information echo '&lt;p class="error"&gt;Please try again.&lt;/p&gt;'; } } // End of submit conditional. echo $flag . "&lt;br /&gt;"; // Point B in Code Flow // Always show the form... // Get team name attribute $team-&gt;pullTeamData(); $teamname = $team-&gt;getTeamAttribute('tmname'); $about = $team-&gt;getTeamAttribute('about'); if ($teamname != '') // Valid user ID, show the form. { // Headliner echo '&lt;h2&gt;Edit Team&lt;/h2&gt;'; // Create the form: echo ' &lt;div id="EditTeam"&gt;&lt;/div&gt; &lt;div id="Team"&gt; &lt;fieldset id="TeamDetails"&gt; &lt;legend&gt;Edit Team&lt;/legend&gt; &lt;form method="post" id="information"&gt; &lt;p id="status"&gt;&lt;/p&gt; &lt;input type="hidden" name="z" value="' . $id . '" /&gt; &lt;p&gt; &lt;label for="tname"&gt;New Team Name:&lt;/label&gt;&lt;br/&gt; &lt;input type="text" name="tname" id="tname" size="10" maxlength="45" value="' . $teamname . '" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="abouttm"&gt;Team Information:&lt;/label&gt;&lt;br/&gt; &lt;textarea id="abouttm" name="abouttm" cols="30" rows="2"&gt;"' . $about . '"&lt;/textarea&gt;&lt;br /&gt; &lt;small&gt;Enter something cool about your team.&lt;/small&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="hidden" name="id" id="id"&gt; &lt;input type="button" value="update" id="update" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;/fieldset&gt; &lt;/div&gt;'; } else { //Not a valid user ID, kill the script echo '&lt;p class="error"&gt;This page has been accessed in error.&lt;/p&gt;'; include '../includes/footer.html'; exit(); } // Close the connection: $db-&gt;close(); unset($db); include '../includes/footer.html'; ?&gt; </code></pre> <p>You'll notice I also have a $flag defined to help with the debugging, but ironically it outputs 0 no matter the number of clicks to the "update" button. So there's no indication that the database is being updated, yet when I check the tables it certainly is.</p> <p>I appreciate any help or pointers. Thanks,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload