2015-01-06 37 views
0

首先,我非常抱歉發佈了其中一個這樣的問題,但是我已經通過了大量與此主題相關的SO問題,並且無法解決我的問題。我是PHP新手,最好使用Jquery mobile等。從使用php的窗體開始,使用JQuery Mobile進行基本POST POST

我試圖張貼到.php文件並得到迴應。最終這將演變成一個數據庫張貼yada yada。現在,我似乎無法從我的帖子中得到我的回覆。我運行XAMPP主辦的PHP,jQuery Mobile的被其他功能使用,因此它不正常工作,

HTML:

<form> 
     <p>Username: </p><input type="text" id="username" value="" /> 
     <p>Password: </p><input type="text" id="password" value="" /> 
     <input type="button" onclick="submitLogIn()" value="Log In" /> 
</form> 

的Javascript:

function submitLogIn() { 

    alert("Submitting: " + $('#username').val() + $('#password').val()); 
    var dbURL = "http://localhost/testerpage.php"; 

    $.post(dbURL, { 
     //These are the names of the form values 
     Username: $('#username').val(), 
     Password: $('#password').val() 

    }, function (data,status) { 
     alert(status); //Won't fire 
     alert(html); //Won't fire 
     var response = html; 
     alert(response); //Won't fire 
     if (response == "Success") 
     { 
      alert("Success!"); //Won't fire 
      //testlog.innerHTML = "Success"; 
     } 
     else 
     { 
      alert("Failure!");//Won't fire 
      //testlog.innerHTML = "Failure"; 
     } 

    }); 

    alert("Finished"); //Fires 

}; 

PHP

<?php 

    // VARS 
    $Username=$_GET["Username"]; //Also tried _POST 
    $Password=$_GET["Password"]; //Also tried _POST 

    //VALIDATION 
    if(
    $Username=="" || 
    $Password=="" 
    ) { 
     echo "Error"; 
    } else { 
     echo "Success"; 
    } 
?> 

我最好的猜測是,.php有些問題,因爲我看過的所有問題似乎證實我的JavaScript是正確的。除了回撥功能中的警報外,我的所有警報都會啓動。用戶名和密碼也正確設置,這不是問題。我嘗試在我的.php文件中使用_POST和_GET,我原來使用_POST是因爲我發佈了數據,但是我正在關注這個問題:(Phonegap contact form not working over PHP),它做了相反的事情,所以我改變了它。沒有不同。我的.php實際上是託管的(我可以導航到它沒有錯誤)。我也嘗試使用$ .ajax函數,但也有相同的問題。

在此先感謝。

編輯:每個請求添加了更多的HTML(所有應該相關的),不能全部添加它,因爲它太長了。

<html> 
     <head> 
      <meta charset="utf-8" /> 
      <meta name="format-detection" content="telephone=no" /> 
      <meta name="msapplication-tap-highlight" content="no" /> 
      <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> 
      <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 

      <!-- Stylesheets --> 
      <link rel="stylesheet" href="css/snctfy2/snctfy2.css" /> 
      <link rel="stylesheet" href="css/snctfy2/jquery.mobile.icons.min.css" /> 

      <link rel="stylesheet" type="text/css" href="css/index.css" /> 
      <link href="jquerymobile/jquery.mobile.structure-1.4.2.min.css" rel="stylesheet" type="text/css" /> <!-- Add .structure after theme--> 

      <!-- Jquery core --> 
      <script src="js/jquery.js" type="text/javascript"></script> 

      <!-- Jquery mobile library file --> 
      <script src="jquerymobile/jquery.mobile-1.4.2.min.js" type="text/javascript"></script> 

      <!-- DateBox --> 
      <link rel="stylesheet" type="text/css" href="css/jqm-datebox.css" /> 
      <script type="text/javascript" src="js/datebox/jqm-datebox.core.js"></script> 
      <script type="text/javascript" src="js/datebox/jqm-datebox.mode.calbox.js"></script> 
      <script type="text/javascript" src="js/datebox/jqm-datebox.mode.datebox.js"></script> 
      <script type="text/javascript" src="js/datebox/jquery.mobile.datebox.i18n.en_US.utf8.js"></script> 


      <!-- Scripts (pre-load)--> 
      <script src="js/scripts.js" type="text/javascript"></script> 

      <!-- CSS Override --> 
      <link rel="stylesheet" type="text/css" href="css/override.css" /> 

      <title>SNCTFY</title> 
     </head> 
     <body> 

    <!--there is some more <div> tags here unrelated--> 
     <!--------------------------------------------------------Login Page--------------------- 

