My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>
Related
im trying to do simple save of form input with php.
im doing this on 000wehost.com
here is the output that goes wrong
https://hikola.000webhostapp.com/
and here is my code in it.
index.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="myprocessingscript.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
</body>
</html>
and another myprocessingscript.php file
<?php
$var1 = $_POST['field1'];
$var1 = $_POST['field2'];
$handle = fopen("dataSave",'w');
fwrite($handle,$var1);
fwrite($handle,$var2);
fclose($handle);
}
?>
can anyone tell me why wont it save a file with input values?
(im trying to do this for 3 hours already... thx..)
I am currently creating an HTML form that has 2 fields; name and an address. It also has a way of selecting one of 2 options. The form will either be used to look up the address of a person. In this case, only the name field is required to be filled out. The other option is to add a person to the file. In this case both fields need to be filled out. For some reason, I am not able to get the values that inputted from the form into my PHP file. Please help.
Here is my HTML Form
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="action_page.php" method="post">
<div>
<label for="name">Name: </label>
<input id="name" type="text" name="name"><br>
</div>
<div>
<label for=address">Address: </label>
<input id="address" type="text" name="address"><br>
<input type="radio" name="action" value="lookup">Lookup<br>
<input type="radio" name="action" value="add">Add<br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Here is my PHP file
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name = $_POST['name'];
echo "<p>",$_POST["name"],"</p>";
echo "<p>",$_POST["action"],"</p>";
echo "<p>",$_POST["address"],"</p>";
$address = array();
if($_POST["action"]=="lookup"){
$fh = fopen("address.txt","r") or die("No such file found.");
while(!feof($fh)) {
$line = fgets($fh);
$info = explode("|",$line);
$address[$info[0]]=$info[1];
}
if(array_key_exists($_POST["name"],$address)) {
echo "<p>",$_POST["name"],"<p>";
echo "<p>",$address[$_POST["name"]],"</p>";
}
?>
<body>
</html>
The error was in
echo "<p>",$_POST["name"],"</p>";
It should be
echo "<p>".$_POST["name"]."</p>";
and same for others
I am learning PHP and in the process of making a search engine. The following code will not echo the users input, in the search field, on the results page.
The search page:
<!DOCTYPE html>
<html>
<head>
<title>Search Engine</title>
<style type="text/css">
body {
background:#F0FFFF;
margin:-80px;
}
form {
margin:25%;
}
</style>
</head>
<body>
<form action="result.php" method="post">
<input type="text" name="user_query" size="80" placeholder="Enter Search Here" />
<input type="submit" name="search" value="Search Now" />
</body>
</html>
The results page:
<!DOCTYPE html>
<html>
<head>
<title>Results</title>
<style type="text/css">
.results {
margin:5%;
}
</style>
</head>
<body bgcolor="#F0FFFF">
<form action="result.php" method="get">
<span><b>Enter Query Here:</b></span>
<input type="text" name="user_keyword" size="80" />
<input type="submit" name="result" value="Search Now" />
</form>
<?php
if(isset($_GET['search'])) {
$get_value = $_GET['user_query'];
echo "<div class='results'>
$get_value;
</div>";
}
?>
</body>
</html>
Can someone tell me why the user's input is not being echoed when the search is run?
If you using method=post
In form tag you must receive you data as $_POST
Also method=get use $_GET
Edit this lines in result page to this
<?php
if(isset($_POST['search'])){
$get_value = $_POST['user_query'];
echo "<div class='results'>{$get_value}</div>";
}
?>
well the problem lies in your form tag you should have used method="get" if you want to get some data from database and if you are using method="post" you should have to use $_POST['search'] so your form tag shuld have to be like this
<form action="result.php" method="get">
and if you are going to use method="post" anyway than your $_GET['search'] should have to be $_POST['search']
but i will suggest you to use method="get" for searching query from database
I have been trying to create a form that reads a post from an HTML form and displays an element from that post IF it detects that the post exists.
However, each time the post is submitted, it simply reloads the form as though no post were provided.
<!DOCTYPE html>
<html>
<head>
<title>Upload from Manifest</title>
</head>
<body>
<?php
if (isset($_POST['manifest'])) {
echo 'we are in the IF';
echo($_POST['manifest']);
}
?>
<h1>Submission from manifest into main db</h1>
<div class="container offset-top120">
<form method="post" action="https://nhsggc.cogiva.com/prism/loadFromManifest.php" enctype="multipart/form-data">
<input id="manifest" type="text" />
<input id="submit" value="Submit" type = "submit" />
</form>
</div>
</body>
</html>
Your form is going to either a different page (https://nhsggc.cogiva.com/prism/loadFromManifest.php so check for that first) if you wanted it to go to same page, you can give the action as just '#', or put in the whole URL like you have.
You're missing the name attribute from your submit input and text input. Read up on the name attribute!
<input id="manifest" type="text" name="manifest">
<input id="submit" value="Submit" type="submit" name='submit' />
Then your PHP should look like this:
<?php
if (isset($_POST['submit'])) {
echo 'Inside an if';
echo $_POST['manifest'];
}
Then it should work.
Okay so I'm confused and lost and have no idea of what I am doing, basically here is my code:
HTML/JavaScript:
<img src="Images/Question1.png" usemap="#mainMap" id="main"/>
<map name="mainMap" id="mainMap">
<area shape="rect" coords="82,192,196,242" onclick="incorrectAnswer()" />
</map
function incorrectAnswer() {
document.getElementById("incorrectVid").style.display="block";
document.getElementById("incorrectVid").play();
document.getElementById("answer_id").value=id;
document.forms["answer_send"].submit();
}
<form id="answer_send" name="answer_send" action="answersend.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/>
<input type="submit" value="Submit" />
</form>
PHP:
$id=$_POST['answer_id'];
echo $id
I have no idea why this is not working any ideas/help?
To grab values submitted via GET/POST you need to assign the name property to your input fields. Try the below:
The HTML and JS
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
function incorrectAnswer() {
document.forms["answer_send"].submit();
}
</script>
</head>
<body>
<img src="none.jpg" alt="placeholder" width="200" height="200" onClick="incorrectAnswer();">
<form id="answer_send" name="answer_send" action="form.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/>
</form>
</body>
</html>
The PHP
<?php
$id=$_POST['answer_id'];
echo $id;
?>
Note: Your AREA element is not going to work unless you assign it to something.
Your JavaScript function must be within tags or you'll receive errors.
The HTML/PHP part of this is working fine... You are experiencing these issues due to the following three lines...
document.getElementById("incorrectVid").style.display="block";
document.getElementById("incorrectVid").play();
document.getElementById("answer_id").value=id;
where is your name attribute ?
Every input must have a name attribute to retrieve it on php
and your html code must look like this:
<form id="answer_send" name="answer_send" action="answersend.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/> </form>