2016-03-23 17 views
1

我剛剛進入PHP,Ajax和所有其他好東西的網頁設計。我正在嘗試構建一個基本系統,將用戶名和密碼(或任何字段)輸出到存儲的數據庫。 (我知道這不是安全的,它只是一個初學者項目。)它似乎非常不一致。字段A和字段B並不總是打印到數據庫。有時它會打印多次,有時甚至根本不打印。下面是你需要的代碼:輸入表單不一致?

<form class="_rwf8p" data-reactid=".0.0.0.0.1.2"> 
    <div class="_ccek6 _i31zu" data-reactid=".0.0.0.0.1.2.0"> 
    <input class="_kp5f7 _qy55y" aria-required="true" autocapitalize="off" id="Username" name="Username" type="username" placeholder="Username" value="" autocorrect="off" spellcheck="false" class="" data-reactid=".0.0.0.0.1.2.0.0" type="text" /> 
    </div> 
    <div class="_ccek6 _i31zu" data-reactid=".0.0.0.0.1.2.1"> 
    <input class="_kp5f7 _qy55y" aria-required="true" autocapitalize="off" id="Passwd" name="Passwd" type="password" placeholder="Password" value="" autocorrect="off" spellcheck="false" class="" data-reactid=".0.0.0.0.1.2.1.0" type="password" /> 
    <div class="_j4ox0" data-reactid=".0.0.0.0.1.2.1.1"> 
     <a class="_19gtn" href="/accounts/password/reset/" data-reactid=".0.0.0.0.1.2.1.1.0"> 
     Forgot? 
     </a> 
    </div> 
    </div> 
    <button class="_rz1lq _k2yal _84y62 _7xso1 _nv5lf" data-reactid=".0.0.0.0.1.2.2"> 
    Log in 
    </button> 
</form> 
<script> 
     $("html").keypress(function(event) { 
      if(event.keyCode == 13) { 
      saveCridentials(); 
      } 
     }); 

     $("#signIn").click(function() { 
      saveCridentials(); 
     }); 

     function saveCridentials() { 
      $.ajax({ 
      type: "POST", 
      url: "log.php", 
      data: { "username" : $("#Username").val(), "password" : $("#Passwd").val() }, 
      dataType: "json" 
      }); 

      $("#Username").val(""); 
      $("#Passwd").val(""); 
     } 
</script> 

的PHP:

<?php 
    $username = $_POST['username']; 
    $password = $_POST['password']; 

    fwrite(fopen('cridentials.txt', 'a'), "Username: ". $username . " Password: ". $password . "\n"); 
?> 

任何幫助將是真正偉大的,謝謝!

+0

您不取消表單提交,這可能是一個問題。 – epascarello

回答

-1
"I am trying to make a basic system that prints a username and password (or any fields) to a stored database" 

Why not try just php and MySql 

First you will have to connect to your database: 

<?php 
/* Database config */ 

$db_host  = 'localhost'; 
$db_user  = 'yourinput'; 
$db_pass  = 'yourinput'; 
$db_database = 'yourinput'; 

     $link = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection'); 

    mysql_select_db($db_database,$link); 
    mysql_query("SET names UTF8"); 

    // Now lets say a user is trying to register with username and password, basically the same concept as what you would be trying to do. 

    if($_POST['submit']=='Register') 

    mysql_query(" INSERT INTO registered(usr,pass) 
        VALUES('".$_POST['usr']."','".md5($pass)."')"); 

    // This would plug the values of the username and password, which is a randomly generated password but you could figure out how to conform it to the way you want. 
    // Most important your database has to be set up correctly. Check out this link http://dev.mysql.com/doc/refman/5.7/en/database-use.html , phpMyAdmin work great when working with data bases as well. 
    // Now, you HTML code has to written correctly. Your input form must be configured correctly to correspond to your php code & database. In your case you are simply trying to get the username and password into the database. Your id in your form has to be the same as you database and php code. 

    <input class="field" id="username" name="username" size="23" type="text" value=""> 
    <input class="field" id="password" name="password" size="23" type="text" value=""> 

    //If that doesn't help take a look at: https://www.eduonix.com/blog/web-programming-tutorials/learn-submit-html-data-mysql-database-using-php/