<form method="post">
<input type="submit" name="'.$pTest.'" value="submit" id="submit"/>
</form>
<?php
if(isset($_POST[''.$pTest.'']))
{echo 'something'
;}
?>
This doesn't work if I use variable as the name of the submit button. Please some help!
Try like this
<input type="submit" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
Your variable is doing nothing. Just echo your <input> name and catch it whith php:
<form method="post">
<input type="submit" name="<?php echo $pTest ?>" value="submit" id="submit"/>
</form>
<?php
if (isset($_POST[$pTest])) {
echo 'something'
}
?>
Add value to the variable first..
<?php $pTest = 'sks'; ?>
<form method="post">
<input type="submit" method="post" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
</form>
<?php
if(isset($_POST[$pTest]))
{
echo '<script>alert("something");</script>';
}
?>
Just replace your quotes with double quotes :
if(isset($_POST["'.$pTest.'"]))
{
echo 'something'
;}
Or name your input with a PHP variable :
<input type="submit" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
Related
I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>
can we use $_POST 2 times with different page ?
this example ..
action.php
<form action="next.php" method="post">
<input name="test" value="test" />
<button type="submit" name="test">submit</button>
</form>
next.php
<?php
$test=$_POST['test'];
?>
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="test1">submit</button>
</form>
next1.php
<?php
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ?
?>
Within next.php and action.php you need to change your input type like as
<input type="text" name="test" value="test" />
<input type="text" name="test1" value="<?php echo $test; ?>" />
Within next.php and action.php you were missing the type attr of input tag
and you have same name attributes for submit and input within next.php need to remove or change the value from respective
<input type="submit" value="submit"/>
Below code i have tried my local server and it executed successfully.
action.php:-
<form action="next.php" method="POST">
<input type="text" name="test"/>
<input type="submit" value="submit"/>
</form>
next.php:-
<?php
$test = $_POST['test'];
echo $test;
?>
<form action="next1.php" method="POST">
<input name="test1" value="<?php echo $test; ?>" />
<input type="submit" value="submit"/>
</form>
next1.php:-
<?php
$test2=$_POST['test1'];
echo "------".$test2;
?>
Hope this will help.I think this is achieved your requirement.
If you want to do it within one file e.g. index.php then here is the code
<?php
if(isset($_POST['test'])){
if(isset($_POST['test_text'])){
echo 'Output from NEXT: '.$_POST['test_text'];
}
}
elseif(isset($_POST['test1'])){
if(isset($_POST['test_text1'])){
echo 'Output from NEXT1: '.$_POST['test_text1'];
}
}
?>
<table style="width:100%;">
<tr >
<td style="width:50%;">
<form action="" method="post" name="next">
<input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" />
<button type="submit" name="test">submit test1</button>
</form>
</td>
<td style="width:50%;">
<form action="" method="post" name="next1">
<input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" />
<button type="submit" name="test1">submit test1</button>
</form>
</td>
</tr>
</table>
I hope this will help.
You need to send the POST value with the second form again, e.g. in a hidden field, otherwise the value won't be sent.
Furthermore your submit button shouldn't have the same name as the input field you want the content from. So change the name of your submit button, e.g.:
action.php
<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form>
next.php
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="submit">submit</button>
</form>
I have tried to put this code in my webpage but I have this error :
Parse error: syntax error, unexpected '?' in E:\MyServer\htdocs\...(line 1 from this script)
CODE :
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
this code is in a .php page
Thanks.
Try to format it properly:
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
How to get the value you pass through your form:
Welcome <?php echo $_POST["name"]; ?><br>.
EDIT
Also you should add this in delete.php file:
if(isset($_POST['name'])){ $name = $_POST['name']; }
See : http://www.w3schools.com/php/php_forms.asp
You need to drop the double quotes around the variable name and move them to the actual element property.
<form action='delete.php?name="<?php echo $contact['name']; ?>"'
^ ^
It then becomes
<form action="delete.php?name=<?php echo $contact['name']; ?>"
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
This should work ..
Do you need to send the data via GET and POST to delete.php?
<form action='delete.php' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
With this you can then just do $_POST['name'] to get your contact name...
Sorry if I have missed something but cannot see why you would need it passed twice
I have this code that fetches information in a file and shows the result. That all works as expected. What I'm not able to do is to export the button value in the function when it is pressed so the value of the pressed button will transfer to a variable in my php function. How can I do that?
Here the code:
<h2>Demmarage script torrent</h2>
<form action="search.php" method="POST">
<input type="text" name="input_value">
<input type="submit" name="submit">
<?php
echo "<br>";
if (isset($_POST['submit'])){
$findme = $_POST['input_value'];
$findme1 = str_replace (" ", ".", $findme);
$savedarr = unserialize(file_get_contents('torrent.bin'));
foreach ($savedarr as $val1){
$mystring = $val1['title'];
if((stripos($mystring, $findme) !== false) or (stripos($mystring, $findme1) !== false)) {
echo "Show trouve: ";
echo $mystring;
?>
<button type="submit" value="<?php echo $val1['link']; ?>" name="editId">Telecharger</button>
<?php
echo "<br>";
}
}
}
if (isset($_POST['editId'])){
//Here i want to import the value of the pressed button to do something
echo "download start";
}
?>
you should use a hidden field, like
<input type="hidden" name="link" id="link" value="<?php echo $val1['link'] ?>" />
After the submit you will be able to get the value with
$_POST['link']
Also, this should be in a form,otherwise the submit will post EVERY field ...
like
foreach(...)
{?>
<form action="" method="POST">
<input type="hidden" name="link" id="link" value="<?php echo $val1['link'] ?>" />
<input type="submit" />
</form>
<?php}
The problem is that you are setting the value twice by <button>value1</button> and by <button value="value2">... remember that the value of a submit button is always the text that shows up inside the button.
You should do replace this:
<button type="submit" value="<?php echo $val1['link']; ?>" name="editId">Telecharger</button>
with this:
<form method="POST">
<input type="hidden" value="<?=$val1['link']?>" name="editId" />
<input type="submit" value="Telecharger" />
</form>
This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>