开发者

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 i开发者_运维技巧nformation 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:

<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. I have not seen any GET methods on how to do this in javascript except for AJAX. But ajax keeps you on the same page, and is not what I am looking for.

Any suggestions?


Why do you need JavaScript at all for this? Why don't you create a normal link which also works if JavaScript is disabled?

<?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:

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

a:active {
    border-style: inset;
}

DEMO


try this..

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