Note that there are some explanatory texts on larger screens.

plurals
  1. POSubmiting a Form without Refreshing the page with jQuery and Ajax Not updating MySQL database
    primarykey
    data
    text
    <p>I'm a newbie to JQuery and have a problem, when I click submit button on the form everything says registration was successful but my MYSQL database was not updated everything worked fine until I tried to add the JQuery to the picture. </p> <p>Can someone help me fix this problem so my database is updated? </p> <p>Thanks</p> <p>Here is the JQuery code.</p> <pre><code>$(function() { $(".save-button").click(function() { var address = $("#address").val(); var address_two = $("#address_two").val(); var city_town = $("#city_town").val(); var state_province = $("#state_province").val(); var zipcode = $("#zipcode").val(); var country = $("#country").val(); var email = $("#email").val(); var dataString = 'address='+ address + '&amp;address_two=' + address_two + '&amp;city_town=' + city_town + '&amp;state_province=' + state_province + '&amp;zipcode=' + zipcode + '&amp;country=' + country + '$email=' + email; if(address=='' || address_two=='' || city_town=='' || state_province=='' || zipcode=='' || country=='' || email=='') { $('.success').fadeOut(200).hide(); $('.error').fadeOut(200).show(); } else { $.ajax({ type: "POST", url: "http://localhost/New%20Project/home/index.php", data: dataString, success: function(){ $('.success').fadeIn(200).show(); $('.error').fadeOut(200).hide(); } }); } return false; }); }); </code></pre> <p>Here is the PHP code.</p> <pre><code>if (isset($_POST['contact_info_submitted'])) { // Handle the form. // Query member data from the database and ready it for display $mysqli = mysqli_connect("localhost", "root", "", "sitename"); $dbc = mysqli_query($mysqli,"SELECT users.*, contact_info.* FROM users INNER JOIN contact_info ON contact_info.user_id = users.user_id WHERE users.user_id=3"); $user_id = mysqli_real_escape_string($mysqli, htmlentities('3')); $address = mysqli_real_escape_string($mysqli, htmlentities($_POST['address'])); $address_two = mysqli_real_escape_string($mysqli, htmlentities($_POST['address_two'])); $city_town = mysqli_real_escape_string($mysqli, htmlentities($_POST['city_town'])); $state_province = mysqli_real_escape_string($mysqli, htmlentities($_POST['state_province'])); $zipcode = mysqli_real_escape_string($mysqli, htmlentities($_POST['zipcode'])); $country = mysqli_real_escape_string($mysqli, htmlentities($_POST['country'])); $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email'])); //If the table is not found add it to the database if (mysqli_num_rows($dbc) == 0) { $mysqli = mysqli_connect("localhost", "root", "", "sitename"); $dbc = mysqli_query($mysqli,"INSERT INTO contact_info (user_id, address, address_two, city_town, state_province, zipcode, country, email) VALUES ('$user_id', '$address', '$address_two', '$city_town', '$state_province', '$zipcode', '$country', '$email')"); } //If the table is in the database update each field when needed if ($dbc == TRUE) { $dbc = mysqli_query($mysqli,"UPDATE contact_info SET address = '$address', address_two = '$address_two', city_town = '$city_town', state_province = '$state_province', zipcode = '$zipcode', country = '$country', email = '$email' WHERE user_id = '$user_id'"); } if (!$dbc) { // There was an error...do something about it here... print mysqli_error($mysqli); return; } } </code></pre> <p>Here is the XHTML code.</p> <pre><code>&lt;form method="post" action="index.php"&gt; &lt;fieldset&gt; &lt;ul&gt; &lt;li&gt;&lt;label for="address"&gt;Address 1: &lt;/label&gt;&lt;input type="text" name="address" id="address" size="25" class="input-size" value="&lt;?php if (isset($_POST['address'])) { echo $_POST['address']; } else if(!empty($address)) { echo $address; } ?&gt;" /&gt;&lt;/li&gt; &lt;li&gt;&lt;label for="address_two"&gt;Address 2: &lt;/label&gt;&lt;input type="text" name="address_two" id="address_two" size="25" class="input-size" value="&lt;?php if (isset($_POST['address_two'])) { echo $_POST['address_two']; } else if(!empty($address_two)) { echo $address_two; } ?&gt;" /&gt;&lt;/li&gt; &lt;li&gt;&lt;label for="city_town"&gt;City/Town: &lt;/label&gt;&lt;input type="text" name="city_town" id="city_town" size="25" class="input-size" value="&lt;?php if (isset($_POST['city_town'])) { echo $_POST['city_town']; } else if(!empty($city_town)) { echo $city_town; } ?&gt;" /&gt;&lt;/li&gt; &lt;li&gt;&lt;label for="state_province"&gt;State/Province: &lt;/label&gt; &lt;?php echo '&lt;select name="state_province" id="state_province"&gt;' . "\n"; foreach($state_options as $option) { if ($option == $state_province) { echo '&lt;option value="' . $option . '" selected="selected"&gt;' . $option . '&lt;/option&gt;' . "\n"; } else { echo '&lt;option value="'. $option . '"&gt;' . $option . '&lt;/option&gt;'."\n"; } } echo '&lt;/select&gt;'; ?&gt; &lt;/li&gt; &lt;li&gt;&lt;label for="zipcode"&gt;Zip/Post Code: &lt;/label&gt;&lt;input type="text" name="zipcode" id="zipcode" size="5" class="input-size" value="&lt;?php if (isset($_POST['zipcode'])) { echo $_POST['zipcode']; } else if(!empty($zipcode)) { echo $zipcode; } ?&gt;" /&gt;&lt;/li&gt; &lt;li&gt;&lt;label for="country"&gt;Country: &lt;/label&gt; &lt;?php echo '&lt;select name="country" id="country"&gt;' . "\n"; foreach($countries as $option) { if ($option == $country) { echo '&lt;option value="' . $option . '" selected="selected"&gt;' . $option . '&lt;/option&gt;' . "\n"; } else if($option == "-------------") { echo '&lt;option value="' . $option . '" disabled="disabled"&gt;' . $option . '&lt;/option&gt;'; } else { echo '&lt;option value="'. $option . '"&gt;' . $option . '&lt;/option&gt;'."\n"; } } echo '&lt;/select&gt;'; ?&gt; &lt;/li&gt; &lt;li&gt;&lt;label for="email"&gt;Email Address: &lt;/label&gt;&lt;input type="text" name="email" id="email" size="25" class="input-size" value="&lt;?php if (isset($_POST['email'])) { echo $_POST['email']; } else if(!empty($email)) { echo $email; } ?&gt;" /&gt;&lt;br /&gt;&lt;span&gt;We don't spam or share your email with third parties. We respect your privacy.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;input type="submit" name="submit" value="Save Changes" class="save-button" /&gt; &lt;input type="hidden" name="contact_info_submitted" value="true" /&gt; &lt;input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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