I am sure this is very simple to many of you!! I am trying to learn basic HTML / PHP form processing. The site is hosted on a local Apache server. I have had PHP working correctly and am happy that the server is working fine.
I have two files, one is settings.html, where the user has a form of 3 elements which they can enter some float values into (humidity, temperature and light tolerances). The submit button triggers a separate file called process.php which should display the three values. The code is as follows:
settings.html:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<meta http-equiv="refresh" content="179" >
<title>Sparks - Settings</title>
<link rel="stylesheet" type="text/css" href="css/default.css">
</head>
<body>
<div id="logo">
<img style="width: 335px; height: 142px;" alt="ESP8266 Logo" src="images\imgESP8266.png">
</div>
<br>
<form method="post" action="php/process.php">
Humidity Tolerance : <input type="float" name="humTolerance" placeholder="Enter %" /><br />
Temperature Tolerance : <input type="float" name="tempTolerance" placeholder="Enter %" /><br />
Light Tolerance : <input type="float" name="lightTolerance" placeholder="Enter %" /><br />
<input type="submit" value="Submit" />
</form>
</body>
<html>
php/process.php:
<?php //process.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$humTolerance = $_POST["humTolerance"];
$tempTolerance = $_POST["tempTolerance"];
$lightTolerance = $_POST["lightTolerance"];
}
echo <<<_END
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<meta http-equiv="refresh" content="179" >
<title>Sparks - Settings</title>
<link rel="stylesheet" type="text/css" href="css/default.css">
</head>
<body>
humTolerance is: $humTolerance<br>
tempTolerance is: $tempTolerance<br>
lightTolerance is: $lightTolerance<br>
</body>
</html>
_END;
?>
This is producing the HTTP ERROR 500 and I can't see why. Please can I have some help? Thanks.
As I said in comments:
"the (first) problem is in your heredoc closing identifier it contains spaces before it."
and
"the second one: type="float" isn't a valid type for an input"
You need to replace those input types with ones that are valid.
https://developer.mozilla.org/en/docs/Web/HTML/Element/input
Note that not all browsers support certain HTML5 attributes, see the above as a reference.
So use type="number" or type="text".
PHP with spaces removed before the closing heredoc identifier.
<?php //process.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$humTolerance = $_POST["humTolerance"];
$tempTolerance = $_POST["tempTolerance"];
$lightTolerance = $_POST["lightTolerance"];
}
echo <<<_END
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<meta http-equiv="refresh" content="179" >
<title>Sparks - Settings</title>
<link rel="stylesheet" type="text/css" href="css/default.css">
</head>
<body>
humTolerance is: $humTolerance<br>
tempTolerance is: $tempTolerance<br>
lightTolerance is: $lightTolerance<br>
</body>
</html>
_END;
?>
Use PHP's error reporting also:
http://php.net/manual/en/function.error-reporting.php
If you don't have access to logs, then set that to catch and display.
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I recently start the php programming. However, I encountered a problem about running a function on php file. My php file is listed as follows:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="Test()" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
function Test()
{
echo "Goodbye";
}
?>
</body>
</html>
After running the program, a webpage is observed with A button entitled submit and the word "Hello". However, After click on the button, the webpage "This page cannot be displayed" is observed. But I respect the word "Goodbye" is shown. I transferred the php code to another file, but the problem was not resolved.
actions in forms aren't functions like JavaScript. You are trying to run Test() on submit which isn't a thing. What you want is action to be action=" "
In reality you should be doing something like this:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
if(isset($_POST['submit'])){
echo "Goodbye";
}
?>
</body>
</html>
If you WANT to use a function....
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
function test(){
echo "Goodbye";
}
if(isset($_POST['submit'])){
test();
}
?>
</body>
</html>
Lets say he file name is test.php where you have put all your code so the action values should be test.php
<form method="post" action="test.php" >
I am trying to create a very simple Control Panel for a web programming project, the "Show" function in my control panel works very well and shows all the elements by my Delete, Add and Update functions do not work at all.
Here's what I want to do with each function:
Add function -> I want to add an element to my database from the input elements
Delete function -> I want to delete the element that its ID is input by the user in the Control Panel
Update -> Using this function I want to change the Product Title of the selected Product.
These 3 functions don't work, when I input data in the webpage I see that nothing is added/altered to/in the tables.
Here's the code:
add.html
<title>Add</title>
</head>
<body>
<form action="add.php" method="post">
<font size="+2" color="#CC0033">ID</font><input type="text" name="ID" />
<font size="+2" color="#CC0033">Product Title</font><input type="text" name="ProductTitle" />
<font size="+2" color="#CC0033">Price</font><input type="text" name="Price" />
<font size="+2" color="#CC0033">Quantity</font><input type="text" name="Quantity" />
<input type="submit" value="Insert" />
</form>
</body>
</html>
add.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Done</title>
</head>
<body>
<?
$ID=$HTTP_POST_VARS["ID"];
$ProductTitle=$HTTP_POST_VARS["ProductTitle"];
$Price=$HTTP_POST_VARS["Price"];
$Quantity=$HTTP_POST_VARS["Quantity"];
$db=mysql_connect("localhost","root","");
if($db==false)
{
print "Error";
exit;
}
mysql_select_db("Computer");
$query=("insert into Products values('".$ID." ',' ".$ProductTitle."',' ".$Price."',' ".$Quantity."')");
mysql_query($query);
?>
</body>
</html>
delete.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
Enter the ID of the product that you wish to delete:
<form action="delete.php" method="post">
<input type="text" name="UserInput">
<br>
<input type="submit" value="Delete">
</form>
</body>
</html>
delete.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?
$UserInput=$HTTP_POST_VARS["UserInput"];
$db=mysql_pconnect("localhost","root","");
if(!db)
{
print "Error";
exit;
}
mysql_select_db("Computer");
$query=("delete from Products where ID=".'$UserInput');
mysql_query($query);
?>
</body>
</html>
update.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="update.php" method="post">
Product ID<input type="text" name="ProductID">
Product Name<input type="text" name="ProductTitle">
New Product Name<input type="text" name="NewProductTitle">
<br>
<input type="submit" value="Update">
</form>
</body>
</html>
update.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?
$ProductID=$HTTP_POST_VARS["ProductID"];
$ProductTitle=$HTTP_POST_VARS["ProductTitle"];
$NewProductTitle=$HTTP_POST_VARS["NewProductTitle"];
$db=mysql_pconnect("localhost","root","");
if(!$db)
{
echo "Error";
exit;
}
mysql_select_db("Computer");
$query=("update Products set ProductTitle='".$NewProductTitle. "' where ID=$ProductID");
mysql_query($query);
?>
</body>
</html>
firstly, good on you for trying to come up with a solution, however, as other commenters have said, you do have issues in your script. Firstly, I'd consider replacing $HTTP_POST_VARS["ID"]; with the $_POST global variable, and also running an if(isset()) to ensure all fields were set when the user submitted the form.
Secondly, you should consider using MySQLi or PDO for handling your database connection, as they can also provide validation and filtration to prevent injection attacks as Marc B mentioned. It's a good attempt if you are an absolute beginner and I feel if you stick at it you can progress further, and this community is great for getting answers and understanding where you have gone wrong. Hopefully this helps and good luck! :)
This is my coding for the search form:
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="https://excelforth.com/search1.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>
</body>
</html>
The thing is , When I submit this form on my web page , It goes to the php Results page and displays the results. But the thing is that it is just a plain page with just the results displayed.
My search page :
www.mysite/test/certification-database-search/
The Php file is located in my webroot folder . I was told that for wordpress , the individual subpages cannot be edited .
How do I :
1.Display the results on the same page as the search page. Not a completely new page.
2.Retain the page layout and theme / headers/ footers of the page
3.If possible run the .php file in /certification-database-search/ and query it from there. instead of using the one in my webroot folder.
THANKS!!
Put your form and PHP in one page, PHP on top with HTML below, then use action=""
Use the variables from the inputs as $var=$_GET['var']; then echo $var;
Sidenote: If you want to stop a process, you can use die(); or exit();
You can put a message inside it; i.e.: die("Enter a search term");
A basic example:
<?php
if(isset($_GET['submit'])){
$query=$_GET['query'];
echo $query;
}
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="" method="GET">
<input type="text" name="query" />
<input type="submit" name="submit" value="Search" />
</form>
</body>
</html>
An alternative, showing an error message if field is empty:
<?php
if(isset($_GET['submit'])){
if(empty($_GET['query'])){
echo "Enter a search term";
}
$query=$_GET['query'];
echo $query;
}
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="" method="GET">
<input type="text" name="query" />
<input type="submit" name="submit" value="Search" />
</form>
</body>
</html>
how to input a number range, from 2 text fields, this is my code is seems to be not working, the range function i want it to be inserted to mysql database but this code is just to test if i can get the 2 textfields to connect to the range function. anyone can help me solve this problem?
<?php
$from = '.from';
$to = '.to';
$number = range('$from','$to');
print_r ($number);
?>
<!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>New Waybill</title>
<link rel="shortcut icon" href="../img/icon.ico">
<link rel="stylesheet" type="text/css" href="../css/forms.css" />
</head>
<body>
<form id="form3" name="form1" method="post" action="">
<p>From:
<input type="text" name="from" id="form_number" class="from" value="0"/>
- To:
<input type="text" name="to" id="form_number" class="to" value="50" />
</p>
<p>Waybill Booklet:
<select name="waybill_booklet" id="form_list">
</select>
</p>
<p>
<input type="submit" name="button" id="form_button" value="OK!" />
</p>
</form>
</body>
</html>
Replace your php code with this:
<?php
if (!empty($_POST)) {
$from = $_POST['from'];
$to = $_POST['to'];
$number = range($from,$to);
print_r($number);
}
?>
To access a form post in PHP you use the $_POST superglobal. More info found here: http://www.php.net/manual/en/reserved.variables.post.php.
I've put a check to see if it's empty or not at the start, so when the page first loads it won't attempt to run the PHP code. Presumably you'll then replace the print_r with whatever you need to insert the data into your database.
I am looking for a way for a text field and submission button to take me to a new site, fill out a text input field and hit enter. The page I want to link to is Googles speed test. I know I can link to spped test results as well like this:
https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false
but how can I have a customer fill out a "test my page" field on my site, hit submit, and it create a link to:
https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false
with their field submission in the "YOURDOMAINHERE" area of the link. This seems linke not a huge task but i cannot wrap my head around it, php, javascript??? not sure. Any help would be greatly appreciated.
You can do something similar to this, although if Google catches you according to their TOC they *could* lock or ban your account:
<?php
if (!empty($_POST['url'])){
$url = preg_replace('!http[s]://!','',strip_tags($_POST['url']));
$url = preg_replace('![%]!','_',urlencode($url));
$newlink = "https://developers.google.com/speed/pagespeed/insights#url=$url&mobile=false";
$page = '<!doctype html>
<html lang="en">
<head>';
$page .= '<script type="text/javascript">
function replaceDoc()
{
window.location.replace("'.$newlink.'")
}
</script>';
$page .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page for testing</title>
</head>
<body onload="replaceDoc()">
Test your page.
</body>
</html>
';
} else {
$page = '<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Checker</title>
</head>
<body>
<form method="post">
Enter your URL:<br />
<input name="url" type="text" style="width:50em;" />
<input type="submit" name="submit" value="Check your page" />
</form>
</body>
</html>';
}
echo $page;
?>
Be careful directly injecting headers:
<?php
if (!empty($_POST['url'])){
$url = preg_replace('!http[s]://!','',strip_tags($_POST['url']));
$url = preg_replace('![%]!','_',urlencode($url));
$newlink = "https://developers.google.com/speed/pagespeed/insights#url=$url&mobile=false";
header("Content-Type: application/x-www-form-urlencoded");
header("Referer: https://developers.google.com/speed/pagespeed/insights");
header("Location: $newlink");
} else {
$page = '<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Checker</title>
</head>
<body>
<form method="post">
Enter your URL:<br />
<input name="url" type="text" style="width:50em;" />
<input type="submit" name="submit" value="Check your page" />
</form>
</body>
</html>';
}
echo $page;
?>
make a submit.php
have the form submit to that script with the "DOMAIN NAME"
your php should look something like this..
$domainName = $_POST["domainname"];
$redirectURL = "https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2F".$domainName."&mobile=false";
header('Location: $redirectURL');
That script should work havn't tested it but it'll give you an idea...
make sure you sanitize your inputs too... :) good luck
Hows about a simple str_replace? Something like:
<?php
$link = "https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false";
$replace = "YOURDOMAINHERE";
if(isset($_POST['myDomain'])){
$newHeader = str_replace($replace, $_POST['myDomain'], $link);
header("Location: ".$newHeader);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>googleDomain</title>
</head>
<body>
<form action="domain.php" method="post">
<input type="text" name="myDomain" />
<input type="submit" />
</form>
</body>
</html>
I haven't tested this at all, but should be something pretty similar