Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: If condition for moving to another page

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

    Default If condition for moving to another page

    Good day!

    I want to know what is wrong in my code that's why when i choose in select option it did not go to another page...Like when I select "Incoming" nothing happen also when I choose "Outgoing" nothing also happen.

    Here is my code:

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

    $call_type $_POST['call_type'];

    $query=mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}'") or die(mysql_error());
    $result mysql_num_rows($query);

    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">
        <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>
    </form>
    </body>
    </html>
    Thank you
    Last edited by rhodarose; 03-10-2011 at 06:25 AM.

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

    Default

    a successful mysql_query doesn't return "1" (it returns a resource), so your first if() condition is false and the code is not executed.

    try changing it to just if($result) instead

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That will work. Here are two additional ways to approach it:

    if ($result!==FALSE)
    Also possible as:
    if ($result!=0) (But that code is a bit lazy, since it really is finding a FALSE, not a 0).

    Or:
    if (mysql_num_rows($result)==1)
    That might be what you wanted-- that's saying "if there's exactly one result".
    You can also change that a bit for other cases. Change 1 to any number you'd like.
    Or use a greater than operator:
    if (mysql_num_rows($result)>5)
    (If there are more than 5 rows)
    if (mysql_num_rows($result)>=5)
    (If there are 5 or more rows)
    if (mysql_num_rows($result)!=0)
    If the number of rows is not 0-- equivalent to the other options above-- if any rows were found...



    But you can also do it a little more directly in some cases:
    if ($row = mysql_fetch_assoc($result))
    This code attempts to set $row as the first row found from the query (as an associative array). If it is successful, it will continue. If not, it will skip the next section. That's a very common way to use if statements with mysql queries, but not the only option.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    Default

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

    if (isset(
    $_POST['call_type'])) { // Check if form has been submitted 
    $call_type mysql_real_escape_string($_POST['call_type']); // SECURE THE FECKING CONTENT!!!!!!!!!!!!!!!!!!!!!! 

    echo $call_type

    $query=mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}'") or die(mysql_error()); 
    $result mysql_num_rows($query); 
    echo 
    '<br />' $result;

    /*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"> 
        <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>
    As you can see I add submit button.

    The output of this code isv , when I choose Incoming and i press submit button the result is Incoming 1 and it was displayed in the same page. I want to happen is when I choose Incoming and I press submit button it will go to incoming.php:cry:

    Thank you

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    $result = mysql_num_rows($query);


    That line sets $result as the number of rows. Is that what you're trying to do?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    Default

    Quote Originally Posted by djr33 View Post
    $result = mysql_num_rows($query);


    That line sets $result as the number of rows. Is that what you're trying to do?

    Yes...

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I think I misread your code in the first post. (Maybe traq did too.)

    What happens if you echo the name of the page instead of using a redirect? Just to test the value.

    header('Location...') is supposed to have an absolute URL (including HTTP), but you are using just a filename. Change those to full URLs-- does that help?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    rhodarose (03-11-2011)

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

    Default

    Am I correct in thinking that...

    --you want to check if the "call_type" is a valid page
    --if so, redirect to that page

    ?

    If so, there are two options for you:

    1) hard code.
    --if the only options are "incoming" and "outgoing", and you don't plan on changing the options often, you can simply verify that the "call_type" is one or the other:

    PHP Code:
    // make an array containing the allowed values
    $allowed_types = array('Incoming','Outgoing');

    // check if the call_type is submitted, and (if so) that it matches one of the allowed values
    if (isset($_POST['call_type']) && in_array($_POST['call_type'], $allowed_types)) {
      
    // if so, set the appropriate header()
      // you can use the value directly if it's the same as the filename
      
    header("Location: http://www.example.com/path/to/".$_POST['call_type'].".php"); 

    looking at your example above, that's more than sufficient for what you're doing. however, you should use the database if you want the allowed pages to be more flexible:
    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"); 
      }


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

    Default

    Quote Originally Posted by traq View Post
    Am I correct in thinking that...

    --you want to check if the "call_type" is a valid page
    --if so, redirect to that page

    ?

    If so, there are two options for you:

    1) hard code.
    --if the only options are "incoming" and "outgoing", and you don't plan on changing the options often, you can simply verify that the "call_type" is one or the other:

    PHP Code:
    // make an array containing the allowed values
    $allowed_types = array('Incoming','Outgoing');

    // check if the call_type is submitted, and (if so) that it matches one of the allowed values
    if (isset($_POST['call_type']) && in_array($_POST['call_type'], $allowed_types)) {
      
    // if so, set the appropriate header()
      // you can use the value directly if it's the same as the filename
      
    header("Location: http://www.example.com/path/to/".$_POST['call_type'].".php"); 

    looking at your example above, that's more than sufficient for what you're doing. however, you should use the database if you want the allowed pages to be more flexible:
    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"); 
      }

    But it depends on what the user choose on the selection, to were the user will go.if the user choose Incoming it will go to incoming.php and if he choose outgoing it will go to outgoing.php

    Thank you

  11. #10
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Then use what Traq showed in the first example where it checks if the call_type is in the array of allowed call_types.

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
  •