Jump to content

[SOLVED] Automatic form submit not working.


env3rt

Recommended Posts

The problem is that when I have javascript automatically submit the form, the php doesn't work, but when I press the submit button it works perfectly. Does anyone know what I am doing wrong?

Code is below.

 


<?php

echo "
<head>
<script>
function yes(){
document.sform.submit()
}
</script>
</head>

<body onKeypress='yes()'>
<form method='post' action='?' name='sform' id='sform'>
<input type='text' id='this' name='this'>
<input type='submit' id='subform' name='subform' value='subform'>
</form>
</body>
";

if ($_POST['subform']){
$page = "expage.html";
$fh = fopen($page, 'w');
$example = $_POST['this'];
fwrite($fh, $example);
fclose($fh);
echo "<script>alert('form submitted')</script>";
}
?>

 

Please help if you can.

Link to comment
https://forums.phpfreaks.com/topic/74080-solved-automatic-form-submit-not-working/
Share on other sites

Your id of your submit button need's to be the same as what you are trying to grab through javascript.

It seems to me you are trying to make something simply and generally easy to do into over convoluted code, and

a very roundabout way.

Simply have them submit the form, then do the file saving stuff right after that.

I could make them click the button but I want it to submit even if they don't click it, and the submit fuction with javascript doesnt work

Well it does work but not in the way you are expecting it to work. Because you are submitting the form when a user enters something into the textbox, and not when the user click the submit button. The submit button is not be sent in the http request ($_POST['subform']).

 

Submit buttons will only be sent in the http request when they are clicked on.

Its the PHP code thats the problem, you'll want to change your if statement:

if ($_POST['subform']){

 

to something else. Prehaps:

if ($_POST['this']){

 

$_POST['this'] will refer to your textarea, as you named it this

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.