How can I get/extract the name value of a submit button? - php

When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...

So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}

I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?

Related

Change certain text from User input (From a User Input field)

I want to have a simple HTML input field where people can type all kinds of nonsense. For example, a user types: "Hello, I'm Nicky". When the user then clicks the button Send, I want a simple PHP script to replace the word "Nicky" to "Nicki" and show it to the user. So basially, just a simple PHP script which replaces specific words from an input field and then print out the exact same line the user has inputted, except show Nicki instead of Nicky.
How can I achieve this, in the most simplest way?
My code looks like this now:
<?php
$_POST['name'] = str_replace("Nicky","Nicki",$_POST['name']);
?>
<form method="post">
<input type="text" name="name">
<input type="submit">
</form>
<?php
if(isset($_POST['form-action']) && $_POST['form-action'] == "submit-form"){ // form has been submitted
echo "<p>BEFORE: ".$_POST['name']."</p>"; // what the user entered "Nicky"
$_POST['name'] = str_replace("Nicky","Nicki",$_POST['name']); // find/replace Nicky with Nicki
echo "<p>AFTER: ".$_POST['name']."</p>"; // what the $_POST['name'] now is
}
?>
<form method="post">
<input type="text" name="name" value="Nicky">
<input type="submit">
<input type="hidden" name="form-action" value="submit-form">
</form>
In addition to this, if you want to expand the Find & Replace variables, you could use an array:
$FindReplace = array("Nicky"=>"Nicki", "Blue"=>"Red"); // build an array of find/replace variables
....
foreach($_POST as $Name=>$Value){
echo "<p>Before: ".$Name."=".$Value."</p>"
foreach($FindReplace as $Find=>$Replace){
$Value = str_replace($Find,$Replace,$Value);
}
echo "<p>After: ".$Name."=".$Value."</p>"
}

Send form data to the same page not functionning

I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.

trouble with extracting form data from another php page

I want to create a checkbox and check whether checkbox is checked or not in another PHP page so I created form like :
<form action="heartbeat.php" method="post">
<input type="checkbox" name="keepme">
<input type="submit" value="log in">
</form>
and then I tried to extract this input on heartbeat.php like:
<?php
/*
* A PHP file for laying down a heartbeat JavaScript call.
*/
if(!isset($_POST["keepme"])){
$auto_logout = 10;
}
else{
$auto_logout = 1000;
}
?>
but it never gets $_POST["keepme"] value. Any idea??
If you don't pass value to checkbox tag, the POST array will be empty. You need to do something like that
<input type="checkbox" name="keepme" value='1'>
Make Sure you have submit button
If(isset($_POST['submit-btn'])){
echo $_POST['keepme'];
}

Returning only the last value of query

This is my second code but the problem is I have 3 queries. So it only returns the last product_id when i Click update it always return product_id=3, but i want update the product_id=2
<form action="update_qty.php" method="POST">
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
<?php } ?>
</form>
Your problem is that the PHP is server side and you need something client side to read the value of the text box. You would need a page refresh to pass the text field value to the server so it could write it to the url in the anchor tag. Which is what the form submit would do, but as it would have submitted the value already the anchor tag would be pointless
To do it without a page refresh use Javascript. It would be easy to do with jQuery. You could add an event that writes whatever is entered in the text box the the anchor tags href as it is typed.
I'll do something more like this.
One form per product.In your case when you submit the form the qty value will always be the las found.
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<form action="update_qty.php" method="POST">
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
</form>
<?php } ?>
You can add more information like this
update
You can not get all values as like that because input name overwrite in every loop iteration.
For multiple values you can try in two ways like:
<?php
while($getorder = mysqli_fetch_array($order)){
$newArr[] = $getorder['price']."~".$getorder['product_id']."~". $ getorder['qty'];
} //while end
?>
<input type="hidden" name="allinputs" value="<?=$newArr?>">
Input field outside the loop.
In php explode array value with ~ and get the all values.
Other solution is that
Your input field name must be change like:
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price_<?=$getorder['product_id']?>">
<?php } ?>
Change field name in every iteration.
In current scenario either you need three different buttons or the best solution to use AJAX request .
update
On update_qty.php u can use like this
<?php echo $_GET['product_id'];?>

Include value of submit button on forms

I have two submit buttons in a form which has different values, one to voteup a post and another to votedown a post, how do i submit the value of the input type="submit" buttons?
Without using javascript, i'm making my script flexible to browsers with javascript disabled.
<?php
if (isset($_POST)) {
var_dump($_POST);
}
echo '
<form action="" method="post">
<input type="submit" name="vote_up" value="up">
<input type="submit" name="vote_down" value="down">
</form>';
?>
The value of the clicked button will always be submitted with the form. This is how you can disambiguate between different submit buttons on the same form.
Further clarification can be found at: http://www.javascript-coder.com/html-form/html-form-submit.phtml
Just include it into the form you are submitting and do something like this for disambiguate the buttons:
<input class="submitButton" type="submit" name="up" id="up">
Then in your code you can do something like:
if (isset($_POST["up"])){
// do something
}
you can also use
print_r($_POST);
in place of
var_dump($_POST);

Categories