I'm trying to submit two form field values to a PHP function, on the same page.
The function works perfect manually filling the two values.
<?PHP // Works as expected
echo "<br />"."<br />"."Write text file value: ".sav_newval_of(e, 45);
?>
On the form I must be missing something, or I have a syntax error. The web page doesn't fail to display. I found the below example here: A 1 Year old Stackoverflow post
<?php
if( isset($_GET['submit']) ) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
<?php if( isset($result) ) echo $result; //print the result of the form ?>
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1"></input>
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2"></input>
<br></br>
<input type="submit" value="send"></input>
</form>
Could it be the placement of my code on the form?
Any suggestions appreciated.
You need to name your submit button, or check for something else on your if(isset($_GET['submit'])) portion:
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1" />
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2" />
<br></br>
<input type="submit" name="submit" value="send" />
</form>
OR keep same form, but change php to:
<?php
if( isset($_GET['val1']) || isset($_GET['val2'])) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
You can you hidden field as:
<input type='hidden' name='action' value='add' >
And check on php by using isset function that form has been submitted.
Related
Here is a code that I've made:
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['name'])){
$sum+=1;
}
echo "sum = $sum";
?>
When I enter some text in the form and click Validate, the page display sum=1, but after this, when I enter nothing in the form and click Validate, the page STILL displays sum=1.
Why does the variable $sum is not reloaded between the two Validate ? Is there a way to escape it ?
Thanks
This will solve the issue
<?php
$sum=0;
if(isset($_POST['name']) && $_POST['name'] != ''){
$sum+=1;
}
echo "sum = $sum";
?>
This is because isset() checks for the existence of the $_POST variable. In your case, the $_POST variable exists and has an empty string value.
Your code will work if you change isset() to !empty() like so;
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(!empty($_POST['name'])){
$sum+=1;
}
echo "sum = $sum";
?>
More about the empty() function here.
Another way would be to check the request that your client has made on your page. So that if it is a simple refresh (not with a form refresh), it is a GET request and so, the variable should not be incremented and if the form has been sent, then you can do whatever you want such as incrementing the data.
So if the client is sending the form with an input text filled, then you can increment the value. In all other cases, the value should remain a zero.
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['name']) && !empty($_POST['name']))
{
$sum++; /* strictly equivalent to: $sum += 1; */
}
?>
<samp>sum = <?php echo $sum; ?></samp>
Try this
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" name="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['submit'])){
$sum+=1;
}
echo "sum = $sum";
?>
You can try below:
if(isset($_POST['name']) && strlen($_POST['name'])>0){
$sum+=1;
You have the code appending 1 to variable $sum
but your if statement is based on the name field being passed.
Not if the name field has any data in it.
So... you have made your code add 1 as long as name field is passed,
regardless if it has text input or not.
Also, you should reassign the varible to reset it.
+= should just be =
<form method="post" action="test.php">
//----------------------------- add empty value to input ------------
<input type="text" name="name" value="" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['name'])){
$sum=1;
}
echo "sum = $sum";
?>
I am trying want to ouput a string ,stored in a variabel, in a label on a page when i click on a button.
But i can't find out how. Still a beginner.
<form action="Test.php" method="post">
Output text: <input type="label" name="word" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
if (isset($_POST['submit']))
{
//something that gives the label value $word//
}
?>
There are a few things wrong with your code.
Let me outline them.
Your submit input should have a name attribute, since your conditional statement is based on it if (isset($_POST['submit'])){...}, something I've modified to check if the input is not left empty, using PHP's empty() function.
The input type you have for your "Output text" is invalid, it should be type="text" and not type="label", there is no type="label".
method="submit" for your submit button is invalid for a few reasons. Method belongs in <form> and there is no method="submit".
You then need to assign a POST variable from the input:
such as:
$word = $_POST['word'];
Plus, from what looks to me that you're executing the entire code from within the same page, you can just do action="", unless your code is set in 2 seperate files.
In regards to what you want to achieve: You can then echo the input (if one was entered) using a ternary operator and giving it (the input) a value.
I.e.:
value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>"
Here:
<form action="" method="post">
Output text: <input type="text" name="word" value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>" />
<input type="submit" name="submit" value="Print!" />
</form>
<?php
if ( isset($_POST['submit']) && !empty($_POST['word']) )
{
$word = $_POST['word'];
echo $word;
}
?>
If you want to use a "label" for your input, then use:
<label for="word">Output text:
<input type="text" name="word" />
</label>
You should also guard against XSS attacks (Cross-side scripting) using:
http://php.net/strip_tags
http://php.net/htmlentities
http://php.net/manual/en/function.htmlspecialchars.php
I.e.:
$word = strip_tags($_POST['word']);
$word = htmlentities($_POST['word']);
$word = htmlspecialchars($_POST['word']);
A few articles you can read on XSS:
http://en.wikipedia.org/wiki/Cross-site_scripting
https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet
Your code should look like this
<form action="Test.php" method="post">
Output text: <input type="label" name="word" value ="<?php echo isset($label['data'])?$label['data']: '' ?>" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
$label = array();
if (isset($_POST['submit']))
{
//something that gives the label value $word//
$label['data'] = $word;
}
?>
This should work
Regards
Ahmad rabbani
First of all your input element has type = label, it doesn't mean anything. Change it to type=text
And you are submitting value but not printing it. So in input field you have to print it also.
Look below code.
<?php
$word = "test";
if (isset($_POST['submit']))
{
// whatever you do with $word
}
?>
<form action="Test.php" method="post">
Output text: <input type="text" name="word" value="<?php echo $word; ?>"/>
<input type="submit" name="submit" value="Print!" />
</form>
UPDATE
One more thing I forgot to mention that you are submitting form to Test.php and printing this to file, if this file's name is Test.php then not an issue, other wise leave action property blank, so it submit data to itself.
method = submit there is nothing like this. you can set button name to submit, like name= "submit".
I have 3 textfields and only one submit button to send all the data of the textfield at once. But how do I send the data of all three textfields at once in php?
<form>
for($x=0;$x<3;$x++){
<input type="text" name="name">
}
<input type="submit" name="submit">
</form>
Now I have a three fields inside a for loop and I have to extract data from all of them using single submit button.So how can I do that?
Using the 'name' attribute on an input allows you to do this for example
<form action='submit.php' method='post'>
<input type='text' name='one'></input>
<input type='text' name='two'></input>
<input type='text' name='three'></input>
<input type='submit' name='submit' value='Submit!' />
</form>
and in your PHP you would do something like this
<?php
if(isset($_POST['submit'])){
$inputOne = $_POST['one'];
$inputTwo = $_POST['two'];
$inputThree = $_POST['three'];
//Do whatever you want with them
}
?>
There are better ways of doing this, but this is probably the simplest to understand
If you want all the inputs to have the same name do this
<input type='text' name='textinput[]'></input>
Use that instead and loop through all of the inputs like so
<?php
foreach($_POST['textinput'] as $input){
//do something with $input
}
?>
You put them all inside the same <form> and make sure they have different values for the name attributes (or that the values end in []).
I believe what you are looking for is this Note the [ ] behind the name field
<form>
for($x=0;$x<3;$x++) {
<input type="text" name="name[]" />
}
<input type="submit" name="submit" />
</form>
Then to retrieve the values
$names = $_POST['name'];
foreach( $names as $name ) {
print $name;
}
This form should calculate numbers and save
Now there are two buttons One is call Calculator and two call Save
If I press Calculator
I get the form action is going to file name save.php And I do not want it that way
How can I set it up that button do something else
Example
Calculator = Calculator
Save = save.php
Is it possible to set it
Because it is one form
Thanks to anyone who can help
<?php
error_reporting (0);
$NUM = $_POST["NUM"];
$NUM2 = $_POST["NUM2"];
$NUM = "$NUM";
$NUM2 = "$NUM2";
$subtotal= $NUM+$NUM2;
?>
<form action="save.php" method="POST" name="Calculator">
<p>
<input name="NUM" type="text" value="<?php echo $_POST["NUM"]; ?>" />
</p>
<p>+</p>
<p>
<input name="NUM2" type="text" value="<?php echo $_POST["NUM2"]; ?>" />
</p>
<p>
<input name="subtotal" type="text" value="<?php echo "$subtotal";?>" />
</p>
<p>
<input name="submit" type="submit" value="Calculator" />
<p>
<input name="submit" type="submit" value="Save" />
</p>
</form>
You can have all the logic in a single PHP script (no need to direct to a different script depending on the button). If the logic is complicated, use include statements in order to separate the code.
Name the buttons differently:
<input name="calculator_submit" type="submit" value="Calculator" />
<input name="save_submit" type="submit" value="Save" />
Then in PHP:
if (isset($_GET['calculator_submit'])) {
// ...
} else if (isset($_GET['save_submit'])) {
// ...
} else {
// ...
}
If you really need different PHP script, then you'll have to go with Javascript (function will change the form action when a submit is clicked).
Since you are now using two submit buttons, both will submit the form and go to save.php.
Make your "calculator" button an input type=button instead of submit, and handle it via JavaScript.
Just FYI:
HTML5 allows to define a different form target URL by specifying the formaction attribut on a submit button – but browser support is lousy as of now.
Form and Buttons
<input name="submit" type="button" onclick="submitForm('Calculator')" value="Calculator" />
<input name="submit" type="button" onclick="submitForm('Save.php')" value="Save" />
Some jquery:
function submitForm(path) {
$('#Calculator').attr('action', path);
$('#Calculator').submit();
}
Sorry if this is a rather basic question.
I have a page with an HTML form. The code looks like this:
<form action="submit.php" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input type="submit" />
</form>
Then in my file submit.php, I have the following:
<?php
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
?>
However, I want to eliminate the use of the external file. I want the $_POST variables on the same page. How would I do this?
Put this on a php file:
<?php
if (isset($_POST['submit'])) {
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
}
?>
<form action="" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input name="submit" type="submit" />
</form>
It will execute the whole file as PHP. The first time you open it, $_POST['submit'] won't be set because the form has not been sent.
Once you click on the submit button, it will print the information.