Jump to content

Redirect to index on submit


Clandestinex337

Recommended Posts

So this is something really easy I would guess. I just can't get it to work.

 

I have a switch setup to run my functions

<?php

include 'db_fns.php';

$controller = 'view';
$view = empty($_GET['view']) ? 'index' : $_GET['view']; 

switch($view)
{
	case index:
		$user = new User($connect, 1);
		echo $user->getUsername() . '<br/>';
		echo $user->getPassword();
	break;

	case register:
		$page = 'register';
		$user = new User($connect);
		$user->setUsername($_POST['username']);
		$user->setPassword($_POST['password']);
		$id = $user->save();
	break;
}
if (isset($page))
{
	include ($_SERVER['DOCUMENT_ROOT'] . 'img_nexus/views/photos/' . $page . '.php');
}


?>

 

and then I have my form

 

<form method="POST">
<input type="text" name="username" value="Username" onclick="this.value = ''" /><br/>
<input type="text" name="password" value="Password" onclick="this.value = ''" /><br/>
<input type="submit" name="submit" />
</form>

 

I tried target="index.php" but if I do that, then it doesn't grab the values from the _POST

Link to comment
https://forums.phpfreaks.com/topic/240441-redirect-to-index-on-submit/
Share on other sites

  Quote
then it doesn't grab the values from the _POST

Not understanding your question, but you're not using any $_POST vars for your index action

		case index:
		$user = new User($connect, 1);
		echo $user->getUsername() . '<br/>';
		echo $user->getPassword();
	break;

I want it to run the register switch..

 

so when I go to ?view=register

 

it shows the form, when I click submit it submits the data to my database. I just now want to redirect to the index once I hit submit and the data is sent to the database. Right now when I hit submit it just opens a new register page.

In order for it run the register switch you need to set your forms action to index.php?view=register

<form action="index.php?view=register" method="POST">
<input type="text" name="username" value="Username" onclick="this.value = ''" /><br/>
<input type="text" name="password" value="Password" onclick="this.value = ''" /><br/>
<input type="submit" name="submit" />
</form>

Now when the form is submitted it'll call the register switch.

 

To redirect the user back to the index page when the user has been registered you'll use header

		case register:
		$page = 'register';
		$user = new User($connect);
		$user->setUsername($_POST['username']);
		$user->setPassword($_POST['password']);
		$id = $user->save();
		header('Location: index.php'); // redirect back to the index page
	break;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.