CodeIgniter form submit redirects to localhost - php

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.

Related

$_POST request not being handled by the server

I have a form with one text input field:
index.html
<form method="post" action="comment.php">
<a href="javascript:void(0)">
<div class="avatar" style="background:url('img/user4561.jpg') center/cover"></div>
</a>
<input name="comm" type="text"/>
<input type="submit" value="submit"/>
</form>
And here is comment.php in the same directory
<?php
echo $_POST["comm"];
?>
Now the most incredible event. The form data does not get submitted! I get an error:
Notice: Undefined index: comm in 192.168.0.1/comment.php on line 2
I changed comment.php like this:
<?php
if(isset($_POST["comm"])) {
echo "It is set";
} else {
echo "It is not set";
}
?>
and I get:
It is not set
So, I copied the HTML code of the form into another blank webpage. I also changed the action attribute of the form to "192.168.0.1/comment.php". Now I get
It is set
I also tried using GET instead of POST, but I am again in the same conflict.
EDIT: I removed all other files and code, except for the form and its script. The problem persists. You can now read and modify the source code. Go to files.000webhost.com. Use chatstack as the website name and 6RxOkuJ1CIkbNqxKUMGr as the password.
EDIT: I modified the code for the form above to exactly match that in the above given link. I noticed that removing the link from inside the form solves the problem. I have already done this, but this is so weird. According to this post it is totally fine to have other elements inside a <form> element. There are also other parts of my website where I have <div> elements inside a form element and the form is working fine.
What is this? Is it the server or something else?
I am pretty sure the information provided here is not enough. Feel free to ask for any additional information. There is just too much information to write in a post, and I don't know which part of it is relevant.
Open your dev-console in browser.
Go to the network tab. Now you fill in your form and submit. After your comment.php is loaded, you check for the very first row in the network tab (should be the comment.php html document).
When you click on that request it should display you, whether the request was GET or POST and also what data were sent to this document.
You can also try at comment.php var_dump($_REQUEST); to see what data were sent and how it can be accessed.
By this you can see if server all over receives any data. If yes, then you go for an other server like apache2 for testing purpose to look if bug is fixed.
That's how I would to that, but I suspect that by editing your code for the public, you accidentally withheld some information that would solve this simple problem.
It's very unlikely that web-servers like WAMP had passed the testing environment before publishing a new version allthough no data could be procesed by server

How do you make your submit login button take you to your php page?

This is what I am currently doing:
<form method="get" go="welcome.php">
<button type="submit">continue</button>
</form>
But it is just taking me to an error page that says:
Firefox can't find the file at /action_page.php?
Check the file name for capitalization and other typing errors,
Check to see if the file was moved, renamed or deleted.
I even did rename it and I put in my form and it still doesn't go to my php page! Anyone know how to do it better.
Still I am only a starter at code and barely know what I'm doing.
You would use the action attribute.
<form method="get" action="welcome.php">
<button type="submit">continue</button>
</form>

PHP contact form returning the PHP code when I press the submit button

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.

Why is $_SERVER['REQUEST_METHOD'] always GET?

I'm a bit confused about this. I'm hoping it's something wildly obvious I've missed! I have a very simple form:
<form class="form-signin" role="form" name="login" method="POST" action="/page">
<input type="password" name="password" />
<input type="submit" value="Sign in" />
</form>
Note: this page lives at /page and is echoed after the following HTML:
On /page I have this at the very top of the file:
<?php
var_dump($_SERVER['REQUEST_METHOD']);
For some reason, it always shows up as GET when I submit this form. If I take the action="/page" part out then it shows up as POST. What am I missing here?
Note: Even when I load the page, then put at exit after the above var_dump() call, it still shows GET.
In the inspector's timeline I see this for the request:
Thanks to the comments to my question I have found the answer to be in apache configuration. It appears that, because the index.php file is inside a folder called page, apache will automatically redirect to the page with a slash on it. This is the default setting as seen in the Apache directorySlash documentation.
As they warn against turning this off, I will just change the url to what I'm posting. Alternatively, of course, I could add a .htaccess file with proper rewrite rules setup.\
Thanks for everyone's help! As a side note, Safari's inspector left me a little wanting in this case. Chrome turned out to be a far better option for testing.

HTML form action goes to wrong place, doesn't send variables to intended file

so I've been fighting with this for a few days, and I just can't seem to make it work. Whenever I press the submit button, the browser should send the post variables to write.php, but instead, it just redirects back to the website homepage, or the Document Root. This should be really, really simple, and I've done it before, but now it doesn't work for me. What I want to know is if this is a problem with my webserver setup, or PHP, or just a stupid mistake on my part. It's just a simple HTML form, not really special, so here's the form itself, in index.php:
<p style="font-size:13px">
<?php
$rp = fopen('mainlog.txt', 'r');
while(!feof($rp))
{
$read = fgets($rp);
echo($read).('<br/>');
}
fclose($rp);
?>
</p>
<form action="write.php" method="post">
Name: <input type="text" name="user" /><br/>
Changes:<br/>
<textarea cols="70" rows="8" name="change" style="background-color:#555;color:#ccc;font-family:verdana,arial,helvetica,sans-serif;font-size:13px"></textarea><br/>
<input type="submit" value="Add Entry"/>
</form>
And here's where it send to, in write.php:
<?php
$fp = fopen('mainlog.txt', 'a');
$wr1 = $_POST['change'];
#$my_t = getdate(date("g"));
date_default_timezone_set("America/New_York");
$date = date("n").('/').date("d").('/').date("Y").(', ').date("g").(':').date("i").(':').date("s");
$who = $_POST['user'];
$write = $date.(' by ').$who.('
').$wr1.('
');
fwrite($fp, $write);
fclose($fp);
header('Location: http://www.zennixstudios.com/first/chlog/');
?>
I have tried this both on my Apache 2.2 dedicated server with PHP 5 on FreeBSD8.2, and on XAMPP for Windows, with the same results. I have a suspicion that it may have something to do with PHP, specifically PHP include(), because I have several of those on this page, and when I put this on a friend's computer with XAMPP, but without the included files, the include()s just put errors on the screen, but the HTML form suddenly works fine. So, are there any known conflicts with HTML forms and certain PHP functions?
Other Notes:
The code shown above for index.php is within the main page div, but if you want the whole page source just ask.
I'm pretty sure the error isn't in write.php, because I KNOW that the browser never sends anything to it, because it would at least put the date in mainlog.txt.
If you want to see what this looks like in context, go to http://www.zennixstudios.com/first/chlog/
Thanks,
Chaos
Here is your problem:
<table align="right"><tr><td align="right"><form action="/" method="post">Username: <input action="login.php" type="text" name="uname"/><br/>Password: <input type="password" name="passwd"/><br/><input type="submit" value="Login" align="right"/></td></tr></table>
You never closed the form up in your header for the username and password, so your <form action="/" method="post"> is being used for pretty much the entire page and your write.php form action is being ignored because a form is already, technically, open. You'll need to close the form in your header for the rest of your page to work properly.
To reiterate: nothing is being redirected, you're actually posting all the data from both 'forms' to the location / as specified.

Categories