Jump to content

php and javascript


r-it

Recommended Posts

can someone please help me, i have a problem with a script similar to this, but its too long so i just decided to make a similar example. it gives me the header already sent error message: Warning: Cannot modify header information - headers already sent by (output started at I:\wamp\www\shzen\addRetail.php:13) in I:\wamp\www\shzen\addRetail.php on line 38. i really do not know why its going into the javascript when i dont call it, i only call it when the submit button is clicked. I do not know how that ob_start thing works or how to implement it, can anyone please help.

<?php
session_start();
require("connex.php");

$conn = new dbConnector();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add new retail product</title>

<script language="javascript" type="text/javascript">
function valid(f)
{
var input = f.txName.value;

if(input == "")
{
	alert("Please enter a retail product name before continuing");
	f.txName.focus();
	return false;
}
else
{
	return true;
}
}
</script>
</head>
<body bgcolor="#00CCFF">
<?php
if($HTTP_POST_VARS)
{
if($_POST['submit'] == "Cancel")
{
	//redirect to menu
	header("Location: menu.php");
	exit;
}
else
{
	//do query
}
}
else
{
$show = "<form action=\"$_SERVER[php_SELF]\" method=\"post\" name=\"fmRetail\" >
<table width=\"40%\">
  <tr>
    <th align=\"left\" width=\"35%\">Source:</th>
    <td><select name=\"sel_src\" size=\"1\" onchange=\"document.forms.fmRetail.submit()\">";

	$qry1 = "SELECT * FROM sourcetbl ORDER BY sourcename";
	$rez = $conn->query($qry1);

	while($rec = $conn->fetchArray($rez))
	{
		$src = $rec['sourcename'];

		$show .= "<option value= \"$src\">$src</option>";
	}


	$show .= "</select></td>	
  </tr>
<td>Name:</td>
  <td><input type=\"text\" name=\"txName\" size=\"30\" /></td>
  </tr>
<tr>
  <td> </td>
  <td><input name=\"submit\" type=\"submit\" value=\"Submit\" onclick='return valid(fmRetail)'  /> <input name=\"submit\" type=\"submit\" value=\"Cancel\" /></td>
  </tr>
</table>


</form>";

}
echo $show;
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/39630-php-and-javascript/
Share on other sites

The error is caused because you are trying to do a header redirect after your output has already started. There are two solutions, but the second is definitely the least preferred:

 

1) Modify your code so that any conditions that result in a header modification are checked before your HTML output begins

 

2) Use the output buffering functions to screen out the error.

Link to comment
https://forums.phpfreaks.com/topic/39630-php-and-javascript/#findComment-191280
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.