Ask Experts Questions for FREE Help !
Ask
    Jbert's Avatar
    Jbert Posts: 56, Reputation: 1
    Junior Member
     
    #1

    Jun 1, 2008, 08:26 PM
    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
    Jbert's Avatar
    Jbert Posts: 56, Reputation: 1
    Junior Member
     
    #2

    Jun 1, 2008, 08:34 PM
    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
    eruckus's Avatar
    eruckus Posts: 6, Reputation: 1
    New Member
     
    #3

    Jun 4, 2008, 04:24 PM
    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 isn't 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 can't 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 didn't 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 don't 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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Page redirection (based on bandwidth) [ 1 Answers ]

Ive got a website that is too large for dialup/256kb users to load (and they leave before it fully loads). I then reduced the size (pictures/quality, flash removal) so they could load it quick, then the higher connection users say that it looks too unprofessional. My idea is to have to index...


View more questions Search