Jump to content

Putting logic behind Button using PHP?


doubledee

Recommended Posts

Can I put logic behind a form button using PHP?

 

I have a Comments form with two buttons: 'Log In' and 'Create an Account'

 

If the user clicks on 'Log In' I would like to set one SESSION variable and then branch off in one direction.

 

If the user clicks on 'Create an Account' then I would like to set a different SESSION variable and branch off into a different direction.

 

Is that possible using solely PHP?

 

 

 

Debbie

 

 

Link to comment
Share on other sites

You simply need to have the button direct to two different php scripts. Or send two different variables via get.

 

PHP executes on the server, so you need to make a request to it before you can run any php.

Link to comment
Share on other sites

You simply need to have the button direct to two different php scripts. Or send two different variables via get.

 

PHP executes on the server, so you need to make a request to it before you can run any php.

 

Trying to figure out a problem since this weekend.

 

Not entirely sure what the problem is or how to solve it?!  :-[

 

Let me try to explain...

 

A user is on "article1234.php".

 

If the user (a Member who is not logged in) clicks on 'Log In' then I want to set $_SESSION['returnToPage'] = '/path/to/article1234.php' and then send them to "log_in.php"

 

If the user (a Visitor) clicks on 'Create an Account' then I want to set $_SESSION['returnToPage'] = '/path/to/article1234.php' and then send them to "create_account.php"

 

In the 2nd scenario, the user will go through several steps, become a Member, and ultimately end up at "log_in.php" as well.

 

Once the Member arrives at "log_in.php", and successfully logs in, they should be redirected to $_SESSION['returnToPage'].

 

(I tried this another way this weekend and got wickedly confused?!)

 

 

Debbie

 

 

Link to comment
Share on other sites

My first reply still applies unless you want to go down the Ajax path.

 

I didn't follow you.

 

Based on what I typed, can I just set $_SESSION['returnToPage'] = '/path/to/article1234.php' and then let the code do the rest...

 

<ul>
<li>
	<a href="<?php echo WEB_ROOT ?>sign_in.php">
		<img src="<?php echo WEB_ROOT ?>buttons/SignIn2.png" alt="Sign In" />
	</a>
</li>
<li>or</li>
<li>
	<a href="<?php echo WEB_ROOT ?>create_account.php">
		<img src="<?php echo WEB_ROOT ?>buttons/CreateAnAccount.png" alt="Create an Account" />
	</a>
</li>
</ul>

 

 

I guess I was thinking like back in my Visual Basic days of...

 

OnClick
<do something 1>
<do something 2>
<do something 3>

 

 

Debbie

 

 

Link to comment
Share on other sites

doubleDee, your making too complicated on yourself here. If you want to redirect the user

 

just use

 

<?php
$path  = $_SESSION['returntopage'];
header('location:'.$path);
?>

 

if you want to do that "behind" the scenes without a page refresh you need to look at Ajax.

Link to comment
Share on other sites

doubleDee, your making too complicated on yourself here. If you want to redirect the user

 

just use

 

<?php
$path  = $_SESSION['returntopage'];
header('location:'.$path);
?>

 

if you want to do that "behind" the scenes without a page refresh you need to look at Ajax.

 

 

Let me explain what I am trying to do...

 

My website has articles.  I want people to add comments, but require users to be logged in Members.

 

If you are on an article page, and the page doesn't detect that you are a Member and logged in, then it displays "To add a comment you must 'Log In' or 'Create an Account'"

 

In either scenario, I want people to land back on the page they were at, so that after the Register/Log-In they can add a comment to the original article they were reading!!

 

If a Member is on "article1234.php" and clicks 'Log In' then after they log in I want them re-routed back to "artcicle1234.php"

 

If a Visitor is on "article1234.php" and clicks on 'Create an Account', they will e taken though several screens and have to activate their account by click on a link in an e-mail but ultimately they will end back at "log_in.php" at which point if the successfully log in I want them to be re-routed to "article1234.php"

 

Follow me?

 

This weekend I was trying to look back and capture where they were coming from in "create_account.php", but that cause an issue because I needed something similar in "log_in.php" which had a different referring page.

 

So tonight I thought it *might* be better to set where the user is at in a $_SESSION *before* I send them off to either "log_in.php" or "create_account.php"

 

Still with me?

 

The only down side to this new approach is that I have to add code to very single article page which is overhead Iw as trying to avoid.

 

 

Debbie

 

 

Link to comment
Share on other sites

Thats the only way you can do it in PHP. Register the returnToPage session on every page except the create account and login pages, thats all you can do.

 

Is it a problem coding things that way?

 

What I am doing is in essence always saving the current articles name and path in $_SESSION and then if and when someone gets to "log_in.php" I am re-directing them to $_SESSION['returnToPage'];

 

Is that okay?

 

 

Would it be better to somehow capture the Referring page in "create_account.php" and then when I get to "log_in.php" either redirect them to the Referring page captured in "create_account.php" OR somehow also capture the Referring page in "log_in.php" if they came directly from there and redirect accordingly??  :shrug:

 

 

Debbie

 

 

Link to comment
Share on other sites

Theres no problem at all with your method, and I dont see how you would capture the referring page without the user actually executing the code inside the page which they are on.

 

What I started to do this weekend was this...

 

For Visitors who had to register, on "create_account.php" I had...

 

$_SESSION['referringPage'] = $_SERVER['HTTP_REFERER'];

 

and then when they finally got to "log_in.php", I had...

 

header("Location: " . $_SESSION['referringPage']);

 

 

So if a Visitor was on "article1234.php", then everything worked fine.

 

The problem was that for Members logging in, they went from "article1234.php" to "log_in.php" but I wasn't capturing a referring page in "log_in.php" and more so I was trying to redirect in "log_in.php" without any referring page.

 

Follow me??

 

So I started to try this, but gave up last night...

 

// Redirect User.
if (isset($_SESSION['referringPage'])){
if ($_SERVER['HTTP_REFERER'] == WEB_ROOT . substr("activate.php", 0, 12)){
	// New Registration.

	// Keep Referring Page as one prior to create_account.php
	header("Location: " . $_SESSION['referringPage']);
}else{
	// Current Member.

	// Set Referring Page to one prior to log_in.php
	$_SESSION['referringPage'] = $_SERVER['HTTP_REFERER'];

	// Return user to pre-Log In page.
	header("Location: " . $_SESSION['referringPage']);
}
}else{
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}

 

 

 

Debbie

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.