Unable to echo user queries in results - php

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

Related

PHP code is converted as comments in browser and $_REQUEST is not displaying any data?

I have created two simple web pages, one is written in HTML, and the second is written in PHP. In the HTML page, I include form tag with action attribute and its attribute value is index.php(second page) and index.php(second page) include $_REQUEST but no value is displayed.
html page
<html>
<head>
<title>testing of php</title>
</head>
<body>
<h3>here the value will be displayed</h3>
<form method="post" action="index.php">
please enter your name:
<input type="text" name="don"></input><br>
<input type="button" value="send"></input>
</form></body>
</html>
php page
<html>
<head>
<title>testing of php</title>
</head>
<body>
<?php
echo "hello:";
echo $_REQUEST['don'];
?>
</body>
</html>
The type should be submit not button to submit the form using post method :
<input type="submit" value="send"></input>
You need to make another input type.
Working code:
HTML
<html>
<head>
<title>testing of php</title>
</head>
<body>
<h3>here the value will be displayed</h3>
<form method="post" action="index.php">
please enter your name:
<input type="text" name="don"></input><br>
<input type="submit" value="send"></input>
</form></body>
</html>
PHP
<html>
<head>
<title>testing of php</title>
</head>
<body>
<?php
echo "hello:";
echo $_REQUEST['don'];
?>
</body>
</html>
https://www.w3schools.com/html/html_form_input_types.asp
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_submit

Form not sending data via POST

I have this code (it's an include), but the thing is that when I send the form the $_POST data is not being sent.
Im checking for $_POST data like this
if(isset($_POST['ft_upload']) && $_POST['ft_upload'] == 1){
//$usuario -> uploadFirstTime2($db);
echo "ok";
}
and the code for the form is
<div class="ft_userImage" style="background: url(<?php echo $usuario -> getProfileImage(); ?>);"></div>
<p class="ft_step2_continue"><?=$TEXT_BUTTONS['continue'];?></p>
<form action="" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<script>
$("p.ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
check.php
<?php
if(isset($_POST['ft_upload']) && $_POST['ft_upload'] == 1) {
echo "ok";
}
?>
index.html
<!DOCTYPE html>
<html>
<head>
<title>form</title>
</head>
<body>
<form action="check.php" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<button id="ft_step2_continue">SEND</button>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$("#ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
</body>
</html>
it works fine.
i think, you just forgot action="check.php" in your form tag.
Here check this:
<?php var_dump($_POST); ?>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script>
$("p.ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
</body>
</html>
Ok so i did this. Saved it in a php file. And then triggered the submit and it outputs:
array(1) { ["ft_upload"]=> string(1) "1" }
Your php code should be in the same file where the form html is written because your action attribute is empty.
You need to specify the action attribute.
<form action="check.php" method="POST" class="ft_step2_form_upload">
I think you want transfer a file to server??? if yes:
<form method="POST" action="this-file-name.php" enctype="multiform/form-data">
Your Photo:
<input type="file" name="filet" required="required">
<input type="submit" value="Send Photo">
</form>
<?php
if(isset($_FILES['filet'])) {
$dir = './my_dir_name/';
$file_name = $_FILES['filet']['name'];
$doit = #move_uploaded_file($_FILES['filet']['tmp_name'],$dir.$file_name);
if($doit) {
echo 'File '.$file_name.' uploaded';
}
}
?>

I can't transport HTML information to PHP

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>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

I cannot understand why, in the following code, the else clause is never performed. The form is displayed, but when I press the Submit button nothing happens. Can anyone help? Cheers.
<!doctype html>
<html>
<head>
<meta charset = "utf-8">
<title>Listing 12-3</title>
</head>
<body>
<h1>Query the Shop Database</h1>
<h3>Search for a Product</h3>
<p> Use a wildcard if necessary - % in front / behind text</p>
<?php
tryagain:
// Wait for submit
if (!$_POST['submit']) {
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p> Product Name: <input type ="text" name="product" /></p>
<p><input type="submit" name = "submit" value = "Submit" /></p>
</form>
<?php
}
else {
// Connect to the Shop database
Please change
if (!$_POST['submit']) {
to
if (!isset($_POST['submit'])) {

Linking html to php with $_POST

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.

Categories