简体   繁体   English

如何使用来自javascript的onclick事件链接到另一个php页面?

[英]How to link to another php page using onclick event from javascript?

I am currently having issues with making buttons based on an array of information on customers. 我目前在根据客户的一系列信息制作按钮时遇到问题。 I have made the buttons in the following way: 我按以下方式制作了按钮:

    foreach($array as $row)
    {
            echo (
            "<tr>".
            "<td>".$row['last_name'].       "</td>".
            "<td>".$row['first_name'].  "</td>".
            "<td>".$row['phone_no'].        "</td>".
            "<td>".$row['date_of_birth'].   "</td>".
            "<td>".$row['membership'].  "</td>".
            "<td><Button class='resultBookButton' data-lastName=".$row['last_name']." data-firstName=".$row['first_name']." data-phone=".$row['phone_no'].">Reserve</Button></td>".
            "</tr></table>");
    }

Above my PHP script I have the following: 在我的PHP脚本之上,我有以下内容:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // Handling search results
    $('.resultBookButton').click(function(e){
        // Get the name of the user clicked
        var lastName = $(this).data("lastName");
        var firstName = $(this).data("firstName");
        var phoneNum = $(this).data("phone");

        window.location = "ss reservation.php?lastName=" + lastName + "&firstName=" + firstName + "&phoneNum=" + phoneNum;

    });
});
</script>

What I want to achieve is being able to open a php page with the parameters lastName, firstName, and phoneNum. 我想要实现的是能够打开一个带有参数lastName,firstName和phoneNum的php页面。 I have not seen any GET methods on how to do this in javascript except for AJAX. 我没有看到任何关于如何在javascript中执行此操作的GET方法,除了AJAX。 But ajax keeps you on the same page, and is not what I am looking for. 但是ajax让你在同一页面,而不是我想要的。

Any suggestions? 有什么建议么?

尝试这个..

window.location.href = "ss reservation.php?lastName=" + lastName + "&firstName=" + firstName + "&phoneNum=" + phoneNum;

Why do you need JavaScript at all for this? 为什么你需要JavaScript呢? Why don't you create a normal link which also works if JavaScript is disabled? 为什么不创建一个普通的链接,如果JavaScript被禁用也可以使用?

<?php foreach($array as $row): ?>
    <tr>
        <td><?php echo $row['last_name'] ?></td>
        <td><?php echo $row['first_name'] ?></td>
        <td><?php echo $row['phone_no'] ?></td>
        <td><?php echo $row['date_of_birth'] ?></td>
        <td><?php echo $row['membership'] ?></td>
        <td><a class='resultBookButton' 
               href="<?php printf("ssreservation.php?lastName=%s&firstName=%s&phoneNum=%s", $row['last_name'], $row['first_name'], $row['phone_no']) ?>"
            >Reserve</a>
        </td>
    </tr>
<?php endforeach; ?>

If you want the appearance of a button, you can use CSS to style the link as button: 如果您想要按钮的外观,可以使用CSS将链接设置为按钮的样式:

a {
    color: black;
    padding: 5px;
    border: 2px outset lightgrey;
    background-color: lightgrey;
    text-decoration: none;
}

a:active {
    border-style: inset;
}

DEMO DEMO

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

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