Reputation: 81
Ok, I know this is probably a really dumb question but I can't figure out what is going on here. I've got this in my original html file-
<form action="./api" method="POST">
<label for="first-name">First Name: </label>
<input id="first-name" type="text" name="first_name" /><br/>
<label for="last-name">Last Name: </label>
<input id="last-name" type="text" name="last_name" /><br/>
<input type="submit" name="send" value="Submit Form"/>
</form>
In my api folder I have an index.php file that has the following code-
<?php
print_r($_POST);
?>
Thats it- I know its not safe but I was just trying to get it to work. It returns an empty array...
Array ()
I really need a second pair of eyes on this, what am I missing? why isn't the information being transmitted?
Update:
changing the action to action="./api/index.php" returns the posted variables as expected. As someone commented below, I was hitting the index.php in the api folder before because it was returning the empty array. Its strange that it didn't accept the data. Is it common practice in php then to make the target of your post explicit?
Upvotes: 0
Views: 77
Reputation: 9885
Please add the full file path to your action and convert your
$_POST['']
to variables. The code below should work perfectly based on your question.
<form action="../api/index.php" method="POST">
<label for="first-name">First Name: </label>
<input id="first-name" type="text" name="first_name" /><br/>
<label for="last-name">Last Name: </label>
<input id="last-name" type="text" name="last_name" /><br/>
<input type="submit" name="send" value="Submit Form"/>
</form>
Add this to ../api/index.php
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
echo $first_name;
echo $last_name;
?>
Upvotes: 1