Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding form data to mysql database PHP and AJAX
    text
    copied!<p>I am wanting to have a form that will let me add names to my database without refreshing the form. I have been working on it but it does not seem to work. I am quite new at this so any help is appreciated. </p> <p>For my index.html I have:</p> <pre><code> &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"&gt; &lt;/script&gt; &lt;script type="text/javascript" &gt; $(function() { $(".submit").click(function() { var name = $("#firstname").val(); var username = $("#lastname").val(); var dataString = 'firstname='+ firstname + 'lastname=' + lastname if(firstname=='' || lastname=='') { $('.success').fadeOut(200).hide(); $('.error').fadeOut(200).show(); } else { $.ajax({ type: "POST", url: "join.php", data: dataString, success: function(){ $('.success').fadeIn(200).show(); $('.error').fadeOut(200).hide(); } }); } return false; }); }); &lt;/script&gt; &lt;body&gt; &lt;form method="post" name="form"&gt; &lt;ul&gt;&lt;li&gt; &lt;input id="firstname" name="firstname" type="text" /&gt; &lt;/li&gt;&lt;li&gt; &lt;input id="lastname" name="lastname" type="text" /&gt; &lt;/li&gt;&lt;/ul&gt; &lt;div &gt; &lt;input type="submit" value="Submit" class="submit"/&gt; &lt;span class="error" style="display:none"&gt; Please Enter Valid Data&lt;/span&gt; &lt;span class="success" style="display:none"&gt; Registration Successfully&lt;/span&gt; &lt;/div&gt;&lt;/form&gt; </code></pre> <p>For join.php:</p> <pre><code>&lt;?php include("db.php"); if($_POST) { $firstname=$_POST['firstname']; $lastname=$_POST['username']; mysql_query("INSERT INTO persons (firstname,lastname) VALUES('$firstname','$lastname')"); } ?&gt; </code></pre> <p>and db.php:</p> <pre><code>&lt;?php $mysql_hostname = "localhost"; $mysql_user = "root"; $mysql_password = ""; $mysql_database = "test"; $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database"); mysql_select_db($mysql_database, $bd) or die("Could not select database"); ?&gt; </code></pre>
 

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