Wanda Embar
Wanda Embar

Reputation: 35

Dynamically changing form action

I'm trying to change the form action on the fly, but think I'm having a syntax problem, that I just can't figure out.

This works:

<?php
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action='linktogoto.php'\">";
?>

This doesn't work:

<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname' 
value='$buttonvalue' onclick=\"this.form.action=$variable\">";
?>

I need to eventually change $variable depending on the situation, so need to get this to work.

Upvotes: 1

Views: 105

Answers (1)

Matt C
Matt C

Reputation: 4555

<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname' 
value='$buttonvalue' onclick=\"this.form.action=$variable\">";
?>

Should be:

<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname' 
value='$buttonvalue' onclick=\"this.form.action='$variable'\">";
?>

Notice the ' around $variable.

Upvotes: 1

Related Questions