简体   繁体   English

我该怎么做 <p> 标签可点击通过使用PHP弹出?

[英]How can i make every <p> tag clickable on a pop-up by using php?

as you can see my code is working with sql. 如您所见,我的代码正在使用sql。 Every email name will be shown on a "pop up". 每个电子邮件名称将显示在“弹出窗口”上。 And all of them will have separated p tag. 并且所有的人都有单独的p标签。 What i am trying to make is when users click this p tag they must access another page. 我想做的是,当用户单击此p标签时,他们必须访问另一页。 I am new to php and did something wrong . 我是php新手,做错了什么。 Some help would be great. 一些帮助将是巨大的。 Thanks. 谢谢。

<div id="dialog" title="Following">
  <?php
  $x=0;
  $arrayName = array();
  $sqls = "SELECT * FROM follow WHERE member_email='$email'";
  $result = mysqli_query($conn,$sqls);
  while($row = mysqli_fetch_assoc($result)) {
    $arrayName[$x]  = $row["person_email"];

    $x=$x+1;
  } ?>
  <?php for($k = 0; $k < $x; $k++) {?>
    <p id="pop" value="$arrayName[$k]" onclick="popFunc(this.value)"><?php echo  $arrayName[$k]; ?></p>

  </div>
/////////////////////////////////////////////////////////////////

function popFunc($element){

$_SESSION['visiter']=$element; 
document.location.href = 'http://localhost/example/visiter.php';

}

The onclick() in this line 此行中的onclick()
<p id="pop" value="$arrayName[$k]" onclick="popFunc(this.value)"><?php echo $arrayName[$k]; ?></p>

it means call the function of javascript. 这意味着调用javascript函数。 And better you change <p> to be <a> tag. 最好将<p>更改为<a>标记。 So the full script like this: 因此,完整的脚本是这样的:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>

function popFunc(name){
alert(name);
}

</script>
<div id="dialog" title="Following">
  <?php
  $x=0;
  $arrayName = array();
  $sqls = "SELECT * FROM follow WHERE member_email='$email'";
  $result = mysqli_query($conn,$sqls);
  while($row = mysqli_fetch_assoc($result)) {
    $arrayName[$x]  = $row["person_email"];

    $x=$x+1;
  } ?>
  <?php for($k = 0; $k < $x; $k++) {?>
    <a id="pop" href="#" onclick="popFunc(<?= $arrayName[$k] ?>)"><?php echo  $arrayName[$k]; ?></a>

  </div>

Hope it can help you ;) 希望它可以帮助您;)

You cannot mix Javascript and PHP in a function. 您不能在函数中混合使用Javascript和PHP。 Here is a solution 这是一个解决方案

<div id="dialog" title="Following">
  <?php
  $x=0;
  $arrayName = array();
  $sqls = "SELECT * FROM follow WHERE member_email='$email'";
  $result = mysqli_query($conn,$sqls);
  while($row = mysqli_fetch_assoc($result)) {
    $arrayName[$x]  = $row["person_email"];

    $x=$x+1;
  } ?>
  <?php for($k = 0; $k < $x; $k++) {?>
    <p id="pop" onclick="popFunc('<?php echo  $arrayName[$k]; ?>')"><?php echo  $arrayName[$k]; ?></p>

  </div>
<?php } ?> 
/////////////////////////////////////////////////////////////////
<script>
function popFunc(element){
document.location.href = 'http://localhost/example/visiter.php?visiter='+element;
}
</script>

And your visiter.php: 和您的visiter.php:

Replace $_SESSION['visiter'] with $_GET['visiter'] $_SESSION['visiter']替换$_SESSION['visiter'] $_GET['visiter']

What you are doing is not correct, you are mixing Javascript with PHP. 您所做的不正确,您正在将Javascript与PHP混合使用。

This onclick="popFunc(this.value)" will try to run JavaScript code, that is not defined in your code. onclick="popFunc(this.value)"将尝试运行您的代码中未定义的JavaScript代码。

