1

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?

7
  • You don't even see an equals sign? If so, your loop is never being ran. Are you sure your post method is actually working? You can verify with Fiddler (fiddler2.com/fiddler2). Commented Dec 1, 2011 at 20:11
  • 2
    for a start remove the javascript, you don't need it to submit a form. And as its not adding anything its more likely to cause a problem than help. Commented Dec 1, 2011 at 20:13
  • 1
    Have you tried without the (unnecessary) javascript submitForm() function? In the code you present it serves no purpose. Try removing the "onClick" attribute from the submit button first as a submit button will simply submit the form without the need for javascript. Commented Dec 1, 2011 at 20:13
  • 3
    Sounds like you're getting an error somewhere in res.php. Do you have display_errors set to On in your php.ini? Also, did you put <?php and ?> in your script correctly? Commented Dec 1, 2011 at 20:14
  • I believe that your form has to have name="myForm" for that javascript to work correctly Commented Dec 1, 2011 at 20:18

6 Answers 6

1

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> 
Sign up to request clarification or add additional context in comments.

9 Comments

removed the func, but still no luck.. It now displays the contents of my php file when i click on submit. In response file "; foreach ($_POST as $field => $value) { echo "$field == $value \n"; } ?>
Uhm...@Shilpa does your server even recognize php? Is it inside <?php ?> tags?
ya, i am using Netbeans (PHP enabled). <?php echo "<p>In response file!</p>"; foreach ($_POST as $field => $value) { echo "$field = $value<br />\n"; } ?>
@Shilpa see my answer and tell me if that works (first snippet)
Created a new php proj with <?php echo 'Hello'; ?> and ran it. It shows Hello. But if i add the sme thing in my existing HTML proj, it doesnt show anything. If i add <?php echo '<p>Hello</p>'; ?> in my exisitng HTML proj, then after submit it shows " Hello '; ?>"
|
1

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.

2 Comments

Good call. Though the robust solution would be to get rid of the JavaScript altogether.
Yes, generally JavaScript is not needed for submitting a form, but I assume that the OP wants to do some sort of operations (which is not posted in the question) with that JavaScript before form submission.
0

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.

11 Comments

Tried this, but still not wrking..When I click on submit, it gives HTTP Status 404 error
So you created a new test.php file, and when you submit the file it goes to test.php and NOTHING appears at all? It sounds like it's submitting to the wrong page. Check your URL, is it actually going to the correct test.php page?
ya i created a new test.php and changed the name in form action to test.php. AFter submit it shows 404 err. url is localhost:8080/sampleproj/test.php- so its accessing the right file
A 404 means the page is not being found. That's not a code error, it's a server error. The file it's accessing is not being found. It's basically saying "the test.php file does not exist." Frankly that's impossible. My guess is that after you submit the URL changes to localhost:8080/test.php, which is incorrect.
I have my test.php(with the above code) in sample proj. After clicking on submit, the form is redirected to test.php(form action = test.php) So its accessing localhost:8080/sampleproj/test.php
|
0

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.

1 Comment

I added echo error_reporting(E_ALL); in php file.It still shows a blank page after submit. So do i need to check the errors in err log or will htey be displayed in the webpage
0

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

2 Comments

@Shilpa try print_r($_POST) or var_dump($_POST) and tell the results (see my update answer)
I tried both print and var_dump, still blank page after submit.
0

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']; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.