Log in

View Full Version : If condition for moving to another page



rhodarose
03-10-2011, 06:00 AM
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
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

traq
03-10-2011, 03:19 PM
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

djr33
03-10-2011, 11:26 PM
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.

rhodarose
03-11-2011, 12:55 AM
I tried this 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

djr33
03-11-2011, 01:09 AM
$result = mysql_num_rows($query);


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

rhodarose
03-11-2011, 01:22 AM
$result = mysql_num_rows($query);


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


Yes...

djr33
03-11-2011, 01:55 AM
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?

traq
03-11-2011, 02:38 AM
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:


// 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:


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");
}
}

rhodarose
03-11-2011, 04:56 AM
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:


// 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:


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

fastsol1
03-11-2011, 12:59 PM
Then use what Traq showed in the first example where it checks if the call_type is in the array of allowed call_types.

traq
03-12-2011, 03:34 AM
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.

rhodarose
03-14-2011, 01:36 AM
You mean i will put an a href in <option>?

Uhm, how about if I used database?

Thank you

traq
03-14-2011, 02:09 AM
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.


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.

rhodarose
03-14-2011, 02:19 AM
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.


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


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

traq
03-14-2011, 02:09 PM
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:


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"); }
}

rhodarose
03-15-2011, 12:25 AM
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:


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?


<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

traq
03-15-2011, 01:15 AM
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.



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).

rhodarose
03-17-2011, 12:35 AM
I tried the code you suggested:


<?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

traq
03-17-2011, 03:27 AM
note the differences in the code example I offered:
header("Location: http://www.example.com/".$call_type.".php");
and the one you used:
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.

rhodarose
03-17-2011, 03:41 AM
I change my header location into:



header("Location: http://localhost/OJT/mae_ann/incoming.php".$call_type.".php");


And still it did not work..


Thank you

traq
03-17-2011, 02:22 PM
you need to remove the " incoming.php " part.
The variable call_type should hold either the word "incoming" or "outgoing" at this point.


header("Location: http://localhost/OJT/mae_ann/".$call_type.".php");

rhodarose
03-21-2011, 08:33 AM
you need to remove the " incoming.php " part.
The variable call_type should hold either the word "incoming" or "outgoing" at this point.


header("Location: http://localhost/OJT/mae_ann/".$call_type.".php");

I tried the code you suggested:


<?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: http://localhost/OJT/mae_ann/".$call_type.".php");
}
//else{ header("Location: http://localhost/OJT/mae_ann/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 the output is still it did not go to incoming.php when I choose incoming .
:confused:

Thank you

traq
03-21-2011, 02:37 PM
hmm... where is it going? do you get a 404? do you get anything?

does this work?

header("Location: http://localhost/OJT/mae_ann/incoming.php"); ?
try this

print call_type;
so we can see if the value is getting passed on correctly.

rhodarose
03-22-2011, 01:20 AM
hmm... where is it going? do you get a 404? do you get anything?

does this work?

header("Location: http://localhost/OJT/mae_ann/incoming.php"); ?
try this

print call_type;
so we can see if the value is getting passed on correctly.

this code:


header("Location: http://localhost/OJT/mae_ann/incoming.php");

did not work and when i print $call_type
no output was display

Thank you

traq
03-22-2011, 01:39 AM
try this and let me know what it tells you.


<?php

include ("config.php");

if(isset($_POST['call_type'])){
$call_type = mysql_real_escape_string($_POST['call_type']);
$result = mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}' ");
if(!$result){ print 'the query did not execute properly: '.mysql_error().'<br>'; }

// if the line below doesn't work, try commenting it out and using the commented line instead
if(mysql_num_rows($result) === 1){
// if(mysql_num_rows($result) > 0){

header("Location: http://localhost/OJT/mae_ann/".$call_type.".php");
}else{
var_dump($call_type);
}
}else{ print '$_POST[call_type] is not set<br>'; }

?>
<html>
<head>
</head>
<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">Incoming</option>
<option value="outgoing">Outgoing</option>
</select>
<input type="submit" name = "Submit" value="Submit">
</form>
</body>
</html>

rhodarose
03-22-2011, 04:43 AM
try this and let me know what it tells you.


<?php

include ("config.php");

if(isset($_POST['call_type'])){
$call_type = mysql_real_escape_string($_POST['call_type']);
$result = mysql_query("SELECT `call_type` FROM `tbl_calltype` WHERE `call_type` = '{$call_type}' ");
if(!$result){ print 'the query did not execute properly: '.mysql_error().'<br>'; }

// if the line below doesn't work, try commenting it out and using the commented line instead
if(mysql_num_rows($result) === 1){
// if(mysql_num_rows($result) > 0){

header("Location: http://localhost/OJT/mae_ann/".$call_type.".php");
}else{
var_dump($call_type);
}
}else{ print '$_POST[call_type] is not set<br>'; }

?>
<html>
<head>
</head>
<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">Incoming</option>
<option value="outgoing">Outgoing</option>
</select>
<input type="submit" name = "Submit" value="Submit">
</form>
</body>
</html>

When i run this code this is the output:

$_POST[call_type] is not set

And when I choose incoming, Finally I redirected to incoming.php

Thank you

traq
03-22-2011, 05:31 AM
glad it worked! just FYI, the following lines are solely for testing purposes and can/should be commented or removed:


if(!$result){ print 'the query did not execute properly: '.mysql_error().'<br>'; }

else{
var_dump($call_type);
}

// if the line below doesn't work, try commenting it out and using the commented line instead
// if(mysql_num_rows($result) > 0){

else{ print '$_POST[call_type] is not set<br>'; }