How does checking for "submit button is pressed" work? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm curious about this kind of behavior. Suppose the code below:
<form name="myfrom" action="" method="post">
Username: <input type="text" name="user" id="username" />
<input type="submit" name="submit_form" value="Submit" />
</form>
<?php
if($_POST['submit_form'] == "Submit") {
echo "do something";
}
?>
What is the reason it works? Is that because when you click on the submit button the page reloads again and then the PHP script runs again? Is there an explanation?

If you mean, how to test when the form is submitted try this:
<form name="myfrom" action="" method="post">
Username: <input type="text" name="user" id="username" />
<input type="submit" name="submit_form" value="Submit" />
</form>
<?php if(isset($_POST['user'])) {
echo "do something";
}
?>
and if you test if($_POST['submit_form'] == "Submit") you get true.. i.e 1...
When ever you submit any PHP form with POST method then the following happens behind the scenes:
The server takes the values of all the HTML input elements and takes their names too...
Then the server, puts the names and values in the POST array in the following fashion:
{'name1'=>'value1','name2'=>'value2','name3'=>'value3','name4'=>'value4'...'name_n_'=>'value_n_'}
You change the value of a text field by entering some text into it. However, the user can't really change the value of a submit button, i.e. the displayed text on a submit button, hence whenever you say echo $_POST['submit_form'] you will always get the value that you set in HTML i.e. Submit
hope that helps...

Related

PHP running 2 scripts at the same time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I hope you can help. I'm a new to PHP and it is driving me crazy!
I have a html document with separate login and registration forms. Each one of these has it's own php script to either register or to login. When testing the input error messages for either the login or registration forms it seems to run both scripts and I get the error messages for both.
I have spent most of today trying to find a solution to this but to no avail. Is there a way that I can define a name to each script so I can add an action to each form tags referring to the particular php script?
Or This there a way of using a php if else statement based on which html button is pressed?
Thank you in advance
Hopeless coder
Or This there a way of using a php if else statement based on which
html button is pressed?
Yes, assuming you have
<input type='submit' name='subbtn' value='Register'>
...
<input type='submit' name='subbtn' value='Log In'>
Then in php:
if ($_REQUEST['subbtn'] == 'Register') {
// they pressed register
} else {
// they pressed log in (or some other submit button)
}
You could attach a hidden element to your post method
<input type="hidden" name="type" value="login">
or
<input type="hidden" name="type" value="register">
The above should be in respective forms.
On the PHP page
<?
if($_POST['type'] == "login") {
// continue login operation
} else {
// do registration
}
?>
Sure there's a way to separate them into two files and then call for action separately.
<form action="registration.php">
...
</form>
<form action="login.php">
...
</form>
or there's another way to do it in one document
<form action="" method="POST">
...
<input type="submit" name="btn_register">
</form>
<form action="" method="POST">
...
<input type="submit" name="btn_login">
</form>
<?php
if(isset($_POST['btn_register'])) {
//Do the stuff with registration
}
if(isset($_POST['btn_login'])) {
//Do the stuff with login
}
?>

Php simple input to array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Recently I started with PHP and there's something im trying to find out.
This is what I want:
Input field + Submit.
When u submit the form, the value of the input field goes in an array. lets say $array = array();
Every time you submit the value gets put in that array.
I've done things with $_GET and $_POST and other things like $_SESSION but I just can't get this to work..
Help would be appreciated! thanks!
EDIT: The information (list) will just be showed on the page. Not send to a mysql database or anything. When u refresh the page it would be gone.
It sounds like you want to keep the data that you submitted.
index.php
<?php
session_start();
if(isset($_POST['a_value']))
{
if(!isset($_SESSION['a_value']) || !is_array($_SESSION['a_value']))
{
$_SESSION['a_value'] = array();
}
array_push($_SESSION['a_value'], $_POST['a_value']);
}
?>
<form action="index.php" method="POST">
<input type="text" name="a_value">
<input type="submit" value="Go">
</form>
<?php
if(isset($_SESSION['a_value']))
{
echo '<br><br>Values so far:<br><pre>'.print_r($_SESSION['a_value'], true).'</pre>';
}
?>
there some better way for do this, but you can use the session like this:
you need 2 session variable for this aim, first for counting the array index, and second for value...
here is an example
<form method="post" name="frm" action="#">
<input type="text" name="txt" id="txt" />
<input type="submit" name="btn" value="submit">
</form>
<?php
session_start();
if ( isset($_POST['btn']) ) {
$_SESSION['counter'] += 1;
$_SESSION['val'][$_SESSION['counter']] = $_POST['txt'];
var_dump($_SESSION);
}
?>

