Ok my first time asking question here. This as been very helpful in the past but now i'm lost.
I'm trying to understand how php work with the help of a book. So i did the exercise as it was shown in the book and the result if not what it should be.
Here's the code:
<div id="content">
<p>Here's a record of everything in the REQUEST array:</p>
<?php
foreach($_REQUEST as $value) {
echo "<p>" . $value . "</p>";
}
?>
</div>
<div id="footer"></div>
</body>
And here's the result:
Here's a record of everything in the REQUEST array:
" . $value . "
"; } ?>
Why is not showing the info it is suppose to? Thanks.
Ok here's all the code:
showRequestInfo.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html
> xmlns="http://www.w3.org/1999/xhtml"> <head> <meta
> http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link
> href="/wamp/www/livrephp/css/phpMM.css" type="text/css"
> rel="stylesheet" />
>
> <title>Untitled Document</title> </head>
>
> <body> <div id="header"><h1>PHP & MySQL: The Missing
> manual</h1></div>
> <div id="example">Example 3-2</div>
>
> <div id="content">
> <p>Here's a record of everything in the REQUEST array:</p>
> <?php foreach($_REQUEST as $value) { echo "<p>" . $value . "</p>"; } ?>
>
>
> </div>
> <div id="footer"></div> </body> </html>
And this goes with this file called "socialEntryForm.php"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/wamp/www/livrephp/css/phpMM.css" rel="stylesheet" type="text/css" />
<title>Untitled Document</title>
</head>
<body>
<div id="header"><h1>PHP & MySQL: The misiing manual</h1></div>
<div id="example">Example -1</div>
<div id="content">
<h1>Join the missing manual (Digital) Social Club</h1>
<p>Please enter your online connections below:</p>
<form action="../showRequestInfo.php" method="post">
<fieldset>
<label for="first_name">First Name:</label>
<input type="text" name="first_name" size="20" /><br />
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" size="20" /><br />
<label for="email">Email Address:</label>
<input type="text" name="email" size="50" /><br />
<label for="facebook_url">Facebook URL:</label>
<input type="text" name="facebook_url" size="50" /><br />
<label for="twitter_handle">Twitter Handle:</label>
<input type="text" name="twitter_handle" size="50" /><br />
</fieldset>
<br />
<fieldset class="center">
<input type="submit" value="Join The Club" />
<input type="reset" value="Clear and Restart" />
</fieldset>
</form>
</div>
<div id="footer"></div>
</body>
</html>
Are you sure your file is a PHP file and it's being run on a PHP enabled server? The browser seems to be receiving the code unparsed, thinking that there's a tag starting at <?php and ending at the first <p> tag. If you look at the source, you'll probably see your PHP code, untouched by the server.
In other words: Your code is correct and the problem is your file type or server configuration. If you are indeed using a server on your machine, make sure you're running the file right, e.g. if it's in the root, open http://localhost/your_file.php, and not C:\xampp\htdocs\your_file.php.
EDIT: Just for the heck of it, I replicated your issue with a fiddle. I got the exact same output as you, meaning it's not getting parsed by the server. Who said JSFiddle was only good for JavaScript?
$_REQUEST Contains data which is gathered from cookies, $_POST and $_GET .. Are you sure that your data is properly assigned?
Take this example.
print_r($_REQUEST); just doing that without no <form method="get/post"> will produce a blank array, that might be why you are getting nothing
Your snippet is correct.. you are lacking a html form to go with that..
Example:
<form method="POST">
<input type="submit" name="test" value="ThisIsCorrect">
</form>
With your code you have shown your question.. your $_REQUEST array will return the value of the button. In this case "ThisIsCorrect"
Moral Of this?
Ensure that you are using using post/get/cookies before calling the $_REQUEST, and for future reference, just using $_POST & $_GET is cleaner to use.. But that is down to my personal preference.
How is the $_REQUEST Array constructed?
Consider this:
the array will contain the name as the array key and the value as the value..
So taking this into account:
<form method="POST">
<input type="text" name="username">
<input type="submit" name="test" value="ThisIsCorrect">
</form>
For your text box & Submit button the array will be:
array ("username" => "UserInputData",
"test" => "ThisIsCorrect");
Related
im trying to make a madlibs kind of game, right now i got everything working besides 1 thing. the output should overwrite the questions instead of showing above the questions. how can i manage to do this?
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Mad Libs</title>
<link rel="stylesheet" type="text/css" href="MadLibs.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:wght#700&display=swap" rel="stylesheet">
</head>
<body>
<form action="" method="POST">
<h1> MadLibs</h1>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$Input1 = $_POST["Input1"];
$Input2 = $_POST["Input2"];
$Input3 = $_POST["Input3"];
$Input4 = $_POST["Input4"];
echo "here is a test $Input1. here is an other test $Input2 and again an other test
$Input4 test $Input3.";
}else{
<p id="Text">test question <input type="text" required name="Input1" placeholder="Enter your answer.."></p>
<p id="Text">test question <input type="text" required name="Input2" placeholder="Enter your answer.."></p>
<p id="Text">test question<input type="text" required name="Input3" placeholder="Enter your answer.."></p>
<p id="Text">test question <input type="text" required name="Input4" placeholder="Enter your answer.."></p>
<input id="Submit" type="submit" name="submit" value="Send">
</form>
?>
<footer>
<p> A #2021 website</p>
</footer>
</body>
</html>
The end result shows like this but the questions and the input field should be gone when the submit button is pressed
I need the HTML File below to connect to different PHP Files as these connect to different tables in a database. I have used JAVA Script to submit an action when the button is pressed but no matter what, the file will only connect to the Prepared.php file and never connect to the Prepared2.php file. Ihave tried labeling differently and many other concepts but cannot seem to get the file to work.
Please find code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> BBC Database search </title>
</head>
<body>
<center>
<h1> BBC Database search </h1>
<script type="text/javascript">
function submitAction(act) {
document.sample.action = act;
document.sample.submit();
}
</script>
<form action="Prepared.php" method="post">
Search Brands: <input type="text" name="term"
onClick="submitAction('Prepared.php')"/><br />
<input type="submit" value="Submit" />
<form action="Prepared2.php" method="post">
Search Available Clips: <input type="text" name="clips"
onClick="submitAction('Prepared2.php')" /><br />
<input type="submit" value="Submit" />
</center>
</form>
The js code is totally unnecessary, and wrong, remove it and the onsubmit.
Then close the first <form> tag with a </form> Currently you have the second form within the first and thats not legal
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> BBC Database search </title>
</head>
<body>
<center>
<h1> BBC Database search </h1>
<form action="Prepared.php" method="post">
Search Brands: <input type="text" name="term" />
<br />
<input type="submit" value="Submit" />
</form>
<form action="Prepared2.php" method="post">
Search Available Clips: <input type="text" name="clips" />
<br />
<input type="submit" value="Submit" />
</form>
I am trying to catch some text from the textbox, to save it in a variable and then post it on my browser if I click the submit button. My code is this:
<?php
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</body>
</html>
I don't know what is wrong. The code on rextester
For starters you need to implement a form on you HTML page and set the action to a new .php page.
<form action='display.php' method='post'>
*your input fields go here*
Now create a new php page (in this case display.php)
Declare the variables as you did....
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
then you can simply echo each variable accordingly...
Final Result
HTML PAGE:
<form action='result.php' method='post'>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
result.php
<?php
$var1 = $_POST['text1']; // create variables of your form elements
$var2 = $_POST['display'];
echo '$var1 . ' ' . $display . '.';
?>
Basically the php page is saying get text1 and display from the form (names) and create variables of them...
Then echo (print) these variables on the screen. (in plain english xD)
p.s
Your rextester isn't working because you haven't specified a form.
You need to setup the FORM tags so they have the correct attributes for POST. Try:
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
</body>
</html>
You need to use a form, otherwise you aren't posting anything.
Something like:
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form method="post" action="yourphpfile.php">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="text" value="display" name="display" id="display" /><br /><br />
<input type="submit" value="submit"><br /><br />
</form>
</body>
</html>
yourphpfile.php
<?php
if(!empty($_POST['text1']) and !empty($_POST['display'])){
$var1=$_POST['text1'];
$var2=$_POST['display'];
echo "$var1 $var2";
}
?>
Check this complete example
To display text box and button on clicking submit button
<form>
<input type="submit" value="OK" name="btn_name"/><br />
<?php
if(isset($_GET['btn_name']))
{
echo "<input type=text name=txt_userinput />";
echo "<input type=submit value=Area name='btn_calculate'/>";
}
?>
</form>
I have a very simple HTML form that is supposed to send information to the file written in action attribute via GET but somehow it's transfering the information back to index.php:
index.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Forms Sandbox</h1>
<form acton="process_form.php" method="get">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" />
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
</body>
</html>
process_form.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Response Sandbox</h1>
<?php
$username = $_GET["username"];
$email = $_GET["email"];
echo $username . " : " . $email . "<br />";
?>
</body>
</html>
The bizarre aspect is that when I submit the form, the URL shows that it is not even using process_form.php:
http://127.0.0.1/Learning/?username=test&email=x%40test.com&submit_btn=Submit
If I manually change the URL to include process_form.php it seems to be working fine and I get the results I am looking for:
http://127.0.0.1/Learning/process_form.php?username=test&email=x%40test.com&submit_btn=Submit
On my development computer, I'm running EasyPHP 14.1 local WAMP server and thought it might be the root of the problem so I uploaded the files to my website that is running newest PHP on Apache, but the problem still exists there.
What am I doing wrong?
you have a typo error in action; you have given acton. Should be like this:
<form action="process_form.php" method="get">
First thing - you have a typo:
<form action="process_form.php" method="get">
^
The second thing - in my opinion the best method of handling forms is using POST method, not GET, so I would change it to:
<form action="process_form.php" method="post">
and in process_form.php I would use $_POST instead of $_GET
After digging around your question,
Index.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Forms Sandbox</h1>
<form action="process_form.php" method="get">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" />
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
</body>
</html>
process_form.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Response Sandbox</h1>
<?php
$username = $_GET["username"];
$email = $_GET["email"];
echo $username . " : " . $email . "<br />";
?>
</body>
</html>
Note: If you will not specify form method, By default it will take GET method. So please make sure action should be perfect.
Above code just copy and paste, it should work perfect.
Ask me for further clarification.
Thanks,
Gauttam
I've started learning PHP. Managed to setup things.
I'm using php version 5.3.13.
I'm trying to post some info to a html form and receive it in a php file.
For the purpose i'm using $_Post variable and the ouput at the php file is blank.
Below is the html code.
<body>
<form action="report.php" method="POST" >
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" /><br />
<input type="submit" value="Report Abduction" name="submit" />
</form>
</body>
And below is the report.php code
<html>
<head>
<title></title>
</head>
<body>
<?php
$name = $_POST['firstname'] ;
print($name);
?>
</body>
</html>
Can any one advise what i'm missing ?
Thanks
Here is a super simple example, I suggest you begin to look for example tutorials # your favorite search engine, or buy a book.
Edit: Do you even have PHP installed? you mention inetpub which is a IIS path.
<?php
error_reporting(E_ALL);
if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['firstname'])){
//Do something with posted data
$out = htmlentities($_POST['firstname']).' has been abducted!';
}else{
//Form has not been posted so show form
$out = <<<FORM
<form action="" method="POST" >
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" /><br />
<input type="submit" value="Report Abduction" name="submit" />
</form>
FORM;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My first test Script</title>
</head>
<body>
<h1>My first test Script</h1>
<?php echo(isset($out))?$out:null; ?>
</body>
</html>