Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 27

Thread: If condition for moving to another page

  1. #11
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    right - if "incoming" and "outgoing" are the only two options, then that's all you really need. it's the simplest solution, though you might use the database if you want something more flexible.

    as long as the value of the button and the name of the associated page are the same
    (e.g., <option value="Incoming"> should direct to "http://www.example.com/path/to/incoming.php")
    then you can use it, basically, as-is.

  2. #12
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    You mean i will put an a href in <option>?

    Uhm, how about if I used database?

    Thank you

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    No, the example I gave earlier uses your existing <option> values.

    I was simply using the 'call_type' variable directly in the page because the value of the variable was the same as the filename for the page: http://www.example.com/path/to/incoming.php. If it's not always so, then you couldn't do it this way; but if it is, then it saves some coding.

    The other block of code I showed you was basically doing the same thing. The only difference was how we validated the call_type.

    without the database, we defined the valid values in an array used in_array() to make sure that the call_type was correct.

    with a database, we list the valid values in the database and use a query to see if call_type was an allowed value.

    PHP Code:
    if (isset($_POST['call_type'])) {
      
    // escape the value
      
    $call_type mysql_real_escape_string($_POST['call_type']);
      
    $result mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}' ");
      
    // if there's one row in the result, then there was a match.
      
    if(mysql_num_rows($result) === 1){
        
    // send the header()
        
    header("Location: http://www.example.com/path/to/".$call_type.".php"); 
      }

    I hope my suggestion didn't confuse things. I only offered the alternative because maintaining a database for only two values might be "overkill." but if there are other possible values, or if the info is used by other scripts as well, then using the database is definitely the way to go. even if it's not needed, it won't really hurt anything to use a database anyway.

  4. The Following User Says Thank You to traq For This Useful Post:

    rhodarose (03-14-2011)

  5. #14
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    No, the example I gave earlier uses your existing <option> values.

    I was simply using the 'call_type' variable directly in the page because the value of the variable was the same as the filename for the page: http://www.example.com/path/to/incoming.php. If it's not always so, then you couldn't do it this way; but if it is, then it saves some coding.

    The other block of code I showed you was basically doing the same thing. The only difference was how we validated the call_type.

    without the database, we defined the valid values in an array used in_array() to make sure that the call_type was correct.

    with a database, we list the valid values in the database and use a query to see if call_type was an allowed value.

    PHP Code:
    if (isset($_POST['call_type'])) {
      
    // escape the value
      
    $call_type mysql_real_escape_string($_POST['call_type']);
      
    $result mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}' ");
      
    // if there's one row in the result, then there was a match.
      
    if(mysql_num_rows($result) === 1){
        
    // send the header()
        
    header("Location: http://www.example.com/path/to/".$call_type.".php"); 
      }

    I hope my suggestion didn't confuse things. I only offered the alternative because maintaining a database for only two values might be "overkill." but if there are other possible values, or if the info is used by other scripts as well, then using the database is definitely the way to go. even if it's not needed, it won't really hurt anything to use a database anyway.
    I tried to edit my code based on oyur suggestion but i'm not too sure if what I should put on my header location and if theirs a syntax should I need to delete.

    here is my new code:
    PHP Code:
    <?php 


    include ("config.php"); 

    if (isset(
    $_POST['call_type'])) { 
      
    // escape the value 
      
    $call_type mysql_real_escape_string($_POST['call_type']); 
      
    $result mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}' "); 
      
    // if there's one row in the result, then there was a match. 
      
    if(mysql_num_rows($result) === 1){ 
        
    // send the header() 
    //here I got confused because i dont know what location should i put is it incoming.php or outgoing.php, but when I tried this code nothig was happened.
        
    header("Location: incoming.php".$call_type.".php");  
      } 
    }  


    if (
    $result == 1){ 
    if(
    $call_type == 'Incoming'){ 
    header ('Location:incoming.php'); 

    elseif(
    $call_type == 'Outgoing'){ 
    header ('Location:outgoing.php'); 

    else{ 
    header('Location:index.php'); 

    }

    ?>



    <html> 
    <body> 
    <form id="form1" name="form1" method="post" action=""> 
      <select name="call_type" onchange="return handleEnter(this, event)"> 
        <option value="Select Call Type">Select Call Type</option> 
        <option value="Incoming" <?php if($_POST['call_type'] == 'Incoming') echo "selected='selected'"?>>Incoming</option> 
        <option value="Outgoing" <?php if($_POST['call_type'] == 'Outgoing') echo "selected='selected'"?>>Outgoing</option> 
      </select> 
        <input type="submit" name = "Submit" value="Submit">
    </form> 
    </body> 
    </html>
    Thank you so much

  6. #15
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by rhodarose View Post
    PHP Code:
    if ($result == 1){ 
    if(
    $call_type == 'Incoming'){ 
    header ('Location:incoming.php'); 

    elseif(
    $call_type == 'Outgoing'){ 
    header ('Location:outgoing.php'); 

    You don't need this part if you use the line I suggested; just one or the other. My line takes the place of all this. You do, however, need to use full URLs in your header()s. Relative paths (like you have now) produce unpredictable results, at best.

    Observe how I write the URLs in the header() calls:

    PHP Code:
    include ("config.php");  

    if (isset(
    $_POST['call_type'])) {  
      
    // escape the value  
      
    $call_type mysql_real_escape_string($_POST['call_type']);  
      
    // query the database
      
    $result mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}' ");  
      
    // if there's one row in the result, then there was a match.  
      
    if(mysql_num_rows($result) === 1){  
        
    // send the header()  
    //here I got confused because i dont know what location should i put is it incoming.php or outgoing.php, but when I tried this code nothig was happened. 

    // at this point, since we've gotten a match from the database, 
    // we know that the variable 'call_type' holds a value of either "incoming" or "outgoing",
    // so we use the variable instead of hard-coding the filename.

        
    header("Location: http://www.example.com/".$call_type.".php");   
      }else{ 
    header("Location: http://www.example.com/index.php"); } 


  7. #16
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    You don't need this part if you use the line I suggested; just one or the other. My line takes the place of all this. You do, however, need to use full URLs in your header()s. Relative paths (like you have now) produce unpredictable results, at best.

    Observe how I write the URLs in the header() calls:

    PHP Code:
    include ("config.php");  

    if (isset(
    $_POST['call_type'])) {  
      
    // escape the value  
      
    $call_type mysql_real_escape_string($_POST['call_type']);  
      
    // query the database
      
    $result mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}' ");  
      
    // if there's one row in the result, then there was a match.  
      
    if(mysql_num_rows($result) === 1){  
        
    // send the header()  
    //here I got confused because i dont know what location should i put is it incoming.php or outgoing.php, but when I tried this code nothig was happened. 

    // at this point, since we've gotten a match from the database, 
    // we know that the variable 'call_type' holds a value of either "incoming" or "outgoing",
    // so we use the variable instead of hard-coding the filename.

        
    header("Location: http://www.example.com/".$call_type.".php");   
      }else{ 
    header("Location: http://www.example.com/index.php"); } 

    I will try this code, and I have question that in this code is there no code that I would change?
    PHP Code:
    <html> 
    <body> 
    <form id="form1" name="form1" method="post" action=""> 
      <select name="call_type" onchange="return handleEnter(this, event)"> 
        <option value="Select Call Type">Select Call Type</option> 
        <option value="Incoming" <?php if($_POST['call_type'] == 'Incoming') echo "selected='selected'"?>>Incoming</option> 
        <option value="Outgoing" <?php if($_POST['call_type'] == 'Outgoing') echo "selected='selected'"?>>Outgoing</option> 
      </select> 
        <input type="submit" name = "Submit" value="Submit">
    </form> 
    </body> 
    </html>
    Thank you so much

  8. #17
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    no, that part should be fine.

    The only issue that (might) exist is capitalization: the URL shouldn't care about capitalization, and database queries are typically case-insensitive as well.

    If it does present a problem, you could simply change all instances of the value to lower-case ( e.g., <option value="incoming">Incoming</option> instead of <option value="Incoming">Incoming</option> ), or do so after the form is submitted (by using a function like strtolower() ).

    Again, I doubt it would be a problem, but you might want to be aware of it just in case.

    Edit:

    I don't know, however, why you have the php statement that checks which option is selected. If one of the POST values is already set, then the php code above will redirect them to their desired page, and the form will not be shown at all. Unless there is something else going on, I don't see how those lines would ever print anything (they shouldn't hurt anything, but I don't think they serve any purpose).


  9. #18
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    I tried the code you suggested:
    PHP Code:
    <?php
      
    include ("config.php");   

    if (isset(
    $_POST['call_type'])) {   
      
    // escape the value   
      
    $call_type mysql_real_escape_string($_POST['call_type']);   
      
    // query the database 
      
    $result mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}' ");   
      
    // if there's one row in the result, then there was a match.   
      
    if(mysql_num_rows($result) === 1){   
        
    // send the header()   
    //here I got confused because i dont know what location should i put is it incoming.php or outgoing.php, but when I tried this code nothig was happened.  

    // at this point, since we've gotten a match from the database,  
    // we know that the variable 'call_type' holds a value of either "incoming" or "outgoing", 
    // so we use the variable instead of hard-coding the filename. 

        
    header("Location: incoming.php".$call_type.".php");    
      }else{ 
    header("Location: outgoing.php"); }  
    }  
    ?>
    <html>  
    <body>  
    <form id="form1" name="form1" method="post" action="">  
      <select name="call_type" onchange="return handleEnter(this, event)">  
        <option value="Select Call Type">Select Call Type</option>  
        <option value="Incoming" <?php if($_POST['call_type'] == 'Incoming') echo "selected='selected'"?>>Incoming</option>  
        <option value="Outgoing" <?php if($_POST['call_type'] == 'Outgoing') echo "selected='selected'"?>>Outgoing</option>  
      </select>  
        <input type="submit" name = "Submit" value="Submit"> 
    </form>  
    </body>  
    </html>
    And when I run it...it did not go to redirected page


    Thank you

  10. #19
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    note the differences in the code example I offered:
    PHP Code:
    header("Location: http://www.example.com/".$call_type.".php"); 
    and the one you used:
    PHP Code:
    header("Location: incoming.php".$call_type.".php"); 
    aside from removing the "incoming.php" part, you really do need to be using absolute URLs. relative URLs can be unpredictable with the header() function. it's best to use the full path, including the protocol ( http:// ) and your domain ( www.example.com ).

    let me know if it works.

  11. #20
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    I change my header location into:

    PHP Code:
    header("Location: http://localhost/OJT/mae_ann/incoming.php".$call_type.".php"); 
    And still it did not work..


    Thank you

Bookmarks

Posting Permissions

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