0

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:

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

2
  • I don't understand your question. Seems like it should work just like you're doing it. Redirect the user to the page reservation.php and add the variables in the url so you can access them from php $_GET[] Commented Apr 11, 2011 at 10:06
  • There's a space in your window.location, is that on purpose? Commented Apr 11, 2011 at 10:06

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :) I did not know it would be that simple
1

try this..

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.