i am writing a unit converter php program. i have the set up of the page, but it seems like my php file is not being found. when i click the submit button i am brought a an error page. this is my html code.
<html>
<head>
</head>
<body>
<form action="hw7.php" method="post">
<h2>Convert length:</h2>
<p>Select conversion direction: <br />
<input type="radio" name="dir" value="1"
checked="checked"/> Feet to meters<br />
<input type="radio" name="dir" value="2" /> Meters to feet<br
/>
</p>
<p>Value to be converted: <br /><input type="text"
name="cvalue" /></p>
<p><input type="submit" value="Convert" /></p>
</form>
</body>
</html>
and here is my php file
<?php
$fTOm = $_POST["cvalue"] * 3.2808;
$mTOf = $_POST["cvalue"] / 3.2808;
echo "Result: ";
if ($POST[ 'dir'] == "1") <?php echo "$_POST["cvalue"]; ?> feet = <?php echo "fTOm"; ?> meters;
?>
Your PHP script is likely causing a server error due to improper coding. Check your server logs for PHP related errors, and possibly turn on error_reporting for PHP.
Within your PHP script, you have a statement of:
if ($POST[ 'dir'] == "1") <?php echo "$_POST["cvalue"]; ?> feet = <?php echo "fTOm"; ?> meters;
Since you're already in a PHP script, why are you using the <?php echo inline an if statement?? Correct this issue and try running the script by directly calling it.
Change the if line to :
if ($POST[ 'dir'] == "1") {
echo $_POST["cvalue"] . "feet , " . $fTOm . " meters";
}
You should know that the hw7.php file must be in the same directory as the HTML page that is calling it from a form.
I will recommend to use always relative URLs or the contexT_path when possible.
try:
<form action="./hw7.php" method="post">
try this maybe it will help:
<form action="./hw7.php" method="post">
Also remove all those syntax errors in the php script.<?php .... ... ?> can not have another <?php inside of it.
Related
Newbie here, and self-taught in PHP. I have a questionnaire where each question has 2 answers, and users can add any combination of numbers to each, as long as they equal, i.e. 10. So far 2 questions (will be more) so each question's answers should equal 10, therefore total submitted values should equal 20. I can't find a way to only allow submit if these conditions are met. I would really appreciate any help.
Currently using this for testing purposes:
<input type="submit" value="Check!" name="check"/>
...at the bottom.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<?php
// Adding stuff
if(isset($_POST['check']))
{
$q1total=$realist[1]+$idealist[1];
$q2total=$realist[2]+$idealist[2];
$grandtotal=$q1total+$q2total;
}
?>
<body>
<form method="post">
Q1: <input type="text" name="realist[1]"/> <input type="text" name="idealist[1]"/>
<?php echo $q1total; ?>
<br>
Q2: <input type="text" name="realist[2]"/> <input type="text" name="idealist[2]"/>
<?php echo $q2total; ?>
<br>
<br><br>
Grand total: <?php echo $grandtotal; ?>
<br><br>
<input type="submit" value="Check!" name="check"/>
</form>
</body>
</html>
I'm also a self-taught PHP "newbie" as you describe it. I would honestly create a seperate PHP file that checks if the conditions are met.
Your form would be:
<form method="post" action="yourfile.php">
And in the php file:
$realist1 = $_POST["realist\[1\]"];
$idealist1 = $_POST["idealist\[1\]"];
$realist2 = $_POST["realist\[2\]"];
$idealist2 = $_POST["idealist\[2\]"];
if ($realist[1]+idealist[1]== 10 && $realist[2]+idealist[2] == 10)
{
echo "<button value='correct'/>";
}else{
echo "<button value='incorrect' disabled/>";
}
Obviously you have to adjust some stuff but hopefully this can help.
So i got this code, at the moment it is repeating everything , and i just wanted it to repeat the echo, so i get all usernames from it, if i leave it as it is it will also repeat the form when i press a username. Every time i tried to ajust it, it just gave me syntax errors
<?php do { ?>
<?php
$username = $row_mensagens['username'];
$user = $row_mensagens['id'];
if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
Introduz mensagem : <br>
<textarea name='message' rows='7' cols='60'></textarea>
<br><br>
<input type='submit' value="Send Message" />
</form>
<?php
} else {
echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
}
?>
<?php } while ($row_mensagens = mysql_fetch_assoc($mensagens)); ?>
that do { } while() will always repeat as many as the number of records come from database.
You can do it this way:
<?php
if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
<input type="hidden" name="user" value="<?php echo $_GET['user']; ?>" /> <!-- hidden field so you can process to who -->
Introduz mensagem : <br>
<textarea name='message' rows='7' cols='60'></textarea>
<br>
<br>
<input type='submit' value="Send Message" />
</form>
<?php
} else {
do {
$username = $row_mensagens['username'];
$user = $row_mensagens['id'];
echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
} while ($row_mensagens = mysql_fetch_assoc($mensagens));
}
?>
Move do { inside else and show the form only if you have a $_GET['user']
I have also added for you a hidden field, so you know who to send message.
Hope you understand how this works. Documentation on Control Structures: do-while
I also suggest to make that form a post form, as by default it is a get form, and since you have a textarea you are more likely to bump into errors if the message is too long.
LE: Another suggestion, try to move to PDO or mysqli_* functions since mysql_* functions are considered deprecated as of PHP 5.5 and have some good chances to be removed.
First, thanks for taking a look at this. I am trying to create an array of forms that acts as a dynamically sized results list. From the results that were given the user can click 'detail' (a submit button) to get further information on the result which is why I am attempting to create an array of forms. Here is what I had tried, which compiled but the buttons aren't doing anything. Any help would be great :)
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$numbers=array(1,2,3,4,5);
$listsize=count($numbers);
for($currentnum=0;$currentnum <$listsize;$currentnum ++){
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
</form>
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
</body>
</html>
This is really just meant to demonstrate what I am trying to do (thought that would be easier without functions out of scope of question).
Try changing the HTML for the button:
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
Should be:
<input type="submit" value="Submit" name="button<?PHP echo $currentnum?>" />
Close bracket for your for loop so that your check is inside it:
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
Should be:
<?php
echo "<br/>";
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
}
?>
If you learn to indent your code you'll find these kinds of bugs much easier to spot!
Other than that you're good to go...
You need to change your input types from 'button' to 'submit' so that they submit the forms, then you need to move
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
inside of the for loop
I am new to PHP, I tried to work w3 schools example of posting data on forms..
It never works for me... the webpage doesn't display any data, I tried several forums and also SO that never helped.. I still keep getting it empty!
Example #1: A simple contact from - HTML code
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
Example #2: Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
Expected output of this script may be:
Hi Joe. You are 22 years old.
Actual Output:
Hi . You are years old
The Post parameter is not displaying data.. Any help is really appreciated.
What W3Schools (PHP Form Handling) fail to mention is, that the entire (2) bodies of code need to either be inside a single file, or in 2 seperate files in order for it to work as expected.
However, the code from W3Schools and the OP are not indentical and have been modified, using htmlspecialchars and (int)
If you wish to make use of htmlspecialchars, do the following in your welcome.php file:
<?php
$fname = htmlspecialchars($fname);
?>
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo (int)$_POST['age']; ?> years old.
Form used:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
I did not see any mention on the W3Schools website about the use of htmlspecialchars or (int)
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If you wish to make use of htmlspecialchars then you should the following syntax:
$fname = htmlspecialchars( $fname );
And placed within <?php and ?> tags such as:
<?php
$fname = htmlspecialchars( $fname );
?>
NOTE: I know next to nothing about running a Webserver from my own computer, yet from information I found here on SO
mention that in order to access your PHP files, you need to type in http://localhost in your Web browser's address bar and the folder where your file is in.
Please visit this answer
StackOverflow did not let me insert the codes on that page, for one reason or another.
In your <form> tag the "action" is where your POST data is being sent. So does your file structure look like this?
//index.php
<form action="action.php" method="POST"> // <-- make sure to capitalize method="POST" as well
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
.
//action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT 2
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.
'post' or 'POST' both works fine in form tag.
The following should be in action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If still you get this then go to php.ini and set your errors to E_ALL and E_STRICT
and check whats the error.
Most probably it should work now...
In your form check if it is not sending empty values.
STEP 1
copy and paste the following code in your text editor and run it. It will allow you to test the values from the form without redirecting the page.
The following code should be in index.php
<form action="action.php" method="POST" onsubmit="return validate()">
<p>Your name: <input type="text" name="name" id="name"/></p>
<p>Your age: <input type="text" name="age" id="age"/></p>
<p><input type="submit" /></p>
</form>
<script type="text/javascript">
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
alert ("Name="+name+" age="+age);
return false;
}
</script>
This code will check if the values are getting entered correctly without redirecting the page to action.php.
Step 2
If you are getting the desired output from the previous code then you can replace the validate function with the code below. (replace everything between the script tags)
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
if (name==null || name==""){
return false;
}
if (age==null || age==""){
return false;
}
return true;
}
If both name and age are filled in the form, the submit will now redirect to action.php
Step 3
In action.php use the following code.
<?
//These code goes in action.php
extract ($_POST);
echo "Hi $name. You are $age years old";
?>
edited with instructions on OP's request
Simply ensue that your form is running from your server (http://localhost..) and not the form location itself(file:///C:xampp..). Happy coding
I am just starting to learn php, how would I initiate a echo statement after a submit button is pushed, or even a anchor tag.
Here is my code so far
form name="myform" method="get" actions="madlib01.php"
Name: <input type="text" name="name" /> <br />
<input type="submit" name="submit" />
form
<?php
$Name = $_GET['name'];
$hello .= "Hello $Name";
echo $hello //I would prefer the echo to happen after the submit button is hit
?>
the correct attribute for your form tag is "action", not "actions"
When the form is submitted, a new request is sent to the server (in your case, using GET).
So to do it all in one page:
form.php:
<form action="form.php" method="GET">
<input type="text" name="name"/>
<input type="submit">
</form>
<?PHP
if (! empty($_GET['name'])){
echo 'Hello, ' . $_GET['name'];
}
?>
You will first need to check if PHP has received your GET parameter using isset or array_key_exists:
if(isset($_GET['name']) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
}
or:
if(array_key_exists('name', $_GET) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
} else {
//example: default to something if nothing has been passed
echo "Hello Guest";
}
Also note, if you're submitting to the same page, you can omit the action attribute from your form tag altogether:
<form method="GET">
echo $hello
You've just gained an HTML-injection vulnerability. If someone sends your user to:
http://www.example.com/madlib01.php?name=<script>stealYourCookies()</script>
you've got problems.
Yes, this is a My First PHP Script. That doesn't make security optional. This is a mistake every tutorial makes: teaching bad practice from the start, treating correctness (and security, which is a subset of correctness) as an optional extra.
The result is that most PHP code out there is full of holes. But there's no need for yours to be! Every time you place a pure-text string into a surrounding HTML context, escape it properly:
echo htmlspecialchars($hello);
I tend to define a function with a shorter name than ‘htmlspecialchars’ to do that for me, as I'm lazy.
<?php
function h($text) {
echo(htmlspecialchars($text, ENT_QUOTES));
}
$name= '';
if (isset($_REQUEST['name']))
$name= trim($_REQUEST['name']);
?>
...
<?php if ($name!=='') { ?>
<p> Hello, <?php h($name); ?>! </p>
<?php } ?>
<form method="get" action="madlib01.php">
<p>
<label for="namefield">Name:</label>
<input id="namefield" type="text" name="name" />
</p>
<p>
<input type="submit" />
</p>
</form>
Now if you say your name is Mister <script>, the page will greet you exactly as such, angle brackets and all, instead of trying to run JavaScript. This is the correct output and thus also secure.