I'm using wamp server for testing my php scripts. I just got an error on one of my recent projects so I created a basic example and found out that it isn't working. So, in my www folder, I have 2 files:
MyApp.html
MyApp.php
MyApp.html body:
<div id="tab">
<form action="MyApp.php" method="post" enctype="multipart/form-data">
<input type="submit" value="Submit" name="submit1">
<br>
</form>
</div>
MyApp.php:
if ($_POST['submit1']) {
$test = 'ThisIsMyApp';
print($test);}
So what I want to do here is to press submit button, and it should print "ThisIsMyApp" from php file. I open .html file on my localhost, and after I click submit button, I get this error:
Not Found
The requested URL /index.php was not found on this server.
How do I fix that?
1st of all make sure both of them are in the same directory.
and do your MyApp.php has the php tags?
<?php ---your code--- ?>
Related
I created a contact form for my website. When I click the submit button, it just shows the PHP code instead of the message it is supposed to show, like the text information the user inputs into the form.
Here is the HTML code for the form:
<html>
<link href="contact.css" rel="stylesheet" type="text/css" />
<head>
<title>Contact Us</title>
</head>
<body>
<form action="contact.php" method="post">
<div id="ContactForm">
<fieldset>
<h4>Contact Parquest:</h4>
<label class="labelone" for="name">Name:</label>
<input name="name"/>
<label for="email">Email: </label>
<input name="email"email"/>
<label for="commens">Comments: </label>
<textarea name="commens"></textarea>
</fieldset>
<fieldset>
<input class="btn" type="submit" value="Send Email"/>
<input class="btn" type="reset" value="Reset Form"/>
</fieldset>
</form>
</div>
</html>
I couldn't post the PHP code, because it just doesn't show when I try, so here's a screenshot:
There is no problem to your code.. The problem is on your environment.. I guess you are not running the html file through a server..
If the url on your browser looks like these:
file:///c:/path/to/your/file/page.html
then you are doing it wrong.. in order to run .php scripts, you need a web server like apache or nginx... the url of should be like these
http://localhost/path/to/file/page.html
then the php file should run as expected..
php files are interpreted scripting language and thus it needs an interpreter in the server in order to run.. if it is just browsed in the browser without a server, it will just output the code inside..
You need a web server to run php scripts.
Try installing wamp and then run your page under localhost.
I have executed the given code. It's working fine on my local server. Conventionally it should print the value inputs posted by form on contact.php.
But the thing I noticed in your PHP code is the variable name is $commets, instead of $comments.
I faced the same problem and when I tried starting my own server it worked.
when you are using Windows, open cmd and type in this;
php -S localhost:4040;
you will see something like this
PHP 8.1.1 Development Server (http://localhost:4040) started
meaning you have started your own server, then you can copy the link found in the parenthesis
so you copy say this "http://localhost:4040" and paste it in your browser then you can now direct it to your html document like this
http://localhost:4040/Desktop/Projects/index.html
now your form should work well with your php script when you submit
i had the same issue, even if appache & mysql were running on my computer.
in the html file, i wrote :
form action="http://localhost/file.php" method="POST"
instead of :
form action="file.php" method="POST"
It seems like connecting HTML forms to php files only works when you download wampserver and turn on all services...
Here's how it worked for me:
You can download wampserver from here: http://www.wampserver.com/en/ (follow instructions)
I saved it to the C: drive on my computer.
Next, you can create a php file and an html file both in the 'www' folder of the wamp folder in whatever drive you put it in. Copy the form code into the HTML file, and the php code into the php file.
Finally, go to 'localhost/yourhtmlname.html' and fill out the form. It should work as normal.
When I submit my form, I get sent to the correct file as specified in the action attribute of my form, but the PHP in the file isn't printing the variables at all... I've combed through posts of other people having the same issue, but none of their solutions fix my problem. I've stripped my code down to a simple, textbox, button, and a php file that's supposed to print the textbox value.
If it matters, I'm running this locally in chrome, not using any servers or websites yet, I'd like to get my code working locally before I upload to my server.
HTML
<html>
<form action="Submit.php" method="post">
<input type="text" placeholder="First Name" name="firstName" id="firstName" required>
</br></br>
<input type="submit" name="submitted" value="Submit">
</form>
</html>
PHP
<html>
<body>
Name <?php echo $_POST["firstName"]; ?><br>
</body>
</html>
All I get when I click the button is a white page with "Name" printed.
Thanks!
Running scripts in response to HTTP requests is something that's done by the webserver. If you just use local files, the script will simply be loaded into the browser as a text or HTML file, it won't be executed. You can't do form processing like this.
You need to run a local server, then access the form as http://localhost/form.html
here is my path
main:
Members/name.html
search.php
Members is a folder
my html code:
in name.html
<form action="../search.php" method="get">
<div>
<br/>
<input type="text" name="input" value="Search the
site…" onfocus="this.value=(this.value=='Search the site…')?
'' : this.value ;" />
<input type="submit" name="go" id="go" value="GO" />
</div>
but when i ran this and put something in the search bar it didn't work.
i read this
Using HTML form action with a php script being in another directory (relative paths)
but it still doesn't work
when i put the html file in the same directory as the search.php it's working great (i'm changing the path to ./search.php when it's in the same directory)
but when i'm putting it back to the another directory it's not working again.
There are multiple ways. Let's assume you are at domein.ext /dir1/dir2/
If you want to post to the exact current url (if you have nice urls, this occurs):
<form> <!-- No target, only in HTML5 -->
If you have page.php in the same dir:
<form action="page.php">
If you are in dir2, and want a file from dir1 (so up 1 dir):
<form action="../page.php"> <!-- ../ means 'up one dir' -->
If you are in dir1, and need something in dir2:
<form action="./dir2/page.php"> <!-- ./ means 'current dir' -->
If you want the location to the resource to be checked from the domain, you start with slash:
<form action="/whole/different/dirs/page.php"> <!-- / means 'document root' -->
Handy to know, ./ and ../ are very common, they work in php includes, resources in webpages, commandline, etc etc.
Please note, the / in php doesnt mean 'document root', in php it will link to /home (on a LINUX server). Thats why you have $_SERVER['DOCUMENT_ROOT']
i found the solution to the problem:
<form action=".././search.php" method="get">
i use ../ to go back in folders like "martijn" said
and ./ for the php use
i am wrinting this code below in notepad and i saved in .html extent ,
i want to design a simple comment system with a database (which is notepad ) to save the comments , but when i click on the submit button it apears there is error in an specific command which is $_SERVER['PHP_SELF'] . can you help me ?
<html>
<body>
<div class ="forumform">
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
name: <input type=text name="name" id="name">
comment: <input type=text name="commentcontent" id="commentcontent">
<button type="submit" value="submit" name="submit" class="btn">submit</button>
</form>
</body>
</html>
Did you say: "i am wrinting this code below in notepad and i saved in .html extent"?
PHP commands/scripts like the <?=$_SERVER['PHP_SELF']?>, only run in files with.php ententions only!! not on HTML.
YOU should change the extension of your file, with the above script to .php
you are using the php echo shortcut make sure it is enabled in your php.ini file serach for this short_open_tag and enable it or simply use <?php echo .... ?> instead of <?= ... ?>
I have a CodeIgniter project with a very simple test application.
Controller opens a view (PHP page) which has a text box and a submit button.
When I press the submit button, instead of redirecting to the appropriate function call in the controller, I get redirected to localhost/xampp.
here's the code on my view, which SHOULD be redirecting to the save_genre function in the controller named "welcome".
<?php echo form_open('welcome/save_genre');?>
<label for="radio_genre">Radio Genre</label>
<input type="text" name="radio_genre" id="radio_genre"></input>
<?php echo form_submit('submit','Save'); ?>
</form>
any ideas what could be wrong? I think it's not the code but is a setting or file wrong somewhere, but i don't know where to start looking.
EDIT:
I had already redefined the base URL in the config file.
I don't think I rewrote the .htaccess - but I'll certainly check. (This is a team project setup and I'll make sure no one else has done that.)
As requested, below is the HTML output by the form. The URL link for the form seems very odd to me because it doesn't mention the project name like I would expect. So there are two places for me to look now. Thanks!
<body>
<h1>Welcome!</h1>
<form action="http://localhost/index.php/welcome/save_genre" method="post">
<label for="radio_genre">Radio Genre</label>
<input type="text" name="radio_genre" id="radio_genre"></input>
<input type="submit" name="submit" value="Save" />
</form>
</body>
EDIT: OK - I recreated the project myself and then brought my PHP files in and it works fine. So the person who created the project did something odd.
You need to edit $config['base_url'] in /system/application/config/config.php.
Somehow, while the index.php file was there in the file system, it wasn't being recognized as part of the project. Once I copied a new index.php file in there (identical file but it trigger the sense that "this changed.), I then went back into my project and refreshed and saved.
Everything then worked.