Link to home
Start Free TrialLog in
Avatar of Simon Leung
Simon Leung

asked on

PHP button action

There are two buttons in PHP. If user click button 1, it will go to action1.php. If user click button2, it will go to action2.php

Any idea how to code it ?

Thx
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Not enough information.

Please share some code - are you talking about a form or links here?
you can simply use JavaScript to achieve that, like:

<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
<button type="button" onclick="doPost(this)">action1.php</button>
<button type="button" onclick="doPost(this)">action2.php</button>
</form>

</body>
</html>

<script language="javascript">
	function doPost(c) {
    	alert(c.textContent);
    	c.form.action = c.textContent;
        c.form.submit();
    }
</script>

Open in new window

Avatar of Simon Leung
Simon Leung

ASKER

can it be handled simply with HTML and php ?

Thx again.
try something like this:

<?php

if (isset($_POST['action'])) {
    echo $_POST['action'];
    header("Location: ".$_POST['action']);
}
?>

<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
    <button type="submit" name="action" value="action1.php">Submit to action1.php</button>
    <button type="submit" name="action" value="action2.php">Submit to action2.php</button>
</form>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx