How do I dinamically change the action atribute of an HTML form with php?

Viewed 33

I'm working in an online experiment where I need a subject to listen and rate some audios. Each audio will be evaluated in a different HTML form, and I want to send the results via php email().

I'm having some difficulties because ideally these audios should be presented in a different listening order for each subject. to this purpose I've also programmed a simple php script that randomizes the playing order. This is stored in a php array: $playingOrderArray, from where I shift and obtain one value after another. then, I add the server route as:

$nextPage = "test_".array_shift($playingOrderArray).".php";

I want to input this string into the action attribute from a form HTML tag:

<form action = <nextPage>.php method="post">

But I'm having a hard time finding a way to pass the string to select the direction page. I should add that the whole code is in one file with .php extension.

3 Answers

You can do it in Php like this:

<form action = "<?php echo $pageUrl ?>" method="post">

Or you can use this line inside a JS function:

   $('#formId').attr('action', pageUrl);
<form action = "<?php echo $nextPage ?>" method="post">

You can use this.

<form action = "<?= $pageUrl; ?>" method="post">
Related