Note that there are some explanatory texts on larger screens.

plurals
  1. POSubmitting data using ajax to refresh
    primarykey
    data
    text
    <p>I have successfully used ajax to refresh in-line, via ajax, a contact form. I'm having a bit more difficulty getting it to work with data being sent to mysql. I appreciate that what I have so far is not intended to be refreshed via ajax so it might need some work. Here's what I've got... and any help is appreciated.</p> <p>Form</p> <pre><code>&lt;form name="email_list" action=""&gt; &lt;p&gt;&lt;strong&gt;Your Email Address:&lt;/strong&gt;&lt;br/&gt; &lt;input type="text" name="email" id="email" size="40"&gt; &lt;input type="hidden" name="sub" id="sub" value="sub"&gt; &lt;p&gt;&lt;input type="submit" name="submit" value="Submit Form" class="email_submit"&gt;&lt;/p&gt; &lt;/form&gt; </code></pre> <p>JQuery</p> <pre><code>$(function() { $('.email_submit').submit(function() { var email = $("input#email").val(); if (name == "") { $("input#email").focus(); return false; } var sub = $("input#sub").val(); if (name == "") { $("input#sub").focus(); return false; } var dataString = $(this).serialize(); //alert (dataString);return false; /*$.ajax({ type: "POST", url: "mailing_list_add2.php", data: dataString, success: function() { $('#display_block') .hide() .fadeIn(2500, function() { $('#display_block'); }); } }); return false; });*/ }); </code></pre> <p>PHP</p> <pre><code>&lt;?php // connects the database access information this file include("mailing_list_include.php"); // the following code relates to mailing list signups only if (($_POST) &amp;&amp; ($_POST["action"] == "sub")) { if ($_POST["email"] == "") { header("Location: mailing_list_add.php"); exit; } else { // connect to database doDB(); // filtering out anything that isn't an email address if ( filter_var(($_POST["email"]), FILTER_VALIDATE_EMAIL) == TRUE) { echo ''; } else { echo 'Invalid Email Address'; exit; } // check that the email is in the database emailChecker($_POST["email"]); // get number of results and do action if (mysqli_num_rows($check_res) &lt; 1) { // free result mysqli_free_result($check_res); // cleans all input variables at once $email = mysqli_real_escape_string($mysqli, $_POST['email']); // add record $add_sql = "INSERT INTO subscribers (email) VALUES('$email')"; $add_res = mysqli_query($mysqli, $add_sql) or die(mysqli_error($mysqli)); $display_block = "&lt;p&gt;Thanks for signing up!&lt;/p&gt;"; // close connection to mysql mysqli_close($mysqli); } else { // print failure message $display_block = "You're email address - ".$_POST["email"]." - is already subscribed."; } } } ?&gt; &lt;html&gt; &lt;?php echo "$display_block";?&gt; &lt;/html&gt; </code></pre> <p>**Updated above after making changes :)</p>
    singulars
    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.
    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