Al Hennessey
Al Hennessey

Reputation: 2445

Using div to redirect to php script

i have this div that i am using as a sort of button, most of the time, i just use it to redirect to another page, but this time, i want it to run some php before it redirects to the next page.

<div class="build_detail_option_ele_wrap" onclick="location.href="builders_checkout.php";"></div>

Could i just make it redirect to builders_checkout.php, which creates some sessions etc... and then from there redirect to the end page.

(I know i could do this easier through using a submit button instead, but circumstances mean i can't use one here. )

Is this a good idea, or are there negatives to doing this?

Thanks

Upvotes: 0

Views: 1304

Answers (6)

honyovk
honyovk

Reputation: 2747

If I am right in what I think you're asking, you might want to consider using AJAX.

jQuery makes this easy:

$(".build_detail_option_ele_wrap").on('click', function () {
    // Do your $_SESSION (or other) stuff
    $.get('builders_checkout.php', function () {
        // after the $.get has completed:
        window.location.href = 'lol_page.php';
    })
});

Your DIV:

 <div class="build_detail_option_ele_wrap"></div>

This way, you run "builders_checkout.php" asynchronously, then load the "lol_page.php" URL without having to redirect twice.

Upvotes: 1

Marcelo Assis
Marcelo Assis

Reputation: 5194

Pass a querystring on your url containing the page you should redirect after that script. I used next.

 ... builders_checkout.php?next=lol_page.php ...

In your builders_checkout.php, use something like this in the end of your script:

// if next is set, we will redirect it to next value (lol_page.php);
if(isset($_GET['next'])){
     header('Location: ' . $_GET['next']);
}

If you any question or problem to understand that, just ask.

Upvotes: 2

Mike Brant
Mike Brant

Reputation: 71384

Would you are suggesting doing should work fine, however you need to change the double-quotes inside the onclick declration to single quotes like this:

<div class="build_detail_option_ele_wrap" onclick="location.href='builders_checkout.php';"></div>

If you want to get more advanced, you could look at using AJAX methods to send the data you need to the PHP script and then redirect only if you have success in doing whatever that script needs to do (or even have that script return a string indicating where the browser should be redirected to).

Upvotes: 1

Eric
Eric

Reputation: 97555

Since no one has actually given the (obvious) solution:

<a class="build_detail_option_ele_wrap" href="builders_checkout.php"></a>

Add this to your CSS, and it won't be visibly different:

a.build_detail_option_ele_wrap {
    text-decoration: none;
    color: inherit;
    display: block;
}

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

There's nothing "wrong" with it, but you'd have better results if you used an <a href="..."> tag (optionally, set style="display:block" to make it behave like a <div>).

If you are insistant on a <div>, use single quotes around the builders_checkout.php string.

Upvotes: 2

Osho
Osho

Reputation: 51

Well, implement it using jQuery (or even plain JS) instead of onclick="". And of course, uses an <a href=""> instead of div

So far, there's no problem. Just make sure to let the user know that it's processing with a loading bar.

Upvotes: 1

Related Questions