Cant use submit button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cord
Forum Newbie
Posts: 2
Joined: Sun Oct 16, 2011 3:32 pm

Cant use submit button

Post by cord »

I am not able to make the following php code work for me. I can not use a submit button and need to use javascript to submit the form.
Anyone who can help and tell me what is wrong?

<?php
if(isset($_REQUEST['submit'])) {
$content = time();
$fi = "nettotest.txt";
$fh = fopen($fi, 'w') or die("can't open file (48) - dot info");
fwrite($fh, $content);
fclose($fh);
} else {
echo " <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
'http://www.w3.org/TR/html4/strict.dtd'>
<html dir='ltr' xml:lang='en' lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html'; charset='utf8' />
<script type='text/javascript'>
function submitForm() {
document.forms['myForm'].submit();
}
</script>
</head>
<body>";
printf("<form id='myForm' name='myForm' method='POST' action='%s'>", $_SERVER["PHP_SELF"]);
echo "
<table>";
echo "
<tr>
<td colspan='4'>";
$fi = "nettotest.txt";
if (file_exists($fi)) {
echo readfile("nettotest.txt");
}
else {
echo "file do not exist";
}
echo "</td>
</tr>
";
// -----------------------------------
/*
echo "
<tr>
<td colspan='3'>
<a href='javascript: submitForm()'>Search</a>
</td>
</tr>
";
*/
// -----------------------------------
echo "<tr><td colspan='3'>
<input type='button' id='registrer' name='registrer' tabIndex='38' value='submit' onclick='submitForm()' />
";
// -----------------------------------
/*
echo "<tr><td colspan='3'>
<input type='Submit' value='Submit' />
</td></tr> ";
*/
// -----------------------------------
echo "</table>
</form>
</body>";
}
?>
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

Re: Cant use submit button

Post by ouchiko »

They all work fine and your code is posting correctly, however you're not defining the HTML elements [name] correctly so the post action does not get processed.

Code: Select all

if(isset($_REQUEST['submit'])) {
You don't specify any HTML element with the name="submit"

Code: Select all

<input type='Submit' value='Submit' />
The above says make my element an input of type submit with the text in the button of "Submit" - adding name="submit" will solve the problem.
cord
Forum Newbie
Posts: 2
Joined: Sun Oct 16, 2011 3:32 pm

Re: Cant use submit button

Post by cord »

Thanks for the answer, but I can not use a standard submit button as it tis activated in another javascript function when it is not supposed to.
I need to get a javascript to submit the form like:

<a href='javascript: submitForm()'>Submit</a>
OR
<input type='button' id='submit' name='submit' value='submit' onclick='javascript: submitForm()' />

<script type='text/javascript'>
function submitForm() {
document.forms['myForm'].submit('submit');
}
</script>

I do need help here as I am gone blind for the solution here.
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

Re: Cant use submit button

Post by ouchiko »

Code: Select all

function submitForm(){
 document.forms.myform.submit();
}
In <form> </form>

Code: Select all

<input type="hidedn" name="subsmit" value="submit"/>
??
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Cant use submit button

Post by Apollo »

If your form has an input element named 'submit', then your javascript code document.myform.submit() will suddenly refer to the input element, rather than the form's submit() method. And no way to explicitly specify which you mean.

This sort of ambiguity crap is one of the many reasons why I hate javascript :(

Anyway, solution: if your 'submit' input is a button, instead of yourform.submit() use yourform.submit.click();
If 'submit' is something else (e.g. a text input or something), add an invisible dummy button with some unique name or id, and call click() on that.

So if you have a form like this:

Code: Select all

<form name='myform'><input type='text' name='submit'><input type='submit' style='display:none' id='blahh'></form>
Then document.myform.submit() won't work, but document.getElementById('blahh').click() will.
Post Reply