How To Fix The Code To Go For Different Pages According To Radio Button Selected Using Single Form
I want to go to different templates(page) whatever is selected(radio button) from a single form. I want to include just one button in my form. Here as if someone selects template1
Solution 1:
Use JavaScript for this instead. PHP is a server-side language, which means that you have to submit the form to the server before PHP knows which option you selected. JavaScript can deal with this right when you change it instead, as it is a client-side language.
Create an event-listener for a click on the input with the name of template
. Then toggle the action accordingly to that.
$("input[name=template]").on("click", function() {
var action = "";
switch($(this).val()) {
case"1":
action = 'template1.php';
break;
case"2":
action = 'template2.php';
break;
}
$(this).parent("form").prop("action", action);
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><formmethod="POST"action=""enctype="multipart/form-data"><label>First Name: </label><inputtype="text"name="firstname"><label>Last Name: </label><inputtype="text"name="lastname"><inputtype="radio"value="1"name="template">Template 1
<inputtype="radio"value="2"name="template">Template 2
<buttontype="submit"name="upload">POST</button></form>
Post a Comment for "How To Fix The Code To Go For Different Pages According To Radio Button Selected Using Single Form"