Jump to content

If condition to move to another page


newphpcoder

Recommended Posts

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:

<?phpinclude ("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

Link to comment
Share on other sites

you can either use Javascript to bind an event handler to the select element to detect changes in the list, or use an actual submit button to submit the form to the page and catch the selected option and redirect with PHP. Either way you need an event to fire to initiate any sort of action on the page.

Link to comment
Share on other sites

n/m
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

Link to comment
Share on other sites

i think your php is outputing (eg echo $call_type;)in the page before its been redirecting. probably there an warning is occuring "header already been sent" to be sure you may want to make all error enabled to display. (may be its not displaying for your php.ini setting)when its ouput something in browser headers are already been sent so in later you cant resend the header again. you can do that though with output bufferring.i can see you commented the redirected part hope its uncommented in your real scirpt

Link to comment
Share on other sites

if you want to use headers, you can't have ANY output before you set the headers, so get rid of the echo'shttp://us.php.net/manual/en/function.header.php

<?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'); } }*/} ?>
Link to comment
Share on other sites

if you want to use headers, you can't have ANY output before you set the headers, so get rid of the echo'shttp://us.php.net/manual/en/function.header.php
I tried this code:
<?phpinclude ("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!!!!!!!!!!!!!!!!!!!!!! $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" 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 nothing was happened when I choose incoming.php

Link to comment
Share on other sites

You need to do some debugging. Check what $call_type and $result are.Location headers are supposed to be absolute URIs.

  // Is $result what I expected it to be?  echo '$result: ' . $result . '<br>';  if ($result == 1) {	// Is $call_type what I expected it to be?	echo '$call_type: ' . $call_type . '<br>';	// Headers commented while testing values	if($call_type == 'Incoming') {	  // Location headers must be an absolute URI:	  // header ('Location: http://' $_SERVER['SERVER_NAME'] . dir($_SERVER['PHP_SELF']) . 'incoming.php');	} elseif($call_type == 'Outgoing') {	  // header ('Location: http://' $_SERVER['SERVER_NAME'] . dir($_SERVER['PHP_SELF']) . 'outgoing.php');	} else {	  // header ('Location: http://' $_SERVER['SERVER_NAME'] . dir($_SERVER['PHP_SELF']) . 'index.php');	}  }

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...