表单提交在$(document).ready(function(){。是否有一个简单的修复?

时间:2012-02-24 04:14:59

标签: form-submit document-ready

对于合适的大师来说,这应该是一个简单的解决方案!一切都在为我工作,除了我无法单击提交按钮提交此表单。数据值都是有效的。操作页面,... gdform.php,使用$ _post从表单中获取“重定向”值,然后使用php进行标题位置更改。如果我使用提交按钮执行表单,那该工作正常。我只是需要它没有任何点击发生...
请看一下!

<?php session_start();
require_once('Connect.php') ;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Nitrofill Document</title>

<?php
//error_reporting(E_ALL);
$sn=$_GET['num'];
echo $sn;

mysql_connect($hostname,$username, $password) OR die('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
 $selectSQL = "select * from `Presentations` where `serialnum` ='" . $sn ."'" ;

$result = mysql_query($selectSQL) or die(mysql_error());
$row = mysql_fetch_array($result,  MYSQL_BOTH);
$thedoc = urldecode($row['docurl']);
$therecip=urldecode($row['recipient']);
$thetracker=urldecode($row['tracker']);
$lastacc=urldecode($row['last_accessed']);

?>
</head>
<body>

<form id="notice" action="http://m3sglobal.com/gdform.php" method="post"> 
<input  name="subject" value="<?php echo $therecip . " has viewed the document you sent them.";?> " /> 
<input type="hidden" name="redirect" value="<?php echo $thedoc ; ?>"/>
<label>Email:</label><input type="text" name="email" value="<?php echo $thetracker ; ?>"/>
<label>Comments:</label><textarea name="comments" cols="40" rows="5">
Document Viewed:<?php echo $thedoc ; ?>

When Accessed:<?php echo $lastacc ; ?>
</textarea>
<input type="submit" name="submit"/>
</form>
</body>

</html>
<script type="text/javascript">

 $(document).ready(function(){
   myfunc
 });

function myfunc () {
var frm = document.getElementById("notice");
frm.submit();
}

</script>

2 个答案:

答案 0 :(得分:0)

我可能错了,但不应该是:(更正错别字)

$(document).ready(function(){
   myfunc(); <--// with ();
}); 

function myfunc() {  <--// without space
    var frm = document.getElementById("notice");
    frm.submit();
}

或更好:

$(document).ready(function(){
   $("form#notice").submit();
});

修改

Prowla是对的,你也没有声明jQuery库。很好的抓住Prowla,我错过了,只是看到了错别字。

编辑#2:

你的代码非常混乱,你在<head>中有PHP生成的字符串。您的提交也没有value,您使用了name。我清理了它,这是工作代码(至少对我而言):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nitrofill Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("form#notice").submit();
    });
    </script>
</head>
<body>
<!--// ALL THE PHP SHOULD GO HERE TO MAKE THE URL BELOW //-->
qyO452ZKphttps://docs.google.com/presentation/pub?id=1chxqg-qjrfEvAR9_Jia7lt4ps2_Q7IfTiI41bQE7Q_4&start=true&loop=false&delayms=3000<br/>greg.mcgee@gmail.com<br/>greg.mcgee@advetel.com<br/>Thu, 23 Feb 2012 19:42:11 MST<br/>
<!--// END PHP //-->

<form id="notice" action="http://m3sglobal.com/gdform.php" method="post"> 
    <input name="subject" value="greg.mcgee@gmail.com has viewed the document you sent them. " /> 
    <input type="hidden" name="redirect" value="https://docs.google.com/presentation/pub?id=1chxqg-qjrfEvAR9_Jia7lt4ps2_Q7IfTiI41bQE7Q_4&start=true&loop=false&delayms=3000"/>
    <label>Email:</label><input type="text" name="email" value="greg.mcgee@advetel.com"/>
    <label>Comments:</label><textarea name="comments" cols="40" rows="5">
        Document Viewed:https://docs.google.com/presentation/pub?id=1chxqg-qjrfEvAR9_Jia7lt4ps2_Q7IfTiI41bQE7Q_4&start=true&loop=false&delayms=3000
        When Accessed:Thu, 23 Feb 2012 19:42:11 MST
    </textarea>
    <input type="submit" value="submit"/>
</form>
</body>
</html>

您可能需要考虑使用firebug来帮助您排查网页问题。我是如何理解这一点的。还要记住Prowla的建议,并保护你的SQL。

答案 1 :(得分:0)

使用jQuery :(记得将jquery脚本放在<head>标签中)。

 <head>
 ...
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
 ...
 <head>
 ...
 // submit form with id notice
 $(document).ready(function(){
     $('#notice').submit();
 });

此外,您的SQL还需要注入。请查看使用准备好的陈述。 PHP中的示例:

$mysqli = new mysqli('localhost', 'user', 'password', 'db');
$stmt = $mysqli->prepare('select * from `Presentations` where `serialnum` =?');
$stmt->bind_param('i',$sn);
$stmt->execute();
....