【问题标题】:Are two functions (one with php) possible with "onclick"?“onclick”可以实现两个功能(一个带有php)吗?
【发布时间】:2013-02-14 12:09:29
【问题描述】:

我有以下脚本可以同时触发两个函数。一个函数应调用/加载 php 文件(index.php 以查询 mysql),另一个应运行 javascript。这甚至可能吗?提前感谢您的帮助。

$.get("index.php", {id : searchform, name : tweetfinder}, function() {
// Here you can do the other operations
onclick("submit");
$('searchform').submit();
});
</script>
<form id="searchform" name="tweetfinder">
<div class="input-append">
<input class="span3" autofocus="autofocus">
<button class="btn btn-primary">Do Stuff!</button>
</div></form>

这个方法怎么样(输入mysql查询作为函数?):

function showstuff(){
<?php
$get = mysql_query("SELECT * FROM `xxx` WHERE `xxx` = '$xxx' ORDER BY `rank` DESC")or die(mysql_error());
while($ass = mysql_fetch_assoc($get)){
$viewer = $ass['playername'];
echo "<a href=\"index.php?viewuser=".$viewer."\">".$viewer."</a> , ";
};

【问题讨论】:

  • 不,你不能用javascript调用PHP函数,你需要了解客户端和服务器端脚本的区别
  • 加载新网址提交表单相互矛盾。请解释一下您要达到的目的。
  • 这取决于你对同时的定义...
  • 谢谢大家。我想同时访问一个数据库并执行一个 javascript。嗯,作为一个菜鸟,要做到这一点比我想象的要难。所以我想回到学校:) @bfavaretto 这个简短的回答有帮助吗?
  • 我仍然不确定您到底想做什么。但如果下面建议的 ajax 解决方案不起作用,您可以编辑您的问题以添加更多详细信息。

标签: php javascript forms function


【解决方案1】:

如果你愿意你的 AJAX (jQuery) 那么我想建议。你做什么,使用 AJAX 调用获取另一个文件的数据,并在其回调中执行其他操作。

$.get("index.php", {id : 5, name : MyName}, function() {
  // Here you can do the other operations
  alert('Do more things');
  $('formID').submit();
});

更多详情请参考here

【讨论】:

  • 非常感谢。我会立即尝试。
  • 经过 2 小时的尝试,我无法做到正确。我将问题中的代码编辑为当前问题...对我来说还有很长的路要走...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 2013-06-16
相关资源
最近更新 更多