I'm trying to link a simple html file with a php file.The data from the html file is properly getting transferred to php file , but while displaying that data through php file nothing is gets displayed on the browser.
html:
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">Calculator</font></b></i></header>
<form action="result.php" method="POST" >
<br>
<br>
Number 1: <input type="text" name="number1">
<br><br>
Number 2: <input type="text" name="number2">
<br>
<br>
<input type="submit">
</form>
</body>
</html>
php:
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">RESULT</font></b></i></header>
<?
$var1= $_POST['number1'];
$var2= $_POST['number2'];
echo $var1;
echo $var2;
?>
</body>
</html>
<form action="result.php" method="GET" >
you are using GET and receiving as POST
$var1= $_POST['number1'];
$var2= $_POST['number2'];
you change either one of them, but my advice would be to change your form action to:
<form action="result.php" method="POST" >
You're are using method as GET in your source file but getting as $_POST in the destination file.
You should use method="POST" from your source file.
So, It should have something like
<form action="result.php" method="POST" >
Learn more about Sending data from Form Here
How do you know that the data is beign transfered? Try this as it seems that you are using GET instead of post, then use the proper one:
<?
echo 'POST: ';
var_dump($_POST);
echo 'GET: ';
var_dump($_GET);
?>
As of this:
<form action="result.php" method="GET" >
you should either do:
$var1= $_GET['number1'];
$var2= $_GET['number2'];
or change
<form action="result.php" method="POST" >
(which is anyway recommended ...)
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">RESULT</font></b></i></header>
<?php
$var1= $_GET['number1'];
$var2= $_GET['number2'];
echo $var1;
echo $var2;
?>
</body>
</html>
Related
I am trying to find a way for multiple pages to access the data provided from a single HTML form submission.
Currently, I have 4 pages...
index.php (containing my HTML form)
functions.php (where the HTML
form sends data to)
results1.php
results2.php
Can anyone please let me know where I'm going wrong or point me in the right direction?
index.php
<!DOCTYPE html>
<head>
</head>
<body>
<form action="functions.php" method="post">
<input type="text" name="value1">
<input type="text" name="value2">
<input type="submit">
</body>
</html>
functions.php
<?php
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
?>
results1.php & results2.php
<!DOCTYPE html>
<head>
<?php include_once("functions.php") ?>
</head>
<body>
<?php echo $value1, $value2; ?>
</body>
</html>
You can do it using PHP Session.
Try with the given code below.
index.php
<!DOCTYPE html>
<head>
</head>
<body>
<form action="functions.php" method="post">
Value 1: <input type="text" name="value1"><br>
Value 2 :<input type="text" name="value2"><br>
<input type="submit">
</body>
</html>
functions.php
<?php
session_start();
$_SESSION["value1"] = $_POST["value1"];
$_SESSION["value2"] = $_POST["value2"];
?>
results1.php & results2.php
<!DOCTYPE html>
<head>
<?php session_start(); ?>
</head>
<body>
<?php echo 'Value 1:'. $_SESSION["value1"]; ?><br>
<?php echo 'Value 2:'. $_SESSION["value2"]; ?>
</body>
</html>
Say I have a page with a textarea which acts as an input.
Then I have a Submit button and right under everything i have the
output textarea.
Now what I want to do is when the input has been submitted and
sent into the output text area, how can I then retrieve the text from the output area.
This is the code i have:
<head>
<?php error_reporting(0);
$OutputText = $_GET['OutputText'];
?>
</head>
<body>
<form action="#" method="_GET">
<textarea name="InputText">
hi
</textarea>
<input type="submit" name="submitFirstInput">
</form>
<textarea name="OutputText">
<?php echo $_GET['InputText']; ?>
</textarea>
<hr>
<p>Output String Length: <?php echo strlen($OutputText); ?> </p>
</body>
for reasons I dont understand, it cant define the $OutputText,
Do they both have to be in a form? As i have understood form's is only to send data, and testing it didn't help much either.
Keep in mind this is just a barebones version of the original, essentially i have some Input text and then through some logic it gets modified, therefor i want some statistics for the output result. So just getting the first input isnt rather useful..
adding some javascript you can sync the two textarea:
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(window).load(function(){
$("#one, #two").on("change keyup", function(){
$("textarea").not($(this)).val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="GET">
<textarea name="InputText" id="one"></textarea>
<textarea name="OutputText" id="two"></textarea>
<input type="submit" name="submitFirstInput">
</form>
<hr>
<?php echo '<pre>'; var_dump($_GET); echo '</pre>'; ?>
<p>Output String Length:
<?php echo strlen($_GET['OutputText']); ?> </p>
</body>
</html>
Textarea must be inside the form tag, and the method must be GET (or POST)
try this:
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<form action="#" method="GET">
<textarea name="InputText">hi</textarea>
<input type="submit" name="submitFirstInput">
<textarea name="OutputText"><?php echo $_GET['InputText']; ?></textarea>
</form>
<hr>
<?php //echo '<pre>'; var_dump($_GET); echo '</pre>'; ?>
<p>Output String Length: <?php echo strlen($_GET['OutputText']); ?> </p>
</body>
</html>
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
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 a file test.php with the following code:
<html>
<head>
<title>Listing 10.2 </title>
</head>
<body>
<div>
<form method="post" action="test.php" >
<p> <input type="text" name="guess"/> </p>
</form>
<?php
if(!empty($_POST['guess'] )) {
print "Last guess $_POST['guess']";
}
?>
</div>
</body>
</html>
I have a problem with the form not showing up. However, if I remove the PHP portion of the code, it is visible. What is my problem?
You can't embed the $_POST variable into the string like that, trying changing the php section to:
<?php
if(!empty($_POST['guess'] )) {
print "Last guess {$_POST['guess']}";
}
?>
Use . for combine values and strings
if(!empty($_POST['guess'] ))
{
echo "Last guess".$_POST['guess'];
}