Hello Friends,

I have an issue after upgrading my firefox to version 4. This issue was there in IE8 but worked nicely in firefox3

Here is my issue:

I have a page with three forms and each form posts using "htmlentities($_SERVER)" action that is to the same page. Based on the posted value This page redirects to different pages.

This is not working any more and the same page is displayed.

What could be the reason?..

Dani AI

Generated

Summary and practical fix

The problem turned out to be the form control type. Image-type submit controls send click coordinates rather than a plain button name, so PHP ends up with keys like name_x and name_y instead of name. Firefox 4 (unlike some other browsers) did not populate the plain name, so isset($_POST['users_view']) stayed false. solved it by switching the logic to rely on the hidden ID field; that is the simplest, most portable fix. 's suggestion to avoid relying on submit names was on target.

Why this happens and what to do

When an <input type="image"> is used the browser sends the coordinates as name.x/name.y; PHP converts dots for array keys, producing name_x/name_y. To detect an image-button click directly, check for the coordinate keys:

if (isset($_POST['myImageBtn_x'])) {
  // image button was clicked
}

Better, more robust alternatives

  • Use a normal submit (or a <button type="submit">) with a name and value. That reliably sets a single POST value across browsers:
<button type="submit" name="action" value="view"><img src="..."></button>
  • Or keep the visual image but include an explicit hidden input carrying the action or id; check that hidden field on the server (this is what did and it avoids browser quirks).

Also remember to call session_start() before any output, and perform header('Location: ...') redirects before sending body content (watch out for stray whitespace or BOMs). For details on image-button behavior see input type="image" — MDN.

Recommended Answers

All 11 Replies

Your code?

Here is my code:

<?
session_start();
include 'includes/auth.php';
include 'includes/config.php';
$_SESSION['page']="users_list";
if ($_POST)
{
	if (isset($_POST['users_view']))
	{
		$_SESSION['id_view'] = $_POST['user_view'];
		header( 'Location: users_view.php' );
		exit;
	}
	elseif (isset($_POST['users_delete']))
	{
		$_SESSION['user_id'] = $_POST['user_delete'];
		header( 'Location: delete_confirm.php' );
		exit;
	}
}

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<!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=utf-8" />
<title>Test</title>
</head>
<body>
........
........
</body>
</html>

$_SERVER will surely just end up on the current page, so why not just use # and use hidden fields to transfer any data you need?

I need to POST data to the same page as the code below will redirect to different pages based on the POST values.

if ($_POST)
{
if (isset($_POST['users_view']))
{
$_SESSION['id_view'] = $_POST['user_view'];
header( 'Location: users_view.php' );
exit;
}
elseif (isset($_POST['users_delete']))
{
$_SESSION['user_id'] = $_POST['user_delete'];
header( 'Location: delete_confirm.php' );
exit;
}
}

The page "users_view.php" works on the value $_SESSION and delete_confirm.php uses the value $_SESSION.

It was working perfect until I upgrade firefox from version 3 to 4.

Yes, I understand you're going to the current page. My question was why you're choosing to use a global php variable to do so when you can just use "#"

I wasn't aware of it. Thank you for letting me know the use of "#"

But here the issue is not with posting to the same page. The issue is it doesn't redirect to other pages.

The POST check including page redirection is done before generating html codes. That is before <html></html>.

Update: Its working fine in "Google Chrome".

I may be wrong, but I think you have to set the session up after the header commands if they are to work.

Member Avatar for Member #120589

IMO: sending a form to its own page is not a good idea. You should send it to a form handler, otherwise, reload/refresh causes problems.

That's correct. But in my case the page redirects to another one(eg: user_view.php) with the form data after the form is submitted to it. The page checks for the form data ("if ($_POST)")before the html is generated (ie. before <html></html>).
If the form is not submitted, the page acts just like a normal page and reload/refresh won't have any effect on the page.

If this method no longer works, or only in certain browsers, then instead of redirecting, you could use code like this:

<?
session_start();
include 'includes/auth.php';
include 'includes/config.php';
$_SESSION['page']="users_list";
if ($_POST)
{
	if (isset($_POST['users_view']))
	{
		$_SESSION['id_view'] = $_POST['user_view'];
		include "./users_view.php";
	}
	elseif (isset($_POST['users_delete']))
	{
		$_SESSION['user_id'] = $_POST['user_delete'];
		include "./delete_confirm.php";
	}
	else 
	{
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    ?>
    <!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=utf-8" />
    <title>Test</title>
    </head>
    <body>
    ........
    ........
    </body>
    </html>
  }
}

I have fixed this issue..

In my form, I am using image instead of button to submit the form:

echo "<form name=formuser action=# method=post>"
echo "<input type=hidden value=".$rows['user_id']." name=user_id />";
echo "<input type=image src=images/more.png value=users_view name=users_view title=View />";
echo "</form>";

and the PHP code at the beginning checks if "users_view" is set (i.e. the image )

if (isset($_POST['users_view']))

I changed this if condition to check if "user_id" is set (i.e. hidden field)

if (isset($_POST['users_id']))

After that everything is working fine as before.

Thank you all for the support :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.