PHP is a Server side code, and JavaScript is a Client side code. PHP服务器端代码,而JavaScript客户端代码。 I am not very knowledge at JavaScript, so I cant help you there, but you can search for jQuery and JavaScript tutorials. 我对JavaScript不太了解,因此我无法为您提供帮助,但是您可以搜索jQueryJavaScript教程。

PHP will run always before HTML/JS/CSS because is Server side. 由于服务器端, PHP将始终在HTML / JS / CSS之前运行。 So, if you want to get data without changing page, you must use AJAX . 因此,如果要获取数据而不更改页面,则必须使用AJAX This is not hard, but you require to know JavaScript. 这并不难,但是您需要了解JavaScript。

Here is a good tutorial to help you. 是一个很好的教程来帮助您。


EDIT 编辑

I wrongly read the code on p tag. 我错误地阅读了p标签上的代码。 He is mixing HTML and PHP, but I didnt see that he wrongly called the popUp function. 他正在混合HTML和PHP,但是我没有看到他错误地调用了popUp函数。 He done: 他做了:

<p id="pop" value="$arrayName[$k]" onclick="popFunc(this.value)"><?php echo  $arrayName[$k]; ?></p>

And what is correct 什么是正确的

<p id="pop" onclick="<?php popFunc($arrayName[$k]); ?>"><?= $arrayName[$k]; ?><?= $arrayName[$k]; ?></p>

And his function: 而他的功能:

function popFunc($element){

    echo "window.location = 'http://localhost/example/visiter.php?element=$element'";

}

The value property only exists on elements that take user input. value属性仅存在于需要用户输入的元素上。 You should use a data-* attribute to add arbitrary data to other types of elements. 您应该使用data-*属性将任意数据添加到其他类型的元素。 Or you could simply put the value directly into the popfunc() argument list. 或者,您可以直接将值直接放入popfunc()参数列表中。 Or, since the value is also in the text of the paragraph, you can use this.textContent , which is what I've done in my answer. 或者,由于该值也在该段落的文本中,因此您可以使用this.textContent ,这是我在回答中所做的。

You also should have the definition of popfunc() outside the loop, it doesn't need to be redefined for each item. 您还应该在循环外定义popfunc() ,不需要为每个项目都重新定义它。

<?php for($k = 0; $k < $x; $k++) {?>
    <p id="pop" onclick="popFunc(this.textContent)"><?php echo  $arrayName[$k]; ?></p>
<?php }

However, your popFunc function can't work as written. 但是,您的popFunc函数不能按书面形式工作。 It's a Javascript function that runs on the client, it can't set PHP variables like $_SESSION . 它是在客户端上运行的Javascript函数,无法设置$_SESSION类的PHP变量。 If you want to do something on the server when the user clicks, you need to use AJAX. 如果要在用户单击时在服务器上执行某些操作,则需要使用AJAX。 Or you could pass it as a parameter when redirecting to the new location. 或者,您可以在重定向到新位置时将其作为参数传递。

function popFunc(element) {
    document.location.href = 'http://localhost/example/visiter.php?visitor=' + encodeURIComponent(element);
}

Then visitor.php can assign to the session variable with: 然后visitor.php可以通过以下方式分配给会话变量:

$_SESSION['visitor'] = $_GET['visitor'];

Try this: 尝试这个:

<div id="dialog" title="Following">
   <?php
       $x=0;
       $arrayName = array();
       $sqls = "SELECT * FROM follow WHERE member_email='$email'";
       $result = mysqli_query($conn,$sqls);
       while($row = mysqli_fetch_assoc($result)) {
           $arrayName[$x]  = $row["person_email"];
           $x=$x+1;
   } ?>
   <?php for($k = 0; $k < $x; $k++) {?>
       <p id="pop" onclick="javascript:window.open('<?php echo $arrayName[$k]; ?>', '_blank');">
       <?php echo  $arrayName[$k]; ?></p><?php } ?>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM