如何在php中编写javascript代码 [英] how to write javascript code inside php
本文介绍了如何在php中编写javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单,点击提交按钮:
i have a got a form, on clicking the submit button:
- 我想在同一个文件中执行一些任务(db任务)AND
- 我希望表单数据通过重定向发送到test.php
< ($ set)($ _ POST ['btn'])这是我的代码
here is my code
<?php
if(isset($_POST['btn'])){
//do some task
?>
<script type="text/javascript">
var e = document.getElementById('testForm'); e.action='test.php'; e.submit();</script>
<?php
}
?>
<form name="testForm" id="testForm" method="POST" >
<input type="submit" name="btn" value="submit" autofocus onclick="return true;"/>
</form>
但是无法提交表单,如果我在onClick上调用javascript代码,是这个代码中的问题,是否有任何解决此问题的方法
but not able to submit the form, if i call the javascript code on onClick, it works.what is the problem in this code, Is there any work around for this
推荐答案
<form name="testForm" id="testForm" method="POST" >
<input type="submit" name="btn" value="submit" autofocus onclick="return true;"/>
</form>
<?php
if(isset($_POST['btn'])){
echo "
<script type=\"text/javascript\">
var e = document.getElementById('testForm'); e.action='test.php'; e.submit();
</script>
";
}
?>
这篇关于如何在php中编写javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文