gpt4 book ai didi

javascript - 使用 php 从表单使用 JQuery Mobile 进行基本 POST

转载 作者:行者123 更新时间:2023-12-03 11:07:09 24 4
gpt4 key购买 nike

首先,我为发布另一个此类问题而道歉,但我已经研究了大量与该主题相关的问题,但无法找出我的问题。我是 PHP 新手,最多也是使用 Jquery mobile 等的新手。

我正在尝试发布到 .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.');
}
}
});

};

最佳答案

试试 jquery Ajax

<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";
}
?>

关于javascript - 使用 php 从表单使用 JQuery Mobile 进行基本 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27792209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com