I am taking an HTML5 Tutorial on YouTube. Part of the course discussing CSS3 and PHP.
In the PHP section, the instructor provides the following code to print the form input to the screen:
<?php
$usersName = $_GET['name'];
$usersPassword = $_GET['password'];
echo 'Hello ' . $usersName . '<br>';
echo 'That is a great password ' . $usersPassword . '<br>';
?>
The HTML it refers to is:
<form method="GET" action="phpscript.php">
Your Name: <input type="type" name="name" size="50" maxlength="50"><br>
Your Password: <input type="password" name="password"><br>
Your history:<br>
<textarea name="history" rows="20" cols="50">Just random words</textarea>
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Reset">
</form>
The PHP variables do not print to the page. This is the output:
echo 'That is a great password ' . $usersPassword . '<br>';
I double checked what the instructor had in the video, and it is exactly the same. Then, I opened the Console in Firefox and I saw an error on the PHP page that said that the doctype and character encoding was not declared. In the instructions, the instructor has a php file with only php code in it. So, I got rid of those two errors by using my html file as a template and adding the php code between my <main></main> tags, but got the same output. So, I added <script type="text/php"></script> around the php code, then nothing printed.
Please help me so I can continue moving forward in this tutorial. The instructor was using a Mac and Chrome and running the files locally. I am running everything locally in Windows 7 and Firefox. I tested things out using IE and Chrome but got the same results.
Thank you!!
Sounds to me like either you do not have php running anywhere (Since it is server side you need a server to run it and I recommend XAMPP for this use) or your file has the ending .html but you would need a .php to run php code. Just putting it in between the <main> tag won't work.
You want to output the script without running it?
Try <xmp></xmp>
For example, if you want to print a link like this, it will look like this:
Google
Google
but with the <xmp> tag, it looks like this:
Google
For just startup you don't need a fully fledged web server , you can use PHP's inbuilt server simply go in php's installation directory save your php then bring a command prompt on this directory and run
php -S localhost:80
Now access file using http://localhost/file.php
You need to keep the shell open as long as you are using php
Related
I'm just starting to learn form and PHP. I am testing a simple HTML file from W3Schools here with the following code:
<html>
<body>
<form action="welcome.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
It is supposed to pass the information to a PHP file called welcome.php, which looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?><br>
Random thing: <?php $rand = "bananas"; echo "$rand"; ?>
</body>
</html>
When I run the HTML file on Chrome, fill in the name and the email and press submit, the page looks like this:
Welcome
Your email address is:
Random thing:
While the HTML part works, the name variable, email variable and rand variable isn't printed.
[EDIT]: I solved it by transferring the files to a server and running it by going onto the actual webpage and it worked. Also Azeez Kallayi suggested running it on Xamp.
I think you are not using any server, just opening in broswer without any server. Also correct semicolon as in the above comment.
Since PHP is a server side programming language , you need a server to execute the PHP scripts.
There are many applications available that you can use as local servers and run your application. Some of them are below.
Wamp, Xamp, Lamp
Hope this will help you
Change this
echo "$rand";
To this
echo $rand;
Firstly you should get knowledge what PHP is and how you can use it.
PHP is a server-side scripting language. So if you try to run it like a html file you will not see the expected output. You need to understand what is a server side server-side scripting is.
If you have have jumped into coding is very essential you should know how you should debug to resolve your error.
One easy way is to enable error reporting.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
Hope this will give you a start .
This is a repost because my question before was not clear enough and deleted it to prevent further confusion.
I have a shell script that takes one argument. It uses the argument to cURL a website which then uses grep, head and ${variable#*>} to extract some data from a website.
In terminal everything works well with the command below. $stockCode being the argument. ./priceAlert.sh $stockCode
I tried to make this into a working webpage on my local host. (MAMP osx) I tried different solutions but the website will always spit out "not from same origin" except for the shell script.
After searching for how to integrate shell scripts with web I came across PHP and now I am figuring out how to get PHP to talk to my shell script.
Structure of my project:
HTML page with an input inside a form
<form action="priceAlert.php" method="post">
<input type="text" name="stockCode"></input>
<input type="submit" name="submit" value="Submit me!">
</form>
A PHP page that receives the input from the HTML page via POST
$stockCode2 = $_POST['stockCode'];
echo $stockCode2;
$output = shell_exec('sh priceAlert2.sh ' . $stockCode2);
echo "<pre>$output</pre>";
Now the interesting part...
The value of $stockCode2 from the HTML form is 033360.
If I use $stockCode2 as POST, the shell script extracts the part of the website that I do not want. (The code above extracts the part I do not want)
If I hard code the value, 033360, into $stockCode2 the shell script extracts the part I want. The code below extracts the part of the website I do want.
$stockCode2 = '033360';
$output = shell_exec('sh priceAlert2.sh ' . $stockCode2);
echo "<pre>$output</pre>";
If I echo everything they all show the value 033360 but behave differently.
Is there a way to fix this? Could this be a way for the target site to prevent scraping?
Thank you.
I am starting to learn php, I made a form that uses a php file to write the information into an xml file. The form is in an html file, and the php action in a seperate file. I am using EAsyphp 12.1
Whenever I use the upload form publishing it through dreamweaver everything goes fine.
But when I open it directly into the broswer it shows me part of the php code.
This is the code for html
<form enctype="multipart/form-data" action="chng.php" method="POST">
<div>Name <input type='text' name="tf" /> </div>
<div>image <input name="uploadedfile1" type="file" /> </div><br />
<input type="submit" />
</form>
The php is
$target_path4 = "../img/";
$target_path4 = $target_path4 . basename( $_FILES['uploadedfile4']['name']);
$name4="../img/linkimagen.jpg";
$t = $_POST['tf'];
if(move_uploaded_file($_FILES['uploadedfile4']['tmp_name'], $name4)) {
$xml = simplexml_load_file('info.xml');
$xml->names[0]=$t;
file_put_contents('info.xml', $xml->asXML());
$xml->images[0]=$shortname;
file_put_contents('info.xml', $xml->asXML());
}
?>
The xml information is then picked up by another page using Jquery ajax.
Everything works fine if I launch the form file using dreamweaver as I said before. Also, sometimes the page that displays the information from the xml will display it if opened directly from the browser, other times it won't, however it will always display if opened from dreamweaver. I've found information for php not working in dreamweaver but not the opposite, as is my case. Is there anything I have to do so it works always? Thanks for any info!
Well if you upload it to your server - there is a php parser which parses your file
if you open it local it won't get parsed so it's just another text document.
Of course you could install something like xampp, to have a local webserver but if you are just starting out - it's a bit much I guess :)
-- Edit --
An addition: Please watch the behaviour of your browser. If you have already a webserver installed on your computer it has an adress which usually is something like h ttp://localhost/ or h ttp://127.0.0.1 - if your file doesn't get parsed it's probably because it opens directly like C:\programm files\dreamweaver\whatever\index.php and not
h ttp://localhost/index.php
hope this helps you ;).
I am trying to build a form in HTML that, once submitted, automatically generates a new webpage using data entered into the form.I'm usually a MATLAB and python user, so I tried them first. Matlab would parse the parse the data and save it to a new .txt file, but not a .html file. Python was much the same. After searching, I came across the suggestion to use PHP to create the new page from the form data. (Someone was using php to create user webpages with the users name, email, and a picture. I tried to adapt this to suit my needs, but it is not generating the new page as I thought it would. Instead, it just displays part of the PHP code. This is the form I made:
<form action="htmlData.php" method="post">
Product Name:
<input name="Name" size="20" type="text">
<br><br>
Project Lead Name:
<input name="PLname" size="20" type="text"> <br><br>
Team-members: <br>
<textarea name="Team_members" rows=10 cols=40> </textarea> <br><br>
Product Type: <br>
<input name="Product_Type" size="20" type="text"> <br><br>
Description: <br>
<textarea name="Description" rows=10 cols=40 type="text"> </textarea>
<br>
<br> <br>
<input value="Submit" type="submit" name="formSubmit">
<input value="Reset" type="reset">
<input value="Help" type="button" onclick="window.location.href='problems.html'">
</form>
...And this is the PHP file named htmlData.php:
ob_start();
$Name = $_POST['Name'];
$PLname= $_POST['PLname'];
$Team_members= $_POST['Team_members'];
$Product_type= $_POST['Product_type'];
$Description= $_POST['Description'];
$html = <<<HEREDOC
Product Name: $Name <br>
Project Lead: $PLname <br>
Team Members: $Team_members <br> <br>
Product Type: $Product_type <br>
Description: $Description
HEREDOC;
file_put_contents('newPage.htm', $html);
header()redirect.header('location: newPage.html')
What do I need to change so that once a user clicks submit, a new page is generated from the data and the user is then taken to the newly created page? Is this possible with what I have, or should I looking into using a different language?
It's possible with PHP and Python (perhaps MATLAB too).
Given your PHP code, there are a few small issues. First, the HEREDOC must be ended at the beginning of the line (there can't be any whitespace before the end HEREDOC).
Second, the PHP code to redirect is invalid. Try these changes:
<?php
$Name = $_POST['Name'];
$PLname= $_POST['PLname'];
$Team_members= $_POST['Team_members'];
$Product_type= $_POST['Product_type'];
$Description= $_POST['Description'];
$html = <<<HEREDOC
Product Name: $Name <br>
Project Lead: $PLname <br>
Team Members: $Team_members <br> <br>
Product Type: $Product_type <br>
Description: $Description
HEREDOC;
file_put_contents('newPage.htm', $html);
header('location: newPage.html');
exit;
The next issue you may encounter is that it cannot write to newPage.htm. You may need/want to specify a full path (e.g. /home/yoursite/public_html/files/newPage.htm).
You will need to make sure that directory is writable by the web server (as often the web server runs as a different user than your files are owned by).
Hope that helps.
If it shows a php code, that means that:
1. You dont have php installed,
2. Your http server like Apache installed.
Even if the 2 above conditions were met, the code you have is not a valid PHP code, and it would not work.
You are not yet good enough with PHP, you need to go back and learn the very basics before you try make any dynamic pages.
Instead, it just displays part of the PHP code
Seems like one of the following:
Have you installed PHP on your server?
Is the server configured to pass .php pages to be processed?
It might be worthwile to take a look here as displaying PHP code on the browser means the webserver isn't dealing with it correctly.
Once you think it's installed run phpinfo() see the docs.
You are missing an 'l' in your new file declaration:
file_put_contents('newPage.htm', $html);
Also, try physically creating the newPage.html file and place it in your project directory then see if the page is displayed or not. If this is the case I would check to see if you have PHP installed and if the server is configured to handle post. Usually it is configured to handle post or get but not usually both by default.
While not exactly solving your original problem, I hope I'm not too out of bounds with this:
Your problem is most often solved by using a database in conjunction with a language like PHP. Storing this information in a database after form submit will allow newPage.htm you have to accept GET parameters and dynamically display the corresponding data from the database. Example: newPage.html?id=245 will deliver that ID to the PHP code processing that request. PHP will use that ID to query your database for the proper information and display that to the page. This way you will not have one file for each form submission and the form data can easier to maintain. Check out something like: http://www.freewebmasterhelp.com/tutorials/phpmysql for more information.
Another aspect to keep in mind is security. Your current code puts direct user input into a file on your server. This is a huge security vulnerability. It is recommended you escape the user input.
If you not need to create new page, in start of your PHP code, remove ob_start(), and add
if(isset($_REQUEST['formSubmit'])){
....
....
}
your page will be created automaticly on submit press.
New to PHP and web development in general. I am trying to get information from an HTML form to appear in a table on another web page after clicking submit. So I installed Apache and then PHP on my local PC and expected to be able to test a PHP script locally however it does not return the information I was expecting.
The following is the code for the form:
<form method="post" action="showform.php">
Please fill out the following form if you would like to be contacted: <br/>
Name:<input type="text" name="name" /> <br/><br/>
Company: <input type="text" name="company"/> <br/><br/>
Phone: <input type="text" name="phone" /> <br/><br/>
Email: <input type="text" name="email" /> <br/><br/>
<input type="submit" name="Submit" value="Submit" />
</form>
The following is the code for the php script:
<table>
<tr><th>Field Name</th><th>Value(s)</th></tr>
<?php
if (empty($_POST)) {
print "<p>No data was submitted.</p>";
} else {
foreach ($_POST as $key => $value) {
if (get_magic_quotes_gpc()) $value=stripslashes($value);
if ($key=='extras') {
if (is_array($_POST['extras']) ){
print "<tr><td><code>$key</code></td><td>";
foreach ($_POST['extras'] as $value) {
print "<i>$value</i><br />";
}
print "</td></tr>";
} else {
print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
}
} else {
print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
}
}
}
?>
</table>
</body>
</html>
I know it works when used on the internet but how come it doesn't work locally. I have checked that apache and php are installed correctly. What could be the issue?
The current result is a table with $key and $value in the places where the correct values should be, in other words in the table cells.
Thanks for your help.
UPDATE: Now working though WAMPSERVER- thanks to all who helped!
Check out xampp. It installs Apache, Mysql, Perl, and PHP on your machine so you can test your entire site locally. It's a one shot installer and comes with a nifty control panel to enable/disable each service as needed.
You could also try WampServer.
It's a package containing: Apache, MySQL, PHP (Preconfigured for use in Windows).
There's no need for a server if using PHP 5.5+ - it has a built-in server (http://www.php.net/manual/en/features.commandline.webserver.php)
Just use:
$ cd ~/project
$ php -S localhost:8000
Sounds like your PHP script isn't being parsed if you're literally getting $value. Try running a basic phpinfo() to check this.
If PHP doesn't pick that up make sure the path to PHP is set in httpd.conf. I'd reccommend using xampp though - I'm terrible at all the local configuration and just let the auto installer set it up for me. I know enough to add new modules etc. later on.
On first glance, it looks like you're trying to place variables, but you're putting the variables inside of a string.
So this:
print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
Should be something like:
print "<tr><td><code>" . $key . "</code></td><td><i>" . $value . "</i></td></tr>\n";