--------------------------------------------> 
      <div data-role="page" id="login" data-theme="a" class="bPage"> 
       <div data-role="content"> 
        <form> 
         <p>Username: </p><input type="text" id="username" value="" /> 
         <p>Password: </p><input type="text" id="password" value="" /> 
         <input type="button" onclick="submitLogIn()" value="Log In" /> 
        </form> 
        <a href="#register" data-role="button">Register</a> 
        <button onclick="showAlert()">Test</button> 
        <p id="testlog">Results</p> 
       </div> 
      </div> 

    <!-- more <div> pages --> 

      <!-- Scripts (post-load)--> 
      <script type="text/javascript" src="phonegap.js"></script> 
      <script type="text/javascript" src="cordova.js"></script> 
      <script type="text/javascript" src="js/index.js"></script> 
      <script type="text/javascript"> 
       app.initialize(); 
      </script> 
     </body> 
    </html> 

EDIT2:更改JavaScript來答案的一個測試

function submitLogIn() { 
    alert("Submitting: " + $('#username').val() + $('#password').val()); 
    var username = $('#username').val(); 
    var password = $('#password').val(); 
    $.ajax({ 
     type: "POST", 
     url: "http://localhost/testerpage.php", 
     data: { "Username": username, "Password": password }, 
     success: function (data) { 
      if (data) { 

       alert(data); 
      } 
      else { 
       alert('Successfully not posted.'); 
      } 
     } 
    }); 

}; 
+0

? – Priyank

回答

1

就是爲什麼你不使用jQuery的AJAX嘗試jQuery的阿賈克斯

<body> 
<form> 
    <p>Username: </p><input type="text" id="username" value="" /> 
    <p>Password: </p><input type="text" id="password" value="" /> 
    <input type="button" onclick="submitLogIn()" value="Log In" /> 
</form> 
<script> 
function submitLogIn() { 
alert("Submitting: " + $('#username').val() + $('#password').val()); 
var username =$('#username').val(); 
var password = $('#password').val(); 
$.ajax({ 
    type: "POST", 
    url: "http://localhost/testerpage.php", 
    data:{"Username":username,"Password":password}, 
    success: function(data) { 
    if (data) { 

     alert(data); 
    } 
    else { 
     alert('Successfully not posted.'); 
    } 
    } 
    }); 

    } 

</script> 
</body> 
</html> 

在PHP

<?php 
$Username=$_POST["Username"]; //Also tried _POST 
$Password=$_POST["Password"]; //Also tried _POST 

//VALIDATION 
if(
$Username=="" || 
$Password=="" 
) { 
    echo "Error"; 
} else { 
    echo "Success"; 
} 
?> 
+0

@Draze試試這個,讓我知道,它的工作與否? – Priyank

+0

不工作,甚至嘗試在你的HTML文檔中使用

0

試試這個:

function submitLogIn() { 

    alert("Submitting: " + $('#username').val() + $('#password').val()); 
    var dbURL = "http://localhost/testerpage.php"; 

    //These are the names of the form values 
    var username = $('#username').val(); 
    var password = $('#password').val(); 

    $.ajax({ 
     url: dbURL, 
     type:'post', 
     data:'&username='+username+'&pass='+password, 
     success:function(response){ 
       alert(response); 
     } 
    }); 

}; 




<?php 

    // VARS 
    $Username=$_POST["username"]; //Also tried _POST 
    $Password=$_POST["pass"]; //Also tried _POST 

    //VALIDATION 
    if(
    $Username=="" || 
    $Password=="" 
    ) { 
     echo "Error"; 
    } else { 
     echo "Success"; 
    } 
?> 
+0

這並沒有解決我的問題。 –

0

試試這個代碼 腳本

<script> 
function submitLogIn() { 
    alert("Submitting: " + $('#username').val() + $('#password').val()); 
    var dbURL = "http://localhost/testerpage.php"; 

    $.post(dbURL, { 
    //These are the names of the form values 
    Username: $('#username').val(), 
    Password: $('#password').val() 

}, function (data,status) { 
    if (data == "Success") 
    { 
     alert("Success!"); //Won't fire 
     //testlog.innerHTML = "Success"; 
    } 
    else 
    { 
     alert("Failure!");//Won't fire 
     //testlog.innerHTML = "Failure"; 
    } 

}); 
alert("Finished"); //Fires 
}; 
</script> 

PHP

// VARS 
$Username=$_POST["Username"]; //Also tried _POST 
$Password=$_POST["Password"]; //Also tried _POST 

//VALIDATION 
if(
$Username=="" || 
$Password=="" 
) { 
    echo "Error"; 
} else { 
    echo "Success"; 
} 
+0

這並沒有解決問題,因爲唯一改變的是當前沒有被調用的回調函數。 –

+0

Draze,我改變了回調以及php代碼中的$ _post。請檢查您的dburl是否有效。開發者控制檯中是否有任何錯誤?你知道如何調試你的JavaScript代碼嗎? – Christian

+0

對不起,我花了這麼長的時間來回應。我其實已經嘗試過做一個更簡單的帖子,但它也無法正常工作。如果我做了。ajax響應我得到拋出的錯誤函數,但唯一引發的是'錯誤'狀態。錯誤消息爲空。我在想,訪問我的本地主機的PHP腳本時出了點問題,我不知道有關權限或需要更改的內容,但是我唯一能想到的就是錯誤的。 –