Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to put a if-else statement around a submit click

    I'm having two problems which are the same between three php files. All 3 are php inside xhtml pages.

    I need to make the code so that when that if the submit button in file1 is pushed, it goes to file2, but if its not pushed, then it displays an error_message and provides a link to file 1.

    Any help as I can't figure this one part out.

    file 1:

    Code:
    <body>
    
    	<div id="content">
    
    	<?php if (!empty($error_message)) { ?>
        <p><?php echo $error_message; ?></p>
        <?php } ?>
        
    		<form action="file2.php" id="loginForm" method="post">
    
    
    			<div id="loginData">
    		
            		<label>User ID: </label>
    				<input type="text" name="user"/>
    				<br \>
    		        <label>Password:</label>
    				<input type="password" name="password" /> <br \>
    			</div>
        
    		    <div id="buttons">
    				<label>&nbsp;</label>
    				<!-- This is the submit button for submission of credentials -->
    		        <input id="submit" type="submit" value="SubmitCredentials" class="submit_btn"/>
    	
    			</div>
       
    
    		</form>
    	</div>
    </body>
    file2
    Code:
    [HTML]
    <body>
    	
        
    		
    <?php 
    	
    	if ($user == "test" && $password == "test") 
    	{ 
    		echo "Hello ".$user;  ?>
            <br />
            <form name="city_name" action="file3.php" method = "post">
    			<input type="text" name="city" /><br />
                <input type="submit" value="SubmitCityName"/>
            </form>
    		
    		
    	} [/HTML]
    :
    Last edited by putha; 11-16-2011 at 04:59 PM.

  • #2
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    I don't know if I under your problem, but you can check for a submit event using the isset() function in PHP.

    Try this:

    Code:
    <body>
    
    	<div id="content">
    
    	<?php if(isset($_POST['submit'])) { ?>
        <p><?php echo $error_message; echo "<a href='link'>LinkHere</a> ?></p>
        <?php } ?>
        
    		<form action="Auth_CityForm.php" id="loginForm" method="post">
    
    
    			<div id="loginData">
    		
            		<label>User ID: </label>
    				<input type="text" name="user"/>
    				<br \>
    		        <label>Password:</label>
    				<input type="password" name="password" /> <br \>
    			</div>
        
    		    <div id="buttons">
    				<label>&nbsp;</label>
                                      //I added the name attribute
    				<!-- This is the submit button for submission of credentials -->
    		        <input id="submit" type="submit" name="submit" value="SubmitCredentials" class="submit_btn"/>
    	
    			</div>
       
    
    		</form>
    	</div>
    </body>
    What exactly are you trying to do? I don't see the point in displaying an error message for an unsubmitted form. I can understand displaying an error message for a form that fails validation- but if hasn't been submitted then you would still be in the original form.

    Why provide a link to file 1 if you'll be staying in file 1?

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Between file 1 and file 2, I am trying to make it so that if the submitcredentials button is pushed, it goes to the php in file 2. Whereas, if submitcredentials is not pushed, an error message appears along with a link to file 1.

    Below is the condition I am trying to fulfill

    " if /* the user has pressed SubmitCredentials and */ the credentials are
    valid (see Requirements below), the page displays
    1. the text "Hello ", followed by the name of the user, as entered by
    the user and
    2. a form called city_name, which has
    1. one text input field, where the user enters a city name
    2. one submit button, whose name is SubmitCityName



    Question: when would the error message ever be triggered if file1 only contains two text fields and a submitcredentials button
    Last edited by putha; 11-16-2011 at 05:04 PM.

  • #4
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, so i have changed the code to the following in file 1 and 2

    file1:
    Code:
    <body>
     
    		<form action="file2.php" id="loginForm" method="post">
    
    			<div id="loginData">
    		
            		<label>User ID: </label>
    				<input type="text" name="user"/>
    				<br \>
    		        <label>Password:</label>
    				<input type="password" name="password" /> <br \>
    			
    </form>    </body>
    file2:
    Code:
    <?php
    	$user=$_POST['user'];
    	$password=$_POST['password'];
    	$submitbtn=$_POST['submitbtn'];
    ?>
    <html>
    <body>
    
    	<?php
    	
    	if(isset($submitbtn["submit"])) 
    	{
    		
    		if ($user == "test" && $password == "test") 
    		{ 
    			echo "Hello ".$user;  ?>
    	        <br />
    		        <form name="city_name" action="file3.php" method = "post">
    				<input type="text" name="city" /><br />
    	            <input type="submit" value="SubmitCityName" name="submitcitybtn"/>
    	        </form>
    		
    			<?php
    }
    	else 
    	{
    			$error_message="You have not clicked the Submit button<br />";
    			echo $error_message; 
    			echo "<a href='file1.php'>file1</a>";
    	}?>	
    </body> /html>


    My question is when is the else statement ever touched if the only fields in file 1 are two text fields and a submit button
    Last edited by putha; 11-16-2011 at 05:00 PM.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You'll only ever hit the else in there if you have hit the enter key while within a non-submittable entry in an older version of IE (it is a bug; the submit button invoking the form action is considered a qualified field and thus has to be provided by the browser). This is why checking isset on a submit button is not recommended, rather you should use isset on all fields expected.
    PHP has no way to detect how a form was submitted. Sending successful fields can be done from ajax and direct socket's as well.

    Now with your actual code, you will ALWAYS enter the else. This is two reasons, the first is that $_POST['submitbtn'] simply never exists. So you assign null to $submitbtn (as well as triggering a warning since you haven't checked for this offset). The next is because you are checking isset with an array offset that wouldn't exist even if you had successfully extracted the $_POST['submitbtn']. At this point, it would now be a scalar string, and not an array.
    Note that while providing the next form, upon submission you will lose the knowledge of what was just provided. If you need to retain this data, it can be done with sessions.

  • #6
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thats not true on always hitting the else statement because when i just tried it, if file1 form is submitted, then it goes into file2 and is able to get into the if the button is submitted.

    The only thing i can't put together is any instance in which the else statement and error msg are thrown other than the one IE bug issue you just mentioned

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by putha View Post
    Thats not true on always hitting the else statement because when i just tried it, if file1 form is submitted, then it goes into file2 and is able to get into the if the button is submitted.

    The only thing i can't put together is any instance in which the else statement and error msg are thrown other than the one IE bug issue you just mentioned
    Not with what you have here it doesn't. If it does, I suggest forcing a non-cached entry instead. You have not provided a submit button at all in file1, so it will fail to pass the first if.

    That said, there will be a syntax error here too. You appear to be missing a closing brace.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •