Testing a PHP script on a local computer - php

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";

Related

Why my php script is not ran when I submit a form, using VSC as IDE?

I am trying to run a basic script using PHP. I am very beginner in PHP and I am trying to handle some data from a static website.
First, I created via VSC an HTML page which contains the following code:
suggestion.html
<form action="test.php" onsubmit="file_handler()" method='POST'>
Pseudonim: <input type="text" id="name" name='name'><br>
<div class="s">Sugestie:</div> <br><textarea maxlength="800" id="suggestion" name='text'></textarea><br>
<div class="span-ch"><span id="charNum"> </span> <span id="charText"></span></div><br>
<input type="submit" value="Submit">
</form>
The data of this form should be sent through a POST method to test.php, which contains the following code:
<!DOCTYPE html>
<html>
<?php
if(isset($_POST['name']) && isset($_POST['text'])) {
$filename = preg_replace('#[^A-Za-z0-9_-]#', '', $_POST['name']);
$file = $_SERVER['DOCUMENT_ROOT']."/textfiles/$filename.txt";
$f = fopen($file, 'w');
fwrite($f, $_POST['text']);
fclose($f);
echo 'Success.';
} else {
echo 'Error.';
}
?>
</html>
I have done the followings:
I went to windows.php.net. Downloaded the last version of PHP.
I extracted the php zip on C drive.
I installed PHP Debug, Intelephence and extension pack.
I configured PHP executablePath. I added the dict "php.executablePath": "C:\\php\\php.exe"
I run the code in my console and it seems to work.
On the browser, instead of executing the code, it simply renders it on the browser. The main point of this file is to create a text file that should be saved on server side using the input sent via POST method.
For the past two days I've been struggling with this issue. Can anybody help me understand why my php script is not ran when I submit the form?
I much appreciate your time!

PHP variables not showing up after submitting form (closed)

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 .

PHP File not printing HTML form content

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

Running .bat script from .php web page

I am attempting to load a webpage on my own server which will run a .bat script (on the same server) as below.
When I access the page, called test.php, it display the 'DO IT!' button and when I press it, it just display the content on the .bat file rather than executing it on the server...
What do I need to configure on the server, I assume in the PHP settings, to force it to run the script rather than just display it on the webpage?
For the purpose of the question, I am happy about the security implications of what I am doing.
I am running a Windows machine with IIS and PHP.
<html>
<head>
<title>Restarting</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
echo exec('c:\scripting.bat');
echo "Done!";
} else {
// display the form
?>
<form action="" method="post">
<input type="submit" name="submit" value="DO IT!">
</form>
<?php
}
?>
</body>
</html>
I think that the echo exec('c:\scripting.bat'); line it's causing you the problem. Try to just execute it without the echo statement.
If you trying to see the output of the function, you must use the second functions parameter: &$output, acording to the documentation itself. See it in the docs here.
I hope it will be useful to you! :D

PHP Script won't load

I'm new to web development and have been wrestling with this problem for several hours now, so I've decided to turn to your wisdom. I'm trying to design a little webpage with a database for my wife to store her recipes in, but I'm having trouble getting form submission to work. Here is the code for the webpage where I take the form information in:
<html><body>
Enter the information below to add a new ingredient for use in your recipes.
<form action="add_to_database.php" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
</body></html>
And here is some silly code I've been trying to display on the page to see if I can even get the form submission to work:
<html><body>
<?php
$name = $_POST['name'];
echo $name."<br />";
?>
</body></html>
Unfortunately, the page comes back as completely back (after I hit the submit button). What's absolutely baffling to me is that I've copied and pasted the examples from this page into some files and everything seems to work fine. So it seems as though apache and php are working correctly, but I'm messing up somewhere along the way. My apologies in advance if this seems like a stupid question but for the life of me I can't figure it out.
Do you name the other file as add_to_database.php where the form is submitted. Instead you can test on teh same page by removing the add_to_database.php from the form action.
form action="" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
and write the php code on the same page as
$name = $_POST['name'];
echo $name;
If this is not working for you. Create a php file named test.php and type phpinfo(); there.
Verify that your page is indeed being processed by PHP - obvious question, but does your PHP file have a .php extension? Do other things like phpinfo() or echo "Test"; work?
Check the error log in /var/log/apache2/error.log or similar (if on Linux, dunno where that'd be on Windows).
Try turning display_errors on in the PHP configuration (this is a good idea only for a development install, not a production server).
a bit of a longshot, but if you can verify that php is pro essing the page. Clean up your html, you are missing the dtd, head, and encoding. . hard to say how a browser is going to interpret a form (missing a name attribute) without these.

Categories