Clandestinex337 Posted June 26, 2011 Share Posted June 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240441-redirect-to-index-on-submit/ Share on other sites More sharing options...
wildteen88 Posted June 26, 2011 Share Posted June 26, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/240441-redirect-to-index-on-submit/#findComment-1234993 Share on other sites More sharing options...
Clandestinex337 Posted June 26, 2011 Author Share Posted June 26, 2011 no, that its showing already users that are in the database. with mysql_query and mysql_fetch Quote Link to comment https://forums.phpfreaks.com/topic/240441-redirect-to-index-on-submit/#findComment-1234995 Share on other sites More sharing options...
wildteen88 Posted June 26, 2011 Share Posted June 26, 2011 What are you to do when the form has been submitted. At the moment every time the form is submitted it will always run the index action within your switch statement, which is the block of code I mentioned in my previous post. Quote Link to comment https://forums.phpfreaks.com/topic/240441-redirect-to-index-on-submit/#findComment-1234997 Share on other sites More sharing options...
Clandestinex337 Posted June 26, 2011 Author Share Posted June 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240441-redirect-to-index-on-submit/#findComment-1234999 Share on other sites More sharing options...
wildteen88 Posted June 26, 2011 Share Posted June 26, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/240441-redirect-to-index-on-submit/#findComment-1235001 Share on other sites More sharing options...
Clandestinex337 Posted June 26, 2011 Author Share Posted June 26, 2011 works like a charm thank you Quote Link to comment https://forums.phpfreaks.com/topic/240441-redirect-to-index-on-submit/#findComment-1235002 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.