I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"
Related
I have the following code snippet of my fields I have in my form:
<input id="username" type="text" placeholder="E-mail Address" value="" name="username"></input>
This is what I have in my input field. Is there anybody who will tell me how to get input values to the field using a url? e.g https://mysite?username=ken and it will show "ken" in the input field?
In your HTML, add the input field like this:
<input type="text" name="username" value="<?php echo htmlspecialchars($_GET['username']); ?>" />
Basically, the value attribute of the text field needs to be set to:
<?php echo $_GET['username']; ?>
The code right above this is how you would output a get variable in php whether you are putting it in a text field or not.
To access get variables, always use:
$_GET['variable_name'];
Then you can assign it to variables or pass it as a function parameter.
**However, I strongly do not recommend passing sensitive information like usernames and passwords through GET variables. **
First off, users could change the URL hence changing the variable. They could also accidentally share the URL with someone and that could give someone else access to their account. I would recommend that you create a cookie on their machine that is set to a random ID, and then in a MySQL database, associate that ID with a username so that you know the user can't accidentally share their account or change their username through the URL.
You can do it like this, make an isset in your php form input that can catch your ken variable from GET post, never forget the method="get" inside the form tag and if you are planning on submitting on the same page you can use action="<?php echo $_SERVER['PHP_SELF']; ?>" inside your form tag.. hope this helps, here is your code.. ^_^
<form id="form" name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<p>Input</p>
<div>
<input type="text" name="nameoffield" id="nameoffield" value="<?php if(isset($_GET['ken'])){echo $_GET['ken'];} ?>"> <br />
</div>
</fieldset>
<div>
<button type="submit" value="Submit" name="submit">
Submit
</button>
</div>
</form>
The <input> tag and other fields of form must be in a <form>tag.
<form action = "https://mysite" method = "get">
<input id = "username" type = "text" placeholder = "E-mail Address" name = "username" value = "<?php echo $_GET['username']; ?>" />
</form>
In the above code, form tag specifies that the method of submission is 'GET' and the action that will be taken on submission is URL to which your form data will be submitted and processed.
Now assuming that your form is in the same URL to which you are submitting your form, you will get the GET value in the same page (or URL), so in the input text field set the value which is obtained by GET method and use it.
All the GET key-value pairs are stored in an associative array $_GET from which you can access the value of a given key by using that as the index of the array.
e.g. Key is username in this case, so to get the value of the username, $_GET['username'] was used.
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.
I have a simple form which was working and now im finding the post data isnt being sent and i can't see the problem
<form role="form" name="challengeform" action="scripts/arena_setup.php" method="POST" onsubmit="return confirm('Are you sure you want to attack this player?');">
<input type="hidden" name="member_id" value="<? echo $member_id;?>">
<input type="image" src="img/map/attack.png" alt="Attack" />
</form>
which is being handled by
if(isset($_POST['challengeform'])){
...
}else{ echo 'error'; }
it always shows the error due to the post data being missing but i just cant see what i've done. Any ideas?
if(isset($_POST['challengeform']))
Form names are not part of the POST data. Only fields within the form.
Try testing for the field itself
if(isset($_POST['member_id']))
You shouldn't write the name of the form. Just write input's name to get the data. Ex:
$var = $_POST['member_id'];
My website involves a user submitting data over several pages of forms. I can pass data submitted on one page straight to the next page, but how do I go about sending it to pages after that? Here's a very simplified version of what I'm doing.
Page 1:
<?php
echo "<form action='page2.php' method='post'>
Please enter your name: <input type='text' name='Name'/>
<input type='submit' value='Submit'/></form>";
?>
Page 2:
<?php
$name=$_POST["Name"];
echo "Hello $name!<br/>
<form action='page3.php' method='post'>
Please enter your request: <input type='text' name='Req'/>
<input type='submit' value='Submit'/></form>";
?>
Page 3:
<?php
echo "Thank you for your request, $name!";
?>
The final page is supposed to display the user's name, but obviously it won't work because I haven't passed that variable to the page. I can't have all data submitted on the same page for complicated reasons so I need to have everything split up. So how can I get this variable and others to carry over?
Use sessions:
session_start(); on every page
$_SESSION['name'] = $_POST['name'];
then on page3 you can echo $_SESSION['name']
You could store the data in a cookie on the user's client, which is abstracted into the concept of a session. See PHP session management.
if you don't want cookies or sessions:
use a hidden input field in second page and initialize the variable by posting it like:
page2----
$name=$_POST['name']; /// from page one
<form method="post" action="page3.php">
<input type="text" name="req">
<input type="hidden" name="holdname" value="<? echo "$name"?>">
////////you can start by making the field visible and see if it holds the value
</form>
page3----
$name=$_POST['holdname']; ////post the form in page 2
$req=$_POST['req']; ///// and the other field
echo "$name, Your name was successfully passed through 3 pages";
As mentioned by others, saving the data in SESSION is probably your best bet.
Alternatly you could add the data to a hidden field, to post it along:
page2:
<input type="hidden" name="username" value="<?php echo $name;?>"/>
page3
echo "hello $_POST['username'};
You can create sessions, and use posts. You could also use $_GET to get variables from the URL.
Remember, if you aren't using prepared statements, make sure you escape all user input...
Use SESSION variable or hidden input field
This is my workaround of this problem: instead of manually typing in hidden input fields, I just go foreach over $_POST:
foreach ($_POST as $key => $value) {
echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}
Hope this helps those with lots of fields in $_POST :)
The code is working, however I still recieve undefined index with the Id "addmsg"
<?php
$addmsg=$_GET["addmsg"];
if (isset($addmsg)) // If the user wants to add a Message
{
?>
This is the code for the textarea and the submit buttons:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p>Post your Message:<br />
<textarea name="msg" rows="10" cols="70" wrap></textarea><br />
<input type="submit" name="smtmsg" value="SUBMIT" /></p>
</form>
Lastly, this code is to connect to the Mysql database
$url = $_SERVER['PHP_SELF'] ."?addmsg=1";
You need to check isset before assigning the variable:
<?php
if (isset($_GET["addmsg"])) // If the user wants to add a Message
{
$addmsg=$_GET["addmsg"];
}
?>
You use $_POST so the field msg would never be evaluated here. Either change the $_GET['addmsg'] to $_POST['msg'] or change the field msg to addmsg and change the form type to "get"
There is no field named "addmsg" in your HTML. The only one you have is named "msg".
Your form is submitting to <?php echo $_SERVER['PHP_SELF'];?>, so on your server side, there is no index addmsg in the $_GET superglobal.
You should change your form action to <?php echo $_SERVER['PHP_SELF'] . "?addmsg=1";?> as you have in the last line of your question.
The message mean exactly what it says. The is nothing set at $_GET['addmsg'].
In this case, you have two problems. Your form method is set to post. You must therefore look for your value in $_POST
Second, you must actually have a field named addmsg in your form. The field in your form is named msg. So the value would be available as $_POST['msg']
There is 2 errors:
First:
IF the method is "post", the variable is $_POST.
Second:
The textarea's name is "msg", then you have to do $_POST["msg"].
Of course, if methos="get", the variable is $_GET["msg"]