Reputation: 237
Here is the code I am working on
<html>
<script>
var submit = document.getElementById('btnFormSubmit');
submit.addEventListener('click', submitForm);
function submitForm(event){
event.preventDefault();
event.stopPropagation();
var href = '',
inputs = this.parentNode.getElementsByTagName('input')
for(var i=0;i<inputs.length;i++){
if(inputs[i].getAttribute('name') == 'scenarionum' && inputs[i].checked){
href = inputs[i].getAttribute('data-href');
value= inputs[i].getAttribute('value');
window.location = href;
}
}
}
</script>
<head>
<title>Hello</title>
</head>
<body>
<form method="post" name="start" action="./test.pl">
<div class="Folder">
<input type="radio" name="scenarionum" value="Run Suite" data-href="test.pl"/>Suite</div>
<input type="submit" value="Run" id="btnFormSubmit" />
</form>
</body>
</html>
this is working fine. When I am clicking the Submit button its taking me to the page "test.pl" but I want to redirect it along with the value of the radio button (which is "Run Suite" in this case")
please tell me the modifications in the javascript to redirect to test.pl along with the value "Run Suite"
Upvotes: 0
Views: 80
Reputation: 129
SEND GET
You can just add it to the URL like this:
window.location = href+"?value="+value;
SEND POST
If you want the form to send a POST you can do the following:
if(inputs[i].getAttribute('name') == 'scenarionum' && inputs[i].checked){
document.getElementById("myForm").action = inputs[i].getAttribute('data-href');
document.getElementById("myForm").submit();
}
You also have to give your form an id, so it can be found with getElementByID, or you can use
document.getElementsByName("start")[0].
Upvotes: 0
Reputation: 102
Change your submit input with the following. it works, just tested.
<input type="submit" value="Run" id="btnFormSubmit" onclick="document.forms['start'].action=document.getElementById('scenarionum').checked ? './test.pl?scenarionum=' + document.getElementById('scenarionum').value : './test.pl'" />
Upvotes: 0
Reputation: 4320
delete all the JavaScript and the form will work as expected. HTML Forms already know how to submit values to a script.
Upvotes: 1
Reputation: 94499
Add the value to the url as a request param:
if(inputs[i].getAttribute('name') == 'scenarionum' && inputs[i].checked){
href = inputs[i].getAttribute('data-href');
value= inputs[i].getAttribute('value');
window.location = href + "?scenarionum=" + value;
}
Upvotes: 0