简体   繁体   English

如何将按钮的PHP变量onclick发送给javascript函数作为参数

[英]How to send php variable onclick of button to javascript function as parameter

I am using codeigniter 3. Below is the code of the view page, where data inside the $row->poll_question; 我正在使用codeigniter3。下面是视图页面的代码,其中$ row-> poll_question中的数据; is coming from the database. 来自数据库。 Thus, the different values coming inside this variable are echoed on a page called voting.php. 因此,此变量内的不同值会在名为voting.php的页面上回显。 And just beside this variable, there is a button called View Poll. 在此变量旁边,有一个名为“查看投票”的按钮。 Now, on clicking this button I want to pass the data of this php variable to javascript section. 现在,单击此按钮,我想将此php变量的数据传递给javascript部分。 And simply alert and check in javascript. 并且只需警报并签入javascript。 Please help. 请帮忙。 I have pasted the code I tried. 我已经粘贴了我尝试过的代码。

voting.php 投票.php

<h3 class="val" id="val" style="color:rgb(158, 158, 158);font-family: 
arial;margin-top: 2%;" ><?php echo $row->poll_question; ?></h3>
</td>

<td>

<a class="btn btn-lg btn-primary view_button" id="view_button" 
 style="margin-left: 18%;padding:10%;border-radius: 10px;" 
 onclick="fetch_val(<?php echo $row->poll_question ?>)">View Poll</a>

Javascript Java脚本

function fetch_val(val)
{
     alert(val);
}

You have forgot to add quote on '<?php echo $row->poll_question ?>' . 您忘记在'<?php echo $row->poll_question ?>'上添加quote Check updated code below 检查下面的更新代码

<h3 class="val" id="val" style="color:rgb(158, 158, 158);font-family: 
arial;margin-top: 2%;" ><?php echo $row->poll_question; ?></h3>
</td>

<td>

<a class="btn btn-lg btn-primary view_button" id="view_button" 
 style="margin-left: 18%;padding:10%;border-radius: 10px;" 
 onclick="fetch_val('<?php echo $row->poll_question ?>')">View Poll</a>

I don't recommend the redundant storage of the string. 我不建议该字符串的冗余存储。

You already have the value in your id 'ed <h3> tag. 你已经在你的价值id “编<h3>标记。 Just use that. 只是使用那个。

Further advice move all stylings to an external stylesheet and use a javascript listener on your #view_button . 进一步的建议将所有样式移至外部样式表,并在#view_button上使用javascript侦听#view_button

You could stay with an inline function call: 您可以使用内联函数调用:

<h3 class="val" id="val"><?php echo $row->poll_question; ?></h3>

<a class="btn btn-lg btn-primary view_button" id="view_button" 
   onclick="fetch_val(document.getElementById('val').innerHTML);">View Poll</a>

Or trigger fetch_val() via a listener written in your javascript (this is the cleaner / more professional way): 或通过用JavaScript编写的侦听器触发fetch_val() (这是一种更清洁/更专业的方式):

document.getElementById('view_button').onclick = function() {
    alert(document.getElementById('val').innerHTML);
}

About writing a pure javascript onclick handler: https://stackoverflow.com/a/17633164/2943403 关于编写纯JavaScript onclick处理程序: https : //stackoverflow.com/a/17633164/2943403

Assigning a custom function to an event: https://stackoverflow.com/a/24665785/2943403 为事件分配自定义功能: https//stackoverflow.com/a/24665785/2943403

Using either of my above techniques will remove the possibility of quoting issues caused by questions like What is your mother's maiden name? 使用上述两种方法中的任何一种,都将消除引用诸如What is your mother's maiden name?等问题引起的问题的可能性What is your mother's maiden name? .

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

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