[英]I am geting “undefined” with this javascript. Why?
I have been digging around for 4 hours now and I decided to ask, as I can't get my head around this. 我现在已经挖了4个小时了,我决定问,因为我无法理解这个问题。
I have the following in one page: 我在一页中有以下内容:
support.php: support.php:
<div id="userID"><?= $dataOwner =></div>
<a class="nav-link" href="JavaScript:void(0);" id="showOpen">Open</a>
<script src="js/tickets.js></script>
In the above page (support.php), $dataOwner returns a value correctly. 在上面的页面(support.php)中,$ dataOwner正确返回一个值。 When clicked, the link must call the "js/tickets.js" script in which I have the following: 单击时,链接必须调用“js / tickets.js”脚本,其中包含以下内容:
tickets.js: tickets.js:
$("#showOpen").click(function(){
$.ajax({
type: "POST",
url: 'https://[redacted]/load_tickets.php?user='+$('#userID').val(),
success:function(data) {
document.getElementById('openTickets').innerHTML = data;
}
});
});
but the value of #userID is always undefined. 但#userID的值始终未定义。 Why is this and what am I doing wrong? 为什么这样,我做错了什么?
firstly, don't use shorthand in php - might cause errors when moving servers at any point. 首先,不要在php中使用速记 - 在任何时候移动服务器时可能会导致错误。
secondly.. no inline JS!!! 其次..没有内联JS !!!
and lastly, you're using url params for a post method. 最后,你使用url params作为post方法。
How I'd do it (and how I'd recommend): 我该怎么做(以及我如何推荐):
[support] [支持]
<div id="customer" data-id="<?php echo $dataOwner; ?>">
<?php echo $dataOwner; ?>
</div>
<a href="#" class="nav-link" id="showOpen">Open</a>
<script>
$(document).ready(function()
{
$('#showOpen').on('click', function()
{
$.ajax({
data: {user: $('#customer').data('id')},
dataType: 'json',
type: 'post',
url: '/your/path/to/load_tickets.php',
success: function(data)
{
$('#openTickets').html(data)
}
});
});
});
then no need for tickets.js
(in this case) 那么就不需要tickets.js
(在这种情况下)
this will post the data to the file as $_POST
(array) where you can use $_POST
however you want :) 这会将数据作为$_POST
(数组)发布到文件中,你可以使用$_POST
但是你想要:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.