PHP how do display the inputted name of a user - php

I'm very new to PHP, if you could help me out that would be great :)
what i am trying to get is a way to display the inputted text with a echo en $_POST function. Ive read about it, but cant really get it to work. For more clarity here is an example. The person types his name in the box and click the "go" button. It would then display: Welcome (name of the person). Could this be done in one PHP file that would then redirect to index? What i mean is that a button is clicked in welcome.php you will then be redirected to index.php and so on.
Thanks is andvance

Variable:
<?php
$variable = 'hello world';
echo $variable;
?>
you need 2 php files
if you post something it's pretty much the same
You do need to have a html input element with 'name' attribute
php file that holds the html
<form method="POST" action="your_php_file.php">
<input type="text" name='name' value="some value">
<button type="submit">Go</button>
</form>
php file that holds the php code basically (your_php_file.php) the form from php file 1 post to this one
<?php
$variable = $_POST['name'];
echo $variable;
?>

Related

PHP textbox with input and Set Answer, when submitted right answer go to page, when wrong show error

Create form with input field, set answer to specific string "London". When Submitted if any answer other then "London", have code show error in same page (no alter boxes. if correct answer typed, submit load to another page.
Language using php.
Please help new to php
Create two file, one is trial.php and one is newFile.php(You can give any name);
an follow the code from below
trial.php
<form method="post">
<input name="location" />
<input name='submit' type="submit">
</form>
<?php
if(!empty($_POST['submit'])){
$location=$_POST['location'];
if($location=='London'){
header('location:newFile.php');
}else echo 'Locations is not correct';
}
?>
newFile.php
<?php
echo 'Location is london, that\'s why you are here';

PHP with HTML textarea

I am quite new to PHP/HTML all this kind of stuff so sorry if this is an easy question!
Is there a way to read the contents of an HTML text area and pass it into a .php file?
Or even just reading the contents of the text area into a variable would do.
Thanks
You can achieve that with a basic form:
index.php:
<?php
if ($_POST) // If form was submited...
{
$text = $_POST["mytextarea"]; // Get it into a variable
echo "<h1>$text</h1>"; // Print it!
}
?>
<form method="post">
<textarea name="mytextarea"></textarea>
<input type="submit" value="Go!" />
</form>
And this can be done whatever input element you want e.g: inputs type text, password, checkbox, radio or a select or even and fresh HTML5 input type.
Give your textarea a name and post it to a PHP script with a form. The data from the textarea will be available in the $_POST['textareaName'] variable.
HTML
<form action="page.php" method="post">
<textarea name="myTextarea"></textarea>
<input type="submit" value="go" />
</form>
PHP
<?php
echo $_POST['myTextarea'];
?>
You can Post the form and can access the variable of the form with $_POST['myObj'];
Looking at the $_POST and fetching the value from the post through object name.
Here, "myObj" is an example object name.
you need to submit the form to get the textarea value. Or you can use Jquery to get the textarea content

Sending value from a form to a html tag counter

I am looking for a bit of code to do the following:
A form containing a single text field and a submit button, must send the value of the text field to a landing page that automatically counts how many html tags that this page contains.
E.g. if the text field states stackoverflow.com, the landing page should say (H1 tags = 20) with many more parameters to come.
How is this done? I know how to make a form, but I do not know how to make it send its value to the landing page.
<form action="landingpage.php/" method="post">
The URL
<input type="text" name="cf_name">
<input type="submit" value="Submit">
</form>
This piece of code is a perfect answer to your question.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
Type In Something: <input name="random-info" type="text" size="25">
<input type="submit" value="Submit">
</form> <br>
<?php
echo "You Typed: " . $_GET['random-info'];
?>
you get the method into the url, then you can use them on another page.
To access data from a form it depends on the method. Since your code shows a post message you simply access it in the php on the landing page by user $POST_['cf_name'].
To learn more you can check out:
http://w3schools.com/php/php_post.asp about the post method and http://w3schools.com/php/php_get.asp about the get method.
Also an invaluable source is php manual itself.
As far as counting the tags, not really sure what you are trying to achieve.
If you are counting the tags in the page you create, just make a variable and add to it each time you put that specific tag on the page.
Then you can put those values in a hidden field of the form to be passed into your landing page.

Call a webpage in background in PHP and use jQuery on it

I have an HTML page which has a text box and a button...
<html>
<head></head>
<body>
<form name = "inputdata" method ="get" action ="prc.php">
<input type="text" name="data">
<button id ="checkbox" type="buttton" name="submit" value="submit">Submit!</button>
</form>
</body>
</html>
The code for prc.php is...
<?php
$data=$_GET['data'];
echo "<html><head></head><body><div id=\"maindata\"> ";
echo $data;
echo "</div></body></html>";
?>
I have a PHP file which gets the contents of the previous page...
<?php
file_get_contents("index.html");
?>
However, I would like to add functionality to the above PHP script such that it gets the page contents in the background and uses some jQuery on it:
$('#inputdata').val('hello boss');
$('#checkButton').click();
This will enable me to fill out the text box in the background and submit the button. Then I need to get the result from prc.php using jQuery...
$('#maindata').html();
Does anybody know how to do this?
It sounds like you want to submit the form on the page. This should be possible by simply requesting the resource in the action field. In your example, requesting the url prc.php with a parameter called data which contains the data input data you wish to submit.
prc.php?data=mydata

HTML PHP Form Processing; Variable

I'm a newbie learning and trying to understand how html forms and php processing works.
Came across this pair of examples:
HTML FORM:
<html>
<body>
<form action="hello-web.php" method="GET">
<label>Name:</label>
<input type="text" name="yourName" size="24">
<input type="submit">
</form>
</body>
</html>
PHP PROCESSOR:
<?php
$fname = $_GET["yourName"];
echo "Hello $fname!";
?>
OUTPUT SHOULD BE:
Hello Entered/Example Name!
QUESTION:
When I try to change the variable "yourName" (on BOTH HTML and PHP files) to, for example "typeName" , the entered name on the form does not show up.
In other words, the output becomes just: Hello !
Is "yourName" a standard php or html variable? Can it not be changed to what ever you want it to be?
Better yet, how exactly does the form process data?
Here is my altered code that won't output the entered name (I posted here as an answer because all the codes shows up as a continuous line, like a paragraph, when I paste as a comment to your answer:
HTML FORM(altered--typeName):
<html>
<body>
<form action="hello-web.php" method="GET">
<label>Name:</label>
<input type="text" name="typeName" size="24">
<input type="submit">
</form>
</body>
</html>
PHP PRCESSOR (altered--typeName):
<html>
<body>
<?php
$fname = $_GET["typeName"];
echo "Hello $fname!";
?>
</body>
</html>
You can see what data is available to you from the submitted form by outputting the entire array. Since your form method is GET, the following will show you all that was submitted:
var_dump( $_GET );
From this, you can see what the variable names should be in your PHP script.
Array
(
[YourName] => Jonathan
)
Anytime you come across a disconnect between what is being submitted, and what you're expecting, check $_GET (or if your method is POST, you would check $_POST).
For instance, if I were trying the following:
echo $_GET["yourName"]; // nothing output to the screen
I could refer to the array contents printed above and see that the correct key is "YourName":
echo $_GET["YourName"]; // Jonathan
$_GET["yourName"]; Contains a value based off of the input of the form field. It's a php superglobal http://us3.php.net/manual/en/reserved.variables.get.php
It sounds like you're changing the html form, but your not entering a value through the form. Therefore, $_GET["yourName"]; is empty

Categories