Php simple input to array [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 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);
}
?>

Related

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.

How does checking for "submit button is pressed" work? [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'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...

Show form validation errors on the same page at the right side of the input box [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'm new to PHP and working on a project.
I want to validate a login and register page using PHP.
I want to validate the form in the same field and display the error on right side of the input box.
I tried many times but I am not successful. I can do it with JavaScript/jQuery, but I am unable to do it with PHP.
My form contains:
email : inputbox - error div
password : input box -error div
submit button
Can you explain how to do this with PHP?
You can let the form post to itself and verify the post data on submit. Something like:
<?php
$errors = array();
if (isset($_POST['submit'])) {
// do some validation and fill $errors variable when something is wrong
// if everything is fine e.g. redirect user
header('Location: http://example.com/success');
}
?>
<form action="" method="post">
<input type="text" name="email">
<?php if ($errors['email']) { echo '<div class="error">' . $errors['email'] . '</div>'; } ?>
<input type="password" name="password">
<?php if ($errors['password']) { echo '<div class="error">' . $errors['password'] . '</div>'; } ?>
<input type="submit" name="Submit">
</form>
You can'tdo it with php... php is ran at the server, the only possible way to do it with php is also using jquery or js (and ajax)...

Categories