I read all questions related to $_GET and $_POST. I didn't find a solution for my case. The code is very simple, it should be working, but I get empty field when using $_GET.
Here is my code :
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Colors</title>
</head>
<body>
<h2> Project</h2>
<form method="get" action="display.php">
R:<input type="text" name="r" size="3"/> <p/>
G:<input type="text" name="g" size="3"/><p/>
B:<input type="text" name="b" size="3"/><p/>
<input type="submit" value ="Show me">
</form>
</body>
</html>
and php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Colors</title>
</head>
<body>
<h2>Color Sampler </h2>
<?php
$r=$_GET['r'];
$g=$_GET['g'];
$b=$_GET['b'];
$rgb=$r . ',' . $g . ',' . $b;
?>
R:<?php echo $r; ?>
G:<?php echo $g; ?>
B:<?php echo $b; ?>
<p/>
<div style ="width:150px;height:150px;
background-color:rgb(<?php echo $rgb;?>"/>
</body>
</html>
and this is what I get:
Project 2 Color Sampler
R: G: B:
so it is empty, like it doesn't see the values he needs to return.
What is wrong?
You can delete 'method="get"' from cause it uses be default.
Also, try to get params from $_REQUEST. Good luck!
Related
I am writing a simple PHP program to calculate the users BMI and return a string indicating whether he is in the normal range or obese. I have just begun so I need to retrieve the user input from html input elements. The problem is, I am getting a non well formed numeric value error. Here is my code:
<!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>
<?php require("header.php") ?>
<title></title>
</head>
<body>
<h1>Lab #05 -- BMI Calculator</h1>
<br /><br /><br />
<form action="Lab05b.php" method="post">
<input id="imperial" name="imperial" type="text" value="0.00" /> <label for="imperial">Height in inches / centimeters</label>
<br />
<input id="metric" name="metric" type="text" value="0.00" /> <label for="metric">Weight in pounds / kilograms</label>
<br /><br />
<input type="radio" name="imperial" id="imperial" value="imperial" /> Imperial
<br />
<input type="radio" name="metric" id="metric" value="metric" /> Metric
<br /><br />
<input type="submit" />
<input type="reset" />
</form>
<?php require("footer.php"); ?>
</body>
</html>
and here is the the Lab05b.php include:
<!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>
<?php require("header.php") ?>
<?php require("Lab05b_Functions.php") ?>;
<title></title>
</head>
<body>
<h1>Lab #05 -- BMI Calculator</h1>
<br /><br /><br />
<?php echo "<p style='color: <?php echo $color; ?>' ?>Your body mass index is in the {$result} range</p>" ?>
<?php require("footer.php"); ?>
</body>
</html>
and here is the functions file:
<?php
$imperial = filter_input(INPUT_POST,'imperial');
$metric = filter_input(INPUT_POST, 'metric');
echo $imperial. " " + "imperial units";
$color = "red";
$result = "Obese";
?>
For this assignment it is ok for the value to be entered into the textfield as an integer. So right now I am ONLY concerned with tracking down the non wel formed numeric value error.
Thank you for your help,
DB
Use:
echo $imperial . " " . "imperial units";
Instead of:
echo $imperial. " " + "imperial units";
+ is for addition, . is for concatenation.
From the docs:
Strings may be concatenated using the '.' (dot) operator. Note that the '+' (addition) operator will not work for this.
I have two pages: order.php and checkout.php. I have 3 items in the order page and I want to pass quantity of the items to the checkout page.
I guess the problem is with isset($_POST['Submit']). My guess is that it still goes straight to the checkout page when I press submit without putting values to session variables.
I have been trying to pass the values from order like this:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!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">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
echo <<<END
<form action="checkout.php"
method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" /></label></p>
<p>Frigge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="submit" value="Order"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['Submit'])) {
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
?>
</body>
</html>
And here in checkout I'm trying to print one session value as test:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!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">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
print ($_SESSION["maara1"]);
?>
</body>
</html>
"I guess the problem is with isset($_POST['Submit'])."
Yes, the problem is with if (isset($_POST['Submit']))
Your submit button is named submit instead of Submit.
<input type="submit" name="submit" value="Order"/>
Change it to
<input type="submit" name="Submit" value="Order"/>
They are case-sensitive.
Or leave it the way it is and change
if (isset($_POST['Submit']))
to
if (isset($_POST['submit']))
Either method will work. You just need to make them both (letter cases) match.
What is happening is, PHP is looking for a named element called Submit.
That alone would have and should have thrown:
Undefined index Submit...
Sidenote:
I noticed:
<input type=hidden name=price1 value=5>
and other inputs.
I would suggest that you use quotes around it:
<input type="hidden" name="price1" value="5">
while doing the same for the others, as it could have adverse effects and/or unexpected results.
I have seen that happen before.
Edit:
You'll need to move this whole block in your second page and not be in the first page.
if (isset($_POST['Submit'])) {
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
then do print ($_SESSION["maara1"]); from there.
Your first page does not recognize the POST variables because they have not been set.
Edit #2:
You could try setting a value value="{$_SESSION["maara1"]}" to your inputs.
I.e.:
Sidenote: You could try <form action="" method="post"> instead of <form action="checkout.php" method="post">
However, I'm unsure if the following is what you're looking to get. It does work if action="" but it won't work trying to get a value before it has been set. That's not how sessions work.
It's kind of like expecting an A+ in a test you haven't written yet, if I can say.
echo <<<END
<form action="checkout.php" method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" value="{$_SESSION["maara1"]}" /></label></p>
<p>Frigge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="Submit" value="Order"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['Submit'])){
$_SESSION["maara1"] = $_POST["maara1"];
$mar1 = $_SESSION["maara1"];
echo $mar1;
}
But as you said in a comment: "what is the point of me using session variables on second page then if I can refer them from $_POST anyways?"
A: Exactly.
I went and did it like this. With two button and it is also more close to webstore now. So I press first button to add the quantity of the items and at the same time it sets session variables. Then I press second button to proceed to the second page which is checkout. What you guys think?
First page:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!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">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
echo <<<END
<form action="teht7.php"
method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" /></label></p>
<p>Fridge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="submit" value="Valitse tuotteet"/></p>
</form>
<hr />
<form action="teht7_kassa.php"
method="post">
<p><input type="submit" name="submit" value="Siirry kasssalle"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['submit'])) {
$ostostenmaara = $_POST["maara1"] + $_POST["maara2"] + $_POST["maara3"];
print ("Ostoskorissa on: $ostostenmaara tuotetta");
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
?>
</body>
</html>
Second page:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!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">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
$maara1 = $_SESSION["maara1"];
$maara2 = $_SESSION["maara2"];
$maara3 = $_SESSION["maara3"];
$summa = $_POST["price1"]*$maara1+$_POST["price2"]*$maara2+$_POST["price3"]*$maara3;
print ("Ostostesi yhteissumma on: $summa euroa");
?>
</body>
</html>
I have used Javascript to enable or disable a button, depending on the input value. I'm new to PHP, so I don't know why this isn't working. Here's the PHP code:
<?php
session_start();
include_once('header.php');
include_once('functions.php');
$_SESSION['userid'] = 1;
?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Microblogging Application</title>
</head>
vbody>
<?php
if (isset($_SESSION['message'])){
echo "<b>". $_SESSION['message']."</b>";
unset($_SESSION['message']);
}
?>
<form method='post' action='add.php'>
<p>Your status:</p>
<textarea name='body' rows='5' cols='40' wrap=VIRTUAL></textarea>
<input type="text" name="age" />
<input type="text" name="poscode" />
<input type="submit" name="submit" value="Post" disabled="disabled"/>
</form>
<script type="text/javascript">
$('input[name="age"], input[name="poscode"]').change(function(){
if ($(this).val())
{
$("input[name='submit']").removeAttr('disabled');
}
});
</script>
<?php
$posts = show_posts($_SESSION['userid']);
if (count($posts)){
?>
<table border='1' cellspacing='0' cellpadding='5' width='500'>
<?php
foreach ($posts as $key => $list){
echo "<tr valign='top'>\n";
echo "<td>".$list['body'] ."<br/>\n";
echo "<small>".$list['stamp'] ."</small></td>\n";
echo "</tr>\n";
}
?>
</table>
<?php
}else{
?>
<p><b>You haven't posted anything yet!</b></p>
<?php
}
?>
</body>
</html>
How do I change it so when the value of the input = "foo", the button is now clickable.
Thanks in advance
-Ben
I can't make sense of your question, but if you're asking what I think you're asking (which is "how do I enable the button if the form is populated on page load?"), change your script to the following:
$.ready(function(){
$('input[name="age"], input[name="poscode"]').change(function(){
if ($(this).val())
{
$("input[name='submit']").removeAttr('disabled');
}
}).change();
});
I added the change call to trigger the change event handler, and wrapped it in jQuery's ready handler.
Try using the attr function like below
$('#element').attr('disabled', true);
Or
$('#element').attr('disabled', false);
Because different browsers treat the disbaled attribute differently, using true and false lets jquery handle the cross browser issues
I am trying to ensure the parameter from a GET method of page 2 (which displays the result) will not be changed by others in the URL (the variables behind the question mark) by making sure users clicked the submit button on page 1 (which contains the form).
The professor ask us to use isset() to implement this requirement
Here's the login page: (page1.php)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...> <html
xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Login
Page</title> </head>
<body>
<label>User ID:</label>
<input type="text" name="uid">
<br />
<input type="submit" value="SubmitCredentials" name="SubmitCredentials"/>
<br />
</form> </body> </html>
And this is the second page: (page2.php)
<?php
// get the data from the form
$uid = $_GET['uid'];
if(!isset($_GET['SubmitCredentials'])){
include('error.html');
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional
...>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HELLO</title>
</head>
<body>
<label>Hello,</label>
<span><?php echo $uid; ?></span><br />
</body>
</html>
However, the if statement doesn't seem to be working. It's supposed to forward the page to error.html when I change the variable uid in the URL (e.g. when I change the URL to http://localhost/1/page2.php?uid=admin) but it's not. How can I fix it?
PS: a relevant tutorial I found on google>>>
http://www.homeandlearn.co.uk/php/php4p7.html
Try:
if(!isset($_GET['SubmitCredentials'])){
header('Location: error.html');
}
Isset is not safe on Arrays. Try comparing with the ===operator instead:
if(!($_GET['SubmitCredentials'] === 'SubmitCredentials')) {
include('error.html');
exit();
}
Page #1:
<form method="GET" action="...">
<input>
<input>
<input>
</form>
Submit URL:
http://localhost/1/page2.php?SubmitCredentials=SubmitCredentials&uid=admin&i_like_php=yes&...
I have an HTML form with three fields, first name surname and mid. name and I want to save this information in a php page. The problem is I don't know how to save multiple entries in array without a connection of database.
HTML form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Εργασία 11 - Βασίλης Τρίγκας</title>
</head>
<body>
<h4>Καταχώριση βιβλίου</h4>
<form action="form_action.php" method="get">
Συγγραφέας <br/><input type="text" name="fnwriter" /><br/>
Τίτλος <br/><input type="text" name="title" /><br/>
Εκδότης<br/> <input type="text" name="editor" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP File
<?php
function display_welcome(){
print("Σωστή εισαγωγή, ");
print($_GET['fnwriter','title','editor'])
}
function display_empty_form(){
print <<<_HTML_
<FORM method="post" action="$SERVER['PHP_SELF']">
<BR/>
<INPUT type="submit" value="SUBMIT NAME">
</FORM>
_HTML_;
}
if ($_GET['fnwriter','title']) {
display_welcome();
}
else {
display_empty_form();
}
$Book=array("fnwriter","title","editor");
$fwriter=$_GET['$fnwriter']
$title=$_GET['$title']
$editor=$_GET['$editor']
echo $fnwriter;
echo $title;
echo $editor;
for ($i = 0; $i < count($fnwriter,$title,$editor); ++$i) {
echo "Book $i= $fnwriter[$i],$title[$i],$editor[$i]<br />";
}
//echo $Book[$_GET];
//print_r($);
?>
I don't really get your question, is that about saving data to a file, or about processing $_POST data?
If it's the first one, you have a plenty of possibilities, from simple CSV (or any other separator) to XML ;)