Jump to content

Getting $_POST without a submit button


Diegar
 Share

Recommended Posts

What am i doing wrong with this?test2.php

<html><body><form method="POST" action="test1.php"><?PHPecho '<input type="image" name="' . $r .'down" src="images/arrowDown.gif">';?></form></body></html>

test1.php

<?PHP $test = $_POST["name"];var_dump ($test);?>

Clicking the button returns a value of NULL for $test. Every example of POST i have seen shows a submit button. Do i HAVE to use one, or am i just missing something?

Link to comment
Share on other sites

That is a submit button, input type="image" works as a submit button. But what you check for in $_POST needs to be the name of the element. If $r was 1 then $_POST['1down_x'] and $_POST['1down_y'] would be set, $_POST['name'] will only be set if you have an element called "name". The reason it gives x and y values is because when you click on type="image" it submits the x,y coordinates you clicked on.If you want to be able to check in $_POST to see which button was pressed, give all the buttons the same name with different values. Unfortunately, that doesn't work with type="image", when you submit a form by clicking on an image instead of $_POST containing the value of the button that was pressed, it contains the x,y coordinates where the user clicked. So, you need to use normal submit buttons for this, but you can still include an image with them. Sadly though I can't find a way to give the button a value but not show the text on the button, so these buttons will have text on them. But if you give each button the same name (for example "submit") then you can check $_POST['submit'] to see which button was pressed. Run this and see how it works:

<?phpif (isset($_POST))  print_r($_POST);?><html>  <head>	<title>image input test</title>  </head>  <body>	<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">	<input type="submit" name="submit" value="up" style="background-image: url(about:blank); width: 50px; height: 20px; border: none; background-color: transparent;">	<input type="submit" name="submit" value="down" style="background-image: url(about:blank); width: 50px; height: 20px; border: none; background-color: transparent;">	<input type="submit" name="submit" value="remove" style="background-image: url(about:blank); width: 50px; height: 20px; border: none; background-color: transparent;">	</form>  </body></html>

Link to comment
Share on other sites

That didn't work half bad. Thank you! I had an issue of the text still being in the way when i added the background images as buttons, but got rid of that by adding a few spaces to the start of each value. Now i just need to figure out how to use that preg_split() function and i should be good on this.

<input type="submit" name="submit" value="    <?PHP echo $r; ?> down" style="background-image: url(images/arrowDown.gif); width: 13px; height: 17px; border: none; background-color: transparent;">

Though i get the feeling that i am going about this the wrong way, and should probably be using javascript, or something, but i don't want to focus on learning it just yet.

Link to comment
Share on other sites

Okay, so i have instituted the code into my project, and changed the input line to:

<input type="submit" name="submit" value="    <?PHP echo $r; ?>d" style="background-image: url(images/arrowDown.gif); width: 13px; height: 17px; border: none; background-color: transparent;">

and then my action= page code to:

if (isset($_POST))  $full = $_POST["submit"];  $post = str_split($full, 4);  $post = $post[1];  $show = str_split($post, 1);  $line = $show[0];  $action = $show[1];  echo "Line:" . $line;  echo "<br>";  echo "Action:" . $action;

The output for line 1, up, is:Line:1Action:uThis is perfect for me, cause i can now use my $line and $action variables to move things... PHP rocks! Justsomeguy rocks!Jhecht rocks too, for turning me on to the preg_split() function, which led me to the str_split()

Link to comment
Share on other sites

  • 4 weeks later...
Sadly though I can't find a way to give the button a value but not show the text on the button, so these buttons will have text on them.
You can do that with
<button type="submit" value="The value POST will receive">Text to be displayed</button>

But one of the major browsers (and I bet you know which one) has a bug - it sends the displayed text, rather then the value. You could possibly use JavaScript included with conditional comments to alter the displayed text on submit, but that won't really help when JS is disabled, so the application would have to, on the very least, degrade gracefully on that.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...