Send html form to variable php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a html form, and i want that when u send the value of the html, to be a php variable
<form action="callback2.php" method="POST">
Telefoon nummer invoeren: <input type="text" size="10" name="nummerb" id="nummerb"> <input type="submit" value="submit">
this is the html and the value of this should be
$nummerb ="" ;
so the nummerb should have the value of the sended form.
You said in a comment:
i know that it should be $nummerb = $_POST ['nummerb']; but when i put
that in an run it, it says Undefined index, so it means it can't find
the 'nummerb'. So i should put ifisset right?
So the answer is:
$nummerb = "";
if(isset($_POST['nummerb']))
$nummerb = $_POST['nummerb'] ;
You also can check first if you got a form submission to make sure you do not check for the $nummerb if the user didn't pressed on the Send button:
add a name to your submit button
<input type="submit" name="submitButton" value="submit"/>
check if the user clicked on the submit button:
<?php
// If the user pressed on submit button
if(isset($_POST['submitButton'])) {
// Get the nummerb
$nummerb = "";
if(isset($_POST['nummerb']))
$nummerb = $_POST['nummerb'] ;
// Do your job here
}
?>
The answer to this very basic and silly question is:
$nummerb = $_POST["nummerb"];
You should learn more PHP before asking another question here.
The answer to your question could have been found within the PHP manual.
A simple Google query for php post returns the answers you are looking for.
http://php.net/manual/en/reserved.variables.post.php
You can access nummerb by this :
$nummerb = $_POST['nummerb'] ;
Or if you don't want to create "manually" the variables, use extract function :
extract($_POST);
echo $nummerb;
I am not sure for what you are looking.
But your html should be:
<form action="callback2.php" method="POST">
Telefoon nummer invoeren:
<input type="text" size="10" name="nummerb" id="nummerb">
<input type="submit" value="submit" name="submit">
</form>
And php code should be like that:
if(isset($_POST['submit'])){
$nummerb = $_POST['nummerb'] ;
}

Formula Calculator Form [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking to build a simple one-field form that will take the number entered and run it through a formula and display the result underneath on a webpage for a school project. Similar to a paypal fee calculator. NO more, no less.
I can handle HTML, CSS, JS, but am pretty PHP-tarded.
Is there something I can read to learn how to go about this? I tried searching but am not sure exactly what I should be searching for.
Thanks
Name the field that you are entering, i.e.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="value" value="">
<input type="submit" value="Submit">
</form>
<?php
$value=$_POST['value'];
if ($value!=""){
//RUN FORMULA HERE:
$total=$value * 1.02;
echo $total;
}
?>
Using this method will allow you to continue entering numbers until you are done.
Next time see php manual or google... there is a lot of similars...
<?php
if(!isset($_POST['number']))
{
?>
<form method="POST">
<input type="text" value=0 name='number'/>
<input type="submit" />
</form>
<?php
}
else
{
$num = $_POST['number'];
$result = ($num * $num) + $num;
echo $result;
}
?>

click submit in html and run php code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to html & php and I appreciate your help. I am trying to accept user input into a webpage and then after the user clicks the submit button, write a new record out to MySQL. I am using Wordpress so the tags are not exactly textbook.
So my first problem is that the submit button does not appear to execute the php code. I also would appreciate it if someone could check how I am passing my html field names to the php variables to make sure that I am on the right track. Thanks for your help!
html code to collect user input for fields on the form.....
<?php>
if(isset($_POST['Submit'])) {
$FName = $_POST['FName'];
$MI = $_POST['MI'];
$LName = $_POST['LName'];
....
?>
<form action="" method="post">
<input type="submit" name="Submit" value="Submit">
I think it can be good to read a bit more about it. Check here.
Anyway, you need to say the name of the file to post to: <input type="submit" action="file.php" name="Submit" /> for example.
And you need to have more inputs than the submit.
Acording to your php you should have as example this in the html:
<form action="your_php_file.php" method="post">
<p>Your first name: <input type="text" name="FName" /></p>
<p>Your last name: <input type="text" name="LName" /></p>
<p>Your MI: <input type="text" name="MI" /></p>
<p><input type="submit" name="Submit"/></p>
</form>
And the php tags are like <?php to open, and ?> to close.
So like:
<?php
if(isset($_POST['Submit'])) {
$FName = $_POST['FName'];
$MI = $_POST['MI'];
$LName = $_POST['LName'];
//....
?>
Your PHP tags are incorrect. Use <?php and ?> to tell PHP to start and stop interpreting the code between them. Also, make sure you're closing the form element (I only see you're opening it) using </form> and that you indeed have all those fields contained inside of it.

Categories