Pawan
Pawan

Reputation: 9

Redirect to a page according to selected option from radio button through 'onclick' , without 'submit' the form in php

This is the code which is not working according to my requirement

<head>

<script type="text/javascript">

function test(){

if(document.form.choice.value='pc'){

window.open('pc.php','_self');

return true;

}

else if(document.form.choice.value='ps2'){

window.open('ps2.php','_self');

return true;

}

else if(document.form.choice.value='ps3'){

window.open('ps3.php','_self');

return true;

}

else if(document.form.choice.value='psp'){

window.open('psp.php','_self');

return true;

}

}

</script>

</head>

<body>

Games<br /><br />

<form name="form">ALL &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type="radio" onClick="test()" name="choice" value="pc">PC &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" onClick="test()" name="choice" value="ps2">PS2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" onClick="test()" name="choice" value="ps3">PS3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" onClick="test()" name="choice" value="psp">PSP &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</form>

</body>

Also i want to know that how to display the field automatically just by selecting the selected field in Drop down list without 'submit'

<select name="genre">
        <option value="">--Select Genre and Click Go--</option>
        <option value="All">All</option>
        <option value="Action / Mission">Action / Mission</option>
        <option value="Arcade">Arcade</option>
        <option value="Racing">Racing</option>
        <option value="Sports">Sports</option>
        <option value="Kids">Kids</option>
        <option value="Strategy">Strategy</option>
        <option value="Adventure">Adventure</option>
      </select>

Upvotes: 0

Views: 15292

Answers (2)

Perlica
Perlica

Reputation: 62

Try this...

EDITED:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
<script type="text/javascript">
 function idForm(){
   var selectvalue = $('input[name=choice]:checked', '#idForm').val();


if(selectvalue == "pc"){
window.open('http://www.google.com','_self');
return true;
}
else if(selectvalue == "ps2"){
window.open('http://www.google.com','_self');
return true;
}else if(selectvalue == 'ps3'){
window.open('http://www.google.com','_self');
return true;
}else if(selectvalue == 'psp'){
window.open('http://www.google.com','_self');
return true;
}
return false;
};


</script>
</head>
<body>
Games<br /><br />
<form id="idForm">ALL &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" onclick="idForm()"  name="choice" value="pc"/>PC &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio"  onclick="idForm()" name="choice" value="ps2"/>PS2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio"  onclick="idForm()" name="choice" value="ps3"/>PS3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio"  onclick="idForm()" name="choice" value="psp"/>PSP &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</form>
</body>

Jsfiddle link

Upvotes: 1

shamon shamsudeen
shamon shamsudeen

Reputation: 5858

You can get the value of the option by adding onchange event in the <select> tag.

<select name="genre" onchange="selectOption()">
            <option value="">--Select Genre and Click Go--</option>
            <option value="All">All</option>
            <option value="Action / Mission">Action / Mission</option>
            <option value="Arcade">Arcade</option>
            <option value="Racing">Racing</option>
            <option value="Sports">Sports</option>
            <option value="Kids">Kids</option>
            <option value="Strategy">Strategy</option>
            <option value="Adventure">Adventure</option>
          </select>

In your script add this

function selectOption(){
//each option value
var option=document.form.genre.value;
alert(option);


}

For redirecting you can use window.location in your test() method use this window.location=yourpage.php instead of this window.open('pc.php','_self')

Upvotes: 0

Related Questions