tags:

views:

88

answers:

7

Is this right, because nothing happens when I hit the submit button:

<HTML>
<BODY>
<?php

$ok_button="<input type='submit' OnClick='document.forms['myForm'].submit();' value='approve!';"
echo $ok_button;
$form="<form name='myForm' action='myPhp.php'><input type='hidden' name='firstname' value='<?php echo $firstname;?>'></form>;"
?>
!!!ALOT OF CONTENT HERE!!!

<?php echo $form;?>
</BODY>
</HTML>

Please help me make this work! Thanks

UPDATE: OK, so the above has errors...

Another question pops up though...

Im trying to approve a form submitted from another page... But I dont want to insert anything into my mysql database, since the user might NOT approve the form. So the values I have in this php file is received from a main-form, and in that main form, users can choose categories! This means I have to create alot of 'if' statements if I want an approve form, right?

Example to clarify:

User submits main form with the value 'CARS' chosen from a drop down list. The action is myPhp.php.

In MyPhp.php the approve page exists. Here I want the user to approve the previous form. Then hit enter, and the data is put into the database. Problem is, I dont know what the user chose in that drop list, so I would have to make alot of 'if' statements.

Then I would have to write a form with php, which you can see im trying, and then submit the form with the right values...

Any other simpler way :) ???

A: 

What is not going to work is the echo $firstname thing - PHP in strings won't be interpreted when echoed. You will have to insert that dynamically after $firstname has been defined.

Pekka
+1  A: 

You have several improperly balanced quotes in your HTML output. Specifically in your onclick attribute. Fixing those should take care of the problem.

You'll also need to construct your PHP strings differently, since strings in single quotes are considered literal values and will not be expanded by the processor.

jheddings
A: 

<input type="submit"> Don't need to have an onclick, it will submit the form when you click on it.

Marius
+2  A: 

Yikes, I don't know where to start. You've got a lot of syntax errors... Try this:

<html>
    <body>
    <?php
    $ok_button ='<input type="submit" value="approve!" />';
    $form = '<form name="myForm" action="myPhp.php">'
        . '<input type="hidden" name="firstname" value="'. $firstname . '">'
        . $ok_button
        . '</form>';
    ?>
    !!!ALOT OF CONTENT HERE!!!
    <?php echo $form; ?>
    </body>
</html>

There's no need to rely on javascript to post the form... just throw the submit button inside the <form>.

p.s. Please use lowercased tags and attributes... uppercase is so 1995.

brianreavis
I think the main problem in the script was the misplaced quotes... The button can exist outside the form, but this approach is much easier to follow. +1
jheddings
please read my update also and help me out!
Camran
Sorry, not getting your question. Not to be an ass or anything, but based on your other questions on SO, I'm thinking it might be a good idea for you to just play around with it for awhile and google around... before impulsively employing everyone here.
brianreavis
A: 

Your OK button does not seem to be inside the form.

Try this:

   $form="<form name='myForm' action='myPhp.php'><input type='hidden' name='firstname' value='<?php echo $firstname;?>'><input type='submit' OnClick='document.forms['myForm'].submit();' value='approve!'/></form>;"
Vincent Ramdhanie
+1  A: 

It is possible to create a form like that using php. BUT it's not recommended at all. You have errors in your quoting of things that come from trying ot output html like that with php.

But if you must output the form like that try this. I corrected the quote errors.

<HTML>
<BODY>
<?php

$ok_button="<input type=\"submit\" OnClick=\"document.forms['myForm'].submit();\" value=\"approve!\">";
echo $ok_button;
$form="<form name=\"myForm\" action=\"myPhp.php\"><input type=\"hidden\" name=\"firstname\" value=\"<?php echo $firstname;?>\"></form>";
?>


<?php echo $form;?>
</BODY>
</HTML>
Galen
Eeek, just wrap in single quotes to get away from all that escaping. Also: `value=\"<?php echo $firstname;?>\"` needs to be fixed...
brianreavis
Then you have issues with the javascript '. Either way i just fixed his code quickly so it would work. I would NEVER do it this way myself.
Galen
+1  A: 

Assuming you're using PHP5, we can clean this up a lot

<?php

$ok_button="<input type='submit' OnClick='document.forms[\'myForm\'].submit();' value='approve!'>"

$form = <<<TEMPLATE
    <form name='myForm' action='myPhp.php'>
        <input type='hidden' name='firstname' value='{$firstname}'>
    </form>
TEMPLATE;

echo <<<TEMPLATE
<HTML>
    <BODY>            

        !!!ALOT OF CONTENT HERE!!!

        {$form}
        {$ok_button}

    </BODY>
</HTML>
TEMPLATE;

?>
Zurahn