Re: Php code in html buttons

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Jul 10, 2007, at 3:59 PM, k w wrote:

I'm trying to make a button execute some php code when the button is
clicked. I'm not sure if it is the button i'm coding wrong or the php code.
Here is the code I am using.

<?php
echo "<button action='<?php mysqli_query($connect,$query)?>'Click</button>";
?>

I've got all my variables stored in the php page, and I know they are all correct. But when I push the button it does nothing. I'm not quite sure what
I am doing wrong.

I have also tried

<input type=button value="Click" OnClick= "<?php
mysqli_query($connect,$query); ?>">

and

<form onclick="<?php mysqli_query($connect,$query); ?>">
<input type=button value="Click">
</form>

Obviously i'm a complete newbie. I'd appreciate any help. Thanks.

You will have to use a form and set the action to the page that runs the code, It can be the same page. In which case the action in the form open tag will be the name of the present file. You have to have code in the top of the page to catch a $_POST or $_GET variable.
 The button will look something like this:

<form action='your code processing page.php' method='get or post, you choose' name='form1'> NAME HERE IS IMPORTANT <input type='button' name='you name the button' onclick='document.forms,form1.submit(form)'> THIS IS WHY THE FORM NAME ASSIGNMENT IS IMPORTANT <!-- The document.forms.form1.submit(form) line is javascript. In most browsers the word form must be entered as an argument for it to work. --> <input type='hidden' name='you name this hidden field' value='some value for the processing code to look for so it knows when to do your processing'>
</form>

In the top of the page you can do:
$some_variable = '';
if($_POST[hidden field name] or $_GET[hidden field name])
  {
   // run your code here
  // assign the result to $some_variable
// assign extra hidden field values to variables that should persist across page loads
}

Then in the page in the location you want it:
<?php print $some_variable; ?>

there is one catch, if you have variable values in the page before you click the button and you want them to be there when the page reloads, or another page sends the result, these variable values will also have to be loaded into hidden fields, sent to the server and re entered in the
returned data.
You can add hidden fields like so:
<input type='hidden' name='unique name for each field' value='<?php print $your_variable; ?>''>
This will work with single or double quotes around the php code block
Each of these extra form fields will have to be inside the same form tag set that the button is in.
JK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux