Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Scripting > PHP   »   URL Redirection

 
Thread Tools Display Modes
Question
 
 
#1  
Old Jun 1, 2008, 07:26 PM
Jbert
Junior Member
Jbert is offline
 
Join Date: Oct 2006
Location: Dallas Area in Texas
Posts: 50
Jbert See this member's comment history on his/her Profile page.
URL Redirection

PHP Code:
// check for submit
if (!isset($_POST['submit'])) {
    // and display form
    ?>
    Please sect one
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="checkbox" name="example[]" value="Arrays">Arrays<br />
    <input type="checkbox" name="example[]" value="Loops">Loops<br />
    <input type="checkbox" name="example[]" value="Sort">Sort<br />
    <input type="checkbox" name="example[]" value="Submit">Submit<br />
    <input type="checkbox" name="example[]" value="New">New<br />
    <input type="checkbox" name="example[]" value="New">New<br /><br /><br />
    <input type="submit" name="submit" value="Select">
    </form>
When I select checkbox, say Arrays and hit submit, I want a new address in the address window like this one

//localhost/Examples/arrays.php. This will then display the Arrays.php script on my local server.


Is this possible?


Thanks


Jim

Reply With Quote
 
     

Answers
 
 
Old Jun 1, 2008, 07:34 PM   #2  
Junior Member
Jbert is offline
 
Join Date: Oct 2006
Location: Dallas Area in Texas
Posts: 50
Jbert See this member's comment history on his/her Profile page.
Let me explain what I am trying to do in my quest to learn PHP. I want to make a check box page of all the scripts I write in my learning. Each script will be connected to its own check box. Once i load the main script , I then can select a check box ( say loops ) and hit submit. That will then load the PHP script and display the example of the loops script.

As I get deeper in my quest, this will save a lot of typing to re visit scripts.



JIM
  Reply With Quote
 
     
 
 
Old Jun 4, 2008, 03:24 PM   #3  
New Member
eruckus is offline
 
eruckus's Avatar
 
Join Date: Jan 2008
Location: Long Island
Posts: 6
eruckus See this member's comment history on his/her Profile page.
First off i think you should use a radio button instead of a check box so the user can only select one choice.

After that...each script you have a check box linked to should have a name so it can be identified by the main script. Then the main script should have some php at the beginning of it to check to see if anything was submitted. If something was submitted, she script should identify what was submitted and load the appropriate page. If nothing was submitted, the main script should load as normal.

For instance, maybe something like this...
Code:
<?php


// Check to see if page was submitted
if(isset($_POST['submit'])){

	// The main script was submitted, check to see if something was selected
	if($_POST['arrays']){

		// Arrays was selected, load array page
		header("Location: ./arrays.php");

	}
	elseif($_POST['loops']){

		// Loops was selected, load loops page
		header("Location: ./loops.php");

	}

	// Nothing was selected but page was still submitted, display error message
	else{

		$error_message = "<div style=\"color:red;\">ERROR: Please select a page before clicking submit.</div>";
		?>

		<html><head>

		<title>The Main Page</title>

		</head><body>

		Welcome to the main page! Select a page then click submit.
		<?php echo $error_message ?>
		</p>
		<form action="./main.php" method="POST">
		Arrays<input type="radio" name="arrays"><br>
		Loops<input type="radio" name="loops"><br>
		<input type="submit" value="Submit" name="submit">

		</body></html>

		<?php

	}

}
else{

	// The main page was not submitted, display main page as normal
	?>

	<html><head>

	<title>The Main Page</title>

	</head><body>

	Welcome to the main page! Select a page then click submit.
	</p>
	<form action="./main.php" method="POST">
	Arrays<input type="radio" name="arrays"><br>
	Loops<input type="radio" name="loops"><br>
	<input type="submit" value="Submit" name="submit">

	</body></html>

	<?php

}

?>
Now because i just went ahead just did all your homework for you lets go over it so you learn why this does what it does...

PHP works great with forms so your task isnt a difficult one. The first few lines just check to see if the page was submitted. If the form was submitted at all it would be submitted using post so PHP looks in the post array to see if something named "submit" was passed. If it was passed, PHP again looks in the post array for the name of the page you selected. If it find the page you selected PHP redirects to the page you selected. If PHP cant find your selection, this means you clicked "submit" without making a selection so the script loads the form with an error message.

Back to the beginning...if PHP didnt find submit in the post array, then the main page was never submitted (ie. this is the first time it has been loaded) and the page should display the form as normal.

If you look at the form, the inputs have names (ie. name="arrays"). These names are what is passed to the post array. If these names differ from what PHP is looking for in the post array, or if these names dont exist at all, the script will fail. When adding more choices or changing your existing ones, take care to see that the name of the input is the same as what PHP is looking for in the array.
  Reply With Quote
 
     


Thread Tools
Display Modes

 
Similar Sponsors

Similar Threads
Question Asker Forum Answers Last Post
page redirection (based on bandwidth) GenomeX Internet & the Web 1 May 7, 2006 02:47 AM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 05:35 PM.