jQuery移动PHP表单post返回未定义的

jQuery移动PHP表单post返回未定义的,php,jquery-mobile,Php,Jquery Mobile,我在这里看到了很多类似的问题,但与我所经历的完全不同 我在我的网站上有一个简单的联系人页面,它可以很好地用于桌面版本,但是当我使用jQuery mobile设置移动版本时,在发布页面时,它只返回未定义的页面 下面是php后面的表单 <html> <head> <title>John's Website | Contact Me</title> <meta name="viewport" content="width=device-width,

我在这里看到了很多类似的问题,但与我所经历的完全不同

我在我的网站上有一个简单的联系人页面,它可以很好地用于桌面版本,但是当我使用jQuery mobile设置移动版本时,在发布页面时,它只返回未定义的页面

下面是php后面的表单

<html>
<head>
<title>John's Website | Contact Me</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery.mobile-1.1.0.min.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<LINK REL="SHORTCUT ICON" HREF="../favicon.ico"> 
</head>
<body>
<div data-role="page">
<h2>Fill out the form below to contact me.</h2>
<form action="contact.php" method="post">
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" size="40" /><br />
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName" size="40" /><br />
<label for="userEmail">Your Email:</label>
<input type="text" id="userEmail" name="userEmail" size="40" /><br />
<label for="message">Message:</label>
<textarea id="message" name="message" cols="30" rows="5"></textarea><br />
<input data-role="none" type="submit" value="Submit"></input>
<input data-role="none" type="reset" value="Reset"></input>
</form> 
</div>
</body>
</html>

约翰的网站|联系我
请填写以下表格与我联系。
名字:

姓氏:
您的电子邮件:
信息:
还有PHP

<?php
$host="localhost"; // Host name 
$username="Idont"; // Mysql username 
$password="thinkso"; // Mysql password 
$db_name="john"; // Database name 
$tbl_name="contact"; // Table name

//connect
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

//gather vars and sanitize input
$first_name = htmlentities($_POST['firstName']);
$last_name = htmlentities($_POST['lastName']);
$email = htmlentities($_POST['userEmail']);
$message = htmlentities($_POST['message']);
$date_stamp = date("Y-m-d h:i:s");

$first_name = mysql_real_escape_string($_POST['firstName']);
$last_name = mysql_real_escape_string($_POST['lastName']);
$email = mysql_real_escape_string($_POST['userEmail']);
$message = mysql_real_escape_string($_POST['message']);


if(empty($_POST['firstName']) && empty($_POST['lastName']) &&
empty($_POST['userEmail']) && empty($_POST['message'])){
echo 'You did not fill out all fields. Please go back and enter all info.';
}

//write contents to db.
$sql = "INSERT INTO $tbl_name (firstName, lastName, email, date, message) 
VALUES ('$first_name', '$last_name', '$email', '$date_stamp', '$message')";

if(mysql_query($sql)){
$content = "Your message has been sent. /n Click <a href="index.php">here</a> to go
back to the home page.";
} else {
$content = mysql_error() . $date_stamp . 'Unable to send your message. Try again
later.';
}
?>

<html>
<head>
<title>John's Website | Contact Me</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery.mobile-1.1.0.min.js"></script>
<LINK href="../js/jquery.mobile-1.1.0.min.css" rel="stylesheet" type="text/css">
<LINK REL="SHORTCUT ICON" HREF="../favicon.ico"> 
</head>
<body>
<div data-role="page">

<div data-role="header">its loaded</div>
<div data-role="content"><?php echo $content; ?></div>
</div>
</body>
</html>

首先检查$\u POST,但也执行SQL,没有条件停止执行或

这就是你所拥有的:

if( empty($_POST['firstName']) && 
    empty($_POST['lastName']) &&
    empty($_POST['userEmail']) && 
    empty($_POST['message'])) {
    echo 'You did not fill out all fields. Please go back and enter all info.';
}
// continue to execute the script ( SQL Below )
这是一项建议:

if( empty($_POST['firstName']) && 
    empty($_POST['lastName']) &&
    empty($_POST['userEmail']) && 
    empty($_POST['message'])) {

    // use $content instead of echo
    $content = 'You did not fill out all fields. Please go back and enter all info.';
}
// Add else condition here
else {
    //write contents to db.
    $sql = "INSERT INTO $tbl_name (firstName, lastName, email, date, message) 
    VALUES ('$first_name', '$last_name', '$email', '$date_stamp', '$message')";

    if(mysql_query($sql)) {
        $content = "Your message has been sent. /n Click <a href="index.php">here</a> to go
back to the home page.";
    } else {
        $content = mysql_error() . $date_stamp . 'Unable to send your message. Try again
later.';
    }
}
if(空($_POST['firstName'])&&
空($\u POST['lastName'])&&
空($_POST['userEmail'])和
空($\u POST['message'])){
//使用$content而不是echo
$content='您没有填写所有字段。请返回并输入所有信息';
}
//在此处添加其他条件
否则{
//将内容写入数据库。
$sql=“插入$tbl_名称(名字、姓氏、电子邮件、日期、消息)
值(“$first_name”、“$last_name”、“$email”、“$date_stamp”、“$message”);
if(mysql_查询($sql)){
$content=“您的邮件已发送。/n单击以继续
返回主页。“;
}否则{
$content=mysql\u error().$date\u stamp。'无法发送您的邮件。请重试
后来",;
}
}
您还可以进行其他优化,但希望这能解决您的问题

您还可以将Google Chrome与此插件结合使用,模拟移动设备:


然后打开Chrome开发者工具,开始调试乐趣

我关闭了页面加载的AJAX功能,现在它可以正常工作了,正如我所怀疑的那样。这可能是另一个原因,但目前还有效。我确实失去了光滑的过渡效果,但哦,好吧

把这个贴在每一页的头上

$(document).bind("mobileinit", function(){
        $.mobile.ajaxenabled = false;
    });

我试过这个,效果很好。在我的屏幕上,我添加了一个target=“\u blank”,它使它在一个新窗口中弹出,我不再得到未定义的错误。这就是我的代码看起来的样子,它可以在Chrome for Android fine上运行。它需要一个新窗口来显示PHP脚本生成的HTML,包括错误代码



请不要使用
mysql.*
函数编写新代码。它们不再得到维护,社区已经开始。看到了吗?相反,您应该学习并使用[PDO](httpI不想让你误解这一点,但你读了整篇文章吗?我在最后一段提到了这一点。这与我提出的问题无关。尽管这不是一个最佳实践,但代码仍然有效。我的皮肤很厚,我只是在为他人发表评论。好吧,酷,我看到有人被指控燃烧火焰我很感激你的关注,你是对的,但是正如我在上面对Mike的评论中所说的,我提到我将进行重构。这只是我对JQM的测试,所以我的问题不是关于PHP。我相信这与JQM想调用AJAX来获得新页面这一事实有关,但是正如我所说,我是JQM新手,所以可能我对功能的理解是错误的。