forward variable to php script on button click - php

I have this HTML button:
<form action="testscript.php" name ="more" method="get">
<input type="submit" value="More Details">
</form>
Which when clicked runs this PHP script:
<?php echo 'button pushed: ';
echo $buttonvalue;
?>
My question is, how to I assign a value to the button so that when its clicked it will forward its value ($buttonvalue) to the PHP script
For example when the button is clicked i want it to run the script and the result should be button pushed: blue
How do i assign "blue" to the button?

you can try this
HTML :
<form action="testscript.php" name ="more" method="get">
<input type="hidden" name="buttonvalue" value="Some Value">
<input type="submit" value="More Details">
</form>
PHP :
<?php
echo 'button pushed: ';
echo !empty($_GET['buttonvalue']) ? $_GET['buttonvalue'] : '';
?>

Use anchor tag with 'btn' class. And pass value to next page with id.
Page 1.
<a class="btn btn-primary" href="next-page.php?id=value">Click me</a>
Page 2.
$buttonvalue = $_GET['id'];
echo $buttonvalue;

<form action="testscript.php" name ="more" method="get">
<input type="hidden" name="getDataName" value="123">
<input type="submit" value="More Details">
</form>
PHP script:
<?php
$buttonvalue= isset ($_GET['getDataName']) ? $_GET['getDataName'] : '';
if(!empty($buttonvalue)){
echo 'button pushed: ';
echo $buttonvalue;
}else{
echo 'button not pushed: ';
}
?>

Related

Losing GET variable on form submit or on refresh

I am sending an id from a link
<a class="suba" href="empty.php?id=1" ></a>
and recieving this id in the empty.php page :
require_once 'connection.php';
session_start();
$id=$_GET['id'];
on refresh or on form submit the variable $id is lost :
<form action="" method="get">
<input type="submit" name="add" value="next">
</form>
<?php if(isset($_GET['add']) && $_SESSION['j'] < $filecount){
$_SESSION['j']++;
echo '<input type=hidden name=id value=' .$id. '>';
} ?>
I tried adding a hidden input in the form but it's not working also tried saving the variable in a session:
$_SESSION["word"] = $id;
You must print the Id in your form action
<form action="?id=<?= $id ?>" method="get">
<input type="submit" name="add" value="next">
</form>

Can't figure out why isset is not working

I have a button that I need to call a function on when it is clicked but for some reason, the isset function is not seeing the button as being clicked.
Below are the two lines in the php/html file:
<input type="submit" id='details' name='details' class="pull-right btn-sm btn-primary" value='Show Details'/>
<h4><?php echo $subject_details; ?></h4>
The below is in an external. I know it is included because if I add an else to the if statement, and set that variable to something different, it will change.
$subject_details = '';
if(isset($_POST['details'])){
$subject_details = 'button clicked';
setDetails();
}
Thanks to anyone looking at this!
The isset() function only works for input type submit. What you can do is embed the button in a form and let the page reload to itself to grab the variable stored in an external php. As an example:
//in your main.php
<?php
include 'external.php';
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" value="Send">
</form>
<h4><?php echo $subject_details; ?></h4>
//in your external.php
<?php
$subject_details = '';
if(isset($_POST['submit'])){
$subject_details = 'button clicked';
}
?>
Without seeing the full code example, I can't be sure, but it's possible you're not specifying the method="post" attribute on the <form> tag. A form defaults to method="get" therefore you would need to use isset($_GET['details']) instead.
A working example would be:
<form action="" method="post">
<input type="submit" name='details' value='Show Details'>
</form>
<?php
if (isset($_POST['details'])) {
echo $_POST['details'];
} else {
echo 'No post data submitted';
}
you could play around with this... might help
<!-- head section -->
<script>
window.onload=function(){
var oCol=document.querySelectorAll('input[type="button"]');
for( var b in oCol ) if( b && oCol[b] && oCol[b].nodeType==1 ) oCol[ b ].onclick=function(e){
alert( this.tagName + ' '+this.id+' was clicked...' );
}.bind( oCol[b]);
};
</script>
<input type='button' id='b1' value='Button 1' />
<input type='button' id='b2' value='Button 2' />
<input type='button' id='b3' value='Button 3' />
<input type='button' id='b4' value='Button 4' />

Multiple Submit buttons, how do determine which one was clicked?

