Reputation: 73
I have some problem. Firefox can't read the value and name of input type image.
Here is my piece of code:
*blabla.php
<?php
echo"
<form action='cek.php' method='POST'>
<input type='image' src='something.png' alt='Submit button' value='Continue' name='noi'>
</form>
";
?>
*cek.php
<?php
echo"
$a = $_POST['noi'];
<input type='text' value='$a'>
";
?>
but Firefox says there is no input named 'noi'. Please help me to solve this problem.
Thanks in advance.
Upvotes: 1
Views: 1585
Reputation: 12709
When using input type=image
you are telling the browser to submit the x and y coordinates where the user clicked on the image. This allows you to add image map type functionality to your forms. Specification says:
“the element's value attribute must be omitted“
But browsers are inconsistent about that, some browsers like Chrome will send it, and some like Firefox follow the specifications and will send only the X and Y coordinates. That is the reason why there is no input named 'noi' in your example.
To learn how to inspect variables check the PHP documentation for functions like var_dump, var_export, print_r, isset etc. Put this code in cek.php, submit the form and you will see the $_POST
array values.
<?php
var_dump( $_POST );
// Firefox:
// array(2) { ["noi_x"]=> string(3) "134" ["noi_y"]=> string(2) "91" }
// Chrome:
// array(3) { ["noi_x"]=> string(3) "121" ["noi_y"]=> string(2) "93" ["noi"]=> string(8) "Continue" }
?>
Upvotes: 0
Reputation: 51
if you want to send "Continue" to the *cek.php script, you can use an html input element with type hidden and value "Continue" in form on *blabla.php
Submit button with type image only send x and y co-ordinates of the button (NOT its value). So, in your *blabla.php:
<?php
echo"
<form action='cek.php' name='myform' method='POST'>
<input type='text' name='first-name' id='first-name' value='' />
<input type='hidden' name='my-input' value='Continue' />
<input type='image' src='submit.png' alt='Submit button' value='Continue' name='noi'>
</form>
";
?>
and in *cek.php
<?php
$a = $_POST['my-input'];
echo "<input type='text' value='$a'>";
?>
Upvotes: 1
Reputation: 74217
What you need to do and am under the impression you want the word "Continue" to appear in the text box.
At least that's the impression I am getting by the use of value='$a'
which tells me that you wish to echo that variable from your form.
If so, then you need to modify your PHP handler to:
<?php
$a = $_POST['noi'];
?>
<input type='text' value='<?php echo $a ?>'>
and adding PHP tags while echoing the value for it, using:
value='<?php echo $a ?>'
as opposed to: (which will cause a parse error)
echo"
$a = $_POST['noi'];
<input type='text' value='$a'>
";
since there is a semi-colon after $_POST['noi']
then trying to inject HTML into without going out of and back into PHP. A semi-colon tells PHP to stop. Yet since there was no closing PHP tag following the semi-colon and no re-opening tag, PHP throws that (parse) error.
That is the reason you are getting the undefined error message.
Having error reporting "ON", would have signaled this "parse" error.
Place the following at the top of your file(s) during development.
error_reporting(E_ALL);
ini_set('display_errors', 1);
which will signal errors, if found in your code.
Footnote:
Alternatively, you can use:
<?php
$a = $_POST['noi'];
echo"
<input type='text' value='$a'>
";
?>
simply by moving the $a = $_POST['noi'];
outside the echo.
Upvotes: 2