-1

I am creating a table of my services or products where each product have their id next to it.
I want to grab that product id into the URL of buy now button when customer click on that button, and when customer have done their shopping , that custom id should return back to my site within success URL.

img example
any idea how can I do this?

many thanks in advance.

2
Contributors
1
Reply
33
Views
3 Years
Discussion Span
Last Post by mmcdonald
0

Now unless I misunderstand the question, this is quite easy to achieve. Each row should have a form and each buy now button should be a form submission button for the row. I personally would do this with jQuery, wrap the whole table in a form, add a custom attribute to each pay now button and .serialize() the submission.

Also, this isn't a 'We give away solutions for nothing' website, you should have given this a go yourself and posted the code from your attempt.

Anyhow seeing as you requested the solution in PHP you can achieve this by accessing the global $_POST array after posting from a HTML form. An example of this is below.

<?php

    if(isset($_POST['buyPlan'])){

        $filterPlan = preg_replace("/[^0-9]/","",$_POST['planId']);
        header('Location: member.php?active_plan='.$filterPlan);

    }

?>

<table>
    <form action="" method="POST">

        <tr>
            <td>
                <input type="hidden" name="planId" value="1" />
                1
            </td>
            <td>
                <button type="submit" name="buyPlan">Buy Now</button>
            </td>
        </tr>
    </form>
</table>

Now this is a poor but working example - you don't get it all for nothing. In the example we collect the id of the plan from the hidden input field and submit the value of that field in a POST to the same file identified by the fields name. We then use the PHP form handler at the top to collect the value of $_POST['planId'], filter the plan id to make sure your visitor hasn't changed the value to something dangerous before submitting the form, and then finally use the header() to redirect to members.php with the query string you wanted.

To explain the filtering part, we don't want a user trying to POST code, so we've used the preg_replace and a regex (regular expression) to remove everything from the variable except numbers. If you plan to submit text or characters then don't use that regex as it will remove them!

Figure it out, improve on it and post back :)

Michael

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.