【问题标题】:onclick fetch data and insert into textarea [closed]onclick 获取数据并插入到 textarea [关闭]
【发布时间】:2016-04-14 06:11:57
【问题描述】:

我想获取分配给某个组的所有联系人行,例如“Mumbai2015”。在撰写电子邮件页面上,我会列出用户拥有的所有组,并且还希望当用户单击特定组时,所有分配的联系人都应插入以逗号(,)分隔的文本区域。

我想在不刷新浏览器的情况下通过 onclick 事件完成此操作。这可能吗?

<script>
function getFunction(str)
{
var thought = jQuery("textarea#recemail").val();
var getemail = "NULL";
<?php 
$phpvar='"+str+"';
$txtcont = '"+thought+"';                 
$ppwp = $phpvar;
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$usern = $_SESSION['login_user'];
$sql2 = "select GROUP_CONCAT(email SEPARATOR ',') allemail from contacts,groups where groups.grpname=contacts.grp and contacts.user='".$usern."' and contacts.level=2 and groups.ID='".$ppwp."';";
$retval2 = mysqli_query($con,$sql2);
$row2 = mysqli_fetch_array($retval2);
$row_cnt = mysqli_num_rows($retval2);
if($row_cnt > 0) {
$getemail = $row2['allemail'];
echo 'var getemail = "'.$getemail.'";';
}
?>
return getFunction2(getemail);
}
function getFunction2(getemail)
{
var thought = jQuery("textarea#recemail").val();
document.getElementById("recemail").innerHTML= thought+getemail+",";
}</script>

【问题讨论】:

  • 借助屏幕截图向我们展示预期输出的样本数据。也显示你的努力。
  • 公开你的代码人...
  • @uzaif 已编辑.. :)
  • 查询工作正常(在mysql上检查),连接也正常..
  • ohh js 和 php 混合太多问题了,这个尝试分开吧

标签: javascript php html mysql textarea


【解决方案1】:

分离你的 JS 和 PHP 代码。 将您的 PHP 放在服务器上的单独 php 文件中,并使其返回 JSON object for example

在您的 Javascript 中,这次是在客户端,使用 $.get 调用您的 php 脚本,并在您的 javascript 代码中,在 $.get 回调函数中处理返回的 JSON。

您将通过这种方式使用的技术称为 AJAX https://en.wikipedia.org/wiki/Ajax_(programming).

【讨论】:

  • 我已经考虑过了,已经开始努力了。谢谢。。
  • 我现在在一个单独的 PHP 页面中获得完整的结果集,但是现在我如何获得这个结果并插入到 textarea 中? (并在 Ajax 中创建一个 get 请求)
  • 我放了文档的链接,关注他们,解释得很好。
猜你喜欢
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 2023-03-29
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
相关资源
最近更新 更多