I have a form with multiple submit buttons.
Each submit button is an IMG SRC trash can which denotes the delete icon for messages in a web based messaging mail inbox
what is the best way to figure out which submit button icon was clicked so that I can then write the PHP/MySQL code to DELETE the message?
if(!empty($_POST)){
// How do I figure out which submit button has been clicked to get the ID of the message to delete?
}
<form method="POST">
<input src="http://www.foo.com/img.png" id="button_1">
<input src="http://www.foo.com/img.png" id="button_2">
<input src="http://www.foo.com/img.png" id="button_3">
<input src="http://www.foo.com/img.png" id="button_4">
...
<input src="http://www.foo.com/img.png" id="button_100">
</form>
Set value for each submit button and check that in php and find which one is clicked
<form method="POST">
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1">
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2">
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3">
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4">
...
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100">
</form>
echo $_POST['submit_btn']; will give you the value of which submit button is clicked
Give each button a name=""
Then you can do something like
isset($_POST['button_name']) {
// execute code here if true
}
THE solution of this problem is to use the NAME attribute of the tag input/button.
<input type="submit" name="submitSave" value="Save"/>
<input type="submit" name="submitAddComment" value="Add comment"/>
or
<button type="submit" name="submitSave">Save</button>
<button type="submit" name="submitAddComment">Add comment</button>
I think you can also use the value attribute of button tag, this is definitively not possible with input tag.
If you need to use an ID or another variable, use name="submitDelete[888]"
Then, check it with PHP:
if( isset($_POST['submitDelete']) ) {
echo key($_POST['submitDelete']);// Displays the ID to delete, e.g. 888.
}
So many years later, I like button because it allows to display a text or an image independently of the value returned.
Here is an illustration of possibilities which fits the title of this post and more cases than the OP.
<?php
if(!empty($_POST['id'])){
echo 'button '. $_POST['id'] .' clicked';
} elseif ('create' === ($_POST['action'] ?? '')) {
echo 'create clicked'; // ?action=create
} elseif (isset($_POST['action'])) {
echo 'refresh clicked'; // ?action
} elseif (isset($_POST)) {
echo 'Default clicked'; // ?
}
?>
<form method="POST">
<!-- Original Post examples -->
<button type="submit" name="id" value="1"><img src="http://www.foo.com/img.png"></button>
<button type="submit" name="id" value="2"><img src="http://www.foo.com/img.png"></button>
...
<button type="submit" name="id" value="100"><img src="http://www.foo.com/img.png"></button>
<!-- Additional possibilities -->
<!-- ?action=create -->
<button type="submit" name="action" value="create">New element</button>
<!-- ?action -->
<button type="submit" name="action">Refresh</button>
<!-- ? -->
<button type="submit">Default</button>
</form>
you can give a name and a value to each of your buttons. It will then show up under $_POST['submit']
<img src="http://www.foo.com/img.png" id="button_4" name='submit' value='4' />
You have to pass your value to the current file by declearing name and value for each.. then you can echo in your php script in order to know which one is clicked.

How do I give a value to an href as its done to a submit?

I named a submit and an href and when I test the conditions submit works but the href does not and there is no error displayed . Please any help will be appreciated
Bellow is the code
<form action="test.php" method="post"/>
<a href="#" name="amhref" >Href</a>
<input type ="submit" name="button" value="button">
</form>
<?php
if(isset($_POST['amhref'])){
echo ("<h1>I am href </h1>");
}else if(isset($_POST['button'])){
echo ("<h1>I am the button</h1>");
}
?>
a link <a> is not sent with a form. so any data you would like to pass should be done through the href field. this is done with a get method, so please note the $_GET
<form action="test.php" method="post"/>
<input type ="submit" name="button" value="button">
</form>
<a href="?amhref" name="amhref" >Href</a>
<?php
if(isset($_GET['amhref'])){
echo ("<h1>I am href </h1>");
}else if(isset($_POST['button'])){
echo ("<h1>I am the button</h1>");
}
?>
Using an <input> control to achieve this. I have used a hidden control.
<input type="hidden" name="id1" value="hello">
You can't do with an <a> tag
tag not element of form type so that you cant pass to in form submite. If you want to pass keep amhref value in input type text or Hidden.
You can pass value with href with GET method also.
<form action="test.php" method="post"/>
<input type ="submit" name="button" value="button">
</form>
<a href="?amhref" name="amhref" >Href</a>
<?php
if(isset($_GET['amhref'])){
echo ("<h1>I am href </h1>");
}else if(isset($_POST['button'])){
echo ("<h1>I am the button</h1>");
}

Change hidden field value on Submit click

I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">

Categories