Shilpa
Shilpa

Reputation: 98

HTML submit forms using PHP

I am learning PHP now, so pardon my silly question which I am not able to resolve. I have created a simple web form where in I display the values entered by a user.

function submitform()
{
  document.forms["myForm"].submit();
} // in head of html



<form action ="res.php" id="myForm" method="post" >
 Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
 Password:<input type="password" name="password" size="25" maxlength="50" /> 
 Description: <textarea name="editor1"> </textarea> 
<input type="submit" value="Submit" onclick="submitForm()" />
</form>

and res.php contains:

foreach($_POST as $field => $value)
{
  echo "$field = $value";
}

When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?

Upvotes: 1

Views: 5625

Answers (6)

Ivan Mudrik
Ivan Mudrik

Reputation: 21

You can submit an HTML form using PHP with fsubmit library.

Example:

require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];

Upvotes: 0

Damien Pirsy
Damien Pirsy

Reputation: 25425

Put your php code inside php tags!

<?php

foreach($_POST as $field => $value)
{
  echo $field ." = ." $value.'<br />';
}

?>

If you do

<?php
  print_r($_POST);
?>

what do you get? If this still doesn't work, does your server parse php?

Create the file test.php and access it directly http://localhost/test.php or whatever your URL is

<?php
echo 'Hello';
?>

if this doesn't work..it's a whole diferent problem

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174937

There are 2 things you should do now.

  1. Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
  2. Enable error display by using error_reporting(E_ALL).

After you do both things, you should be able to debug and assess the problem much more easily.

Upvotes: 0

fardjad
fardjad

Reputation: 20394

Let's start with fixing errors:

JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.

Upvotes: 1

middus
middus

Reputation: 9121

There's no need for the javascript. This should do:

<form action ="res.php" id="myForm" method="post" >
        Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
        Password:<input type="password" name="password" size="25" maxlength="50" /> 
        Description: <textarea name="editor1"> </textarea> 
        <input type="submit" value="Submit" />
  </form>

Upvotes: 1

kylex
kylex

Reputation: 14406

The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:

Create a test.php file for test purpose:

<?php
if($_POST){
    foreach($_POST as $key=>$value){
        echo "$key: $value<br />";
    }
}else{
    <form action="test.php" method="post">
        <input type="text" value="1" name="name1" />
        <input type="text" value="2" name="name2" />
        <input type="submit" value="submit" name="Submit" />
    </form>
}
?>

If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.

Upvotes: 0

Related Questions