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
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

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.