Reputation: 668
I have and issue submitting my form. I've tried both with ajax and with a regular submit($.post()
, $.get()
, method="post"
, method="get"
).
When I use either get I can get the data fine with $_GET
, but using either post doesn't populate the $_POST
variable.
Any suggestions would be great. I need to get post working, because I don't like the idea of using get for things like passwords.
EDIT:
POST:
<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<input type="text" name="some_input" value="">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST["some_input"])) echo $_POST["some_input"];
?>
Or
GET:
<form method="get" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<input type="text" name="some_input" value="">
<input type="submit" name="submit">
</form>
<?php
if (isset($_GET["some_input"])) echo $_GET["some_input"];
?>
The first one doesn't show anything.
The second shows whatever I typed.
EDIT 2:
The following code work and populates my $_POST
<?php
print "CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "<BR />";
$data = file_get_contents('php://input');
print "DATA: <pre>";
var_dump($data);
var_dump($_POST);
var_dump($_SERVER["PHP_SELF"]);
print "</pre>";
?>
<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<input type="text" name="name" value="ok" />
<input type="submit" name="submit" value="submit" />
</form>
Result:
CONTENT_TYPE: application/x-www-form-urlencoded
DATA:
string(21) "name=ok&submit=submit"
array(2) {
["name"]=>
string(2) "ok"
["submit"]=>
string(6) "submit"
}
string(37) "/path/to/test-post.php"
The strange part is that action="<?php $_SERVER["PHP_SELF"]; ?>"
or no action at all works just fine, but either action="test-post.php"
or action="/path/to/test-post.php"
doesn't.
Broken result:
CONTENT_TYPE:
DATA:
string(0) ""
array(0) {
}
string(37) "/path/to/test-post.php"
Upvotes: 0
Views: 2674
Reputation: 668
I'm still not too sure what was causing it in the first place, but I have discovered that if I am making a request with a relative path then I have to omit the file extension. e.g. action="test-post"
instead of action="test-post.php"
.
File extensions seem to be fine for $_GET
and $_REQUEST
if I am sending it via get (method="get"
), but not for $_POST
or $_REQUEST
if I am sending it via post (method="post"
).
EDIT
This was something to do with the .htaccess
file. I don't know much about that file, so I don't know exactly which bit was causing this issue.
Upvotes: 2
Reputation: 74
Here is an example of submitting with POST threw AJAX :
HTML:
<form id="form" name="form">
<h3>Fill Your Information!</h3>
<div>
<label>Name :</label>
<input id="name" type="text">
<label>Email :</label>
<input id="email" type="text">
<label>Password :</label>
<input id="password" type="password">
<label>Contact No :</label>
<input id="contact" type="text">
<input id="submit" onclick="myFunction()" type="button" value="Submit">
</div>
</form>
Javascript :
jQuery(document).ready(function(){
$('#form').submit(function(e){
var emailVal = $('#email').val();
e.preventDefault();
$.ajax({
type: "POST",
url: "thePHPfile.php",
data: 'data': emailVal ,
success: function(html) {
alert(html);
}
});
});
});
PHP :
<?php
if(isset($_POST['data']){
echo $_POST['data'];
}
?>
Upvotes: 0
Reputation: 3679
This is an example of how to make a form with post, and handle it. Notice you can leave the action blank (then it stays on the same URL).
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST["some_input"])) {
echo $_POST["some_input"];
}
}
?>
<form method="post" action="">
<input type="text" name="some_input" value="">
<input type="submit" name="submit">
</form>
Upvotes: 0