php - Submit and AJAX -
i have registration form uses post method ajax, need submit button of type "submit" because php function seems work type set submit want ajax send data off , return result instantaneously without having page change.
html
<form name = "reg" action = "insert.php" method = "post"> <table> <tr> <td class="ule">first name</td> <td><input type = "text" name="firstnames" id="firstnames"></input></td> </tr> <tr> <td class="ule">last name</td> <td><input type = "text" name="lastnames" id="lastnames"></input></td> </tr> <tr> <td class="ule">email</td> <td><input type = "text" name="emails" id="emails"></input></td> </tr> <tr> <td class="ule">user name</td> <td><input type = "text" name = "usernames" id="usernames"></input></td> </tr> <tr> <td class="ule">password</td> <td><input type = "password" name = "passwords" id="passwords"></input></td> </tr> </table> <div class="regbutton"> <button onclick = "register()" type = "button" >register</button> </div> </form>
php
<?php session_start(); $db = sqlite_open('my_database.db', 0666, $error); $fname = $_post["firstnames"]; //potential problem area $lname = $_post["lastnames"]; $email = $_post["emails"]; $user = $_post["usernames"]; $pass = $_post["passwords"]; $query = "insert user (firstname, lastname, email, username, password) values ('.$fname','.$lname','.$email','.$user','.$pass')"; insertrows($db, $fname, $lname, $email, $user, $pass, $query); function insertrows($dbs, $names, $lnames, $emails, $usernames, $passwords, $querys) { if($names != null && $lnames != null && $emails != null && $usernames != null && $passwords != null) { echo "thank registering"; $result = sqlite_query($dbs, $querys); } else { echo "<h1 style=color:red;>fill out required fields</h1>"; } } ?>
javascript
function register(){ var xmlhttpreq1; var name = encodeuricomponent(document.getelementbyid('firstnames').value); var lastname = encodeuricomponent(document.getelementbyid('lastnames').value); var myemail = encodeuricomponent(document.getelementbyid('emails').value); var users = encodeuricomponent(document.getelementbyid('usernames').value); var passwords = encodeuricomponent(document.getelementbyid('passwords').value); var params = "fname="+name+"&lname="+lastname+"&email="+myemail+"&user="+users+"&pass="+passwords; if (window.xmlhttprequest) { try { xmlhttpreq1 = new xmlhttprequest(); } catch(e) { xmlhttpreq1 = false; } } else { try{ xmlhttpreq1 = new activexobject("microsoft.xmlhttp"); }catch(e){ xmlhttpreq1 = false; } } xmlhttpreq1.onreadystatechange = function() { if(xmlhttpreq1.readystate == 4 && xmlhttpreq1.status == 200){ document.getelementbyid('mydiv').innerhtml = xmlhttpreq1.responsetext; } } xmlhttpreq1.open("post", "insert.php?", true); xmlhttpreq1.setrequestheader("content-type","application/x-www-form-urlencoded"); xmlhttpreq1.send(params); }
no don't, can have button of type or link or whatever actually, , add onclick()
event ajax call.
note submit button won't submit form (and reload page) if in onsubmit()
event of form return false.
eg :
<form method='post' onsubmit='doajax();return false;'> <input.../> <input.../> <input type='submit'... /> </form>
Comments
Post a Comment