Jump to content

[SOLVED] Multiple buttons w/ onclick="location.href..." and $_POST: how?


Kat013

Recommended Posts

Hi folks,

I'd like to put several buttons on a page that assemble their own urls, as shown below.  I have drastically simplified it from what I actually want to accomplish, partly to try to figure it out on my own and partly for ease of getting help.  There is some file I/O in php I would like to accomplish on the processing page, so I don't want to resort to javascript.  The problem is that my processing page is acting like it can't read the value passed to it in the url.

 

<form action="" method="post">
<p>
<input type="button" value="Foo"
onclick="location.href= 'processIt.php' + '?' + 'number=foo32';" />

<input type="button" value="Bar"
onclick="location.href= 'processIt.php' + '?' + 'number=bar32';" />
</p>
</form>

 

... and then process them with:

 


<?php
    $itemNumber=$_POST['number'];
    echo "<p>Item Number: ".$itemNumber."</p>";
?>

 

$itemNumber appears to have no value, even though the url assembles correctly, ex:

processIt.php?number=foo32

 

I just get "Item Number:" on the page, and that's all.

 

I've been googling for a couple of hours on this.  Any help appreciated.

POST should be GET?

 

<?php
    $itemNumber=$_GET['number'];
    echo "<p>Item Number: ".$itemNumber."</p>";
?>

 

PS. because you're using double quotes you don't need to remove the variable from the string. In fact, if you're going to remove the variable from the string, you shouldn't use double quotes because it takes (marginally) longer to process (the string has to be parsed by PHP, but the variable was removed from the string so it's parsing that string for no reason wasting processing cycles).

 

This works the same way, and makes the double quotes not redundant.

<?php
    $itemNumber=$_POST['number'];
    echo "<p>Item Number: $itemNumber</p>";
?>

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.