Get Selected Option in PHP Without Pressing Submit

Getting selected option value without postback/submit PHP

you need to use onchange event

$('select').on('change', function() {
var value= this.value ;
$.ajax({

url:'action.php',
type:'POST',
data: {
'value':value
},

success: function(data) {
alert(data);
}
});
})

And make file action.php

if(isset($_POST['value'])) {
$username = $_POST['value'];
//your database query
$query=something;
if ($query) {
echo 'success';
}
else {
echo 'something went wrong';
}
}

Get value without submitting it

See this fiddle

You can use Javascript to do this. See the below JS

function addValues() {
var a = 0,
b = 0;
var e = document.getElementById("radio");
a = e.options[e.selectedIndex].value;
var e1 = document.getElementById("radio1");
b = e1.options[e1.selectedIndex].value;
alert(parseFloat(a) + parseFloat(b));
}

Add the above JS inside a <script> before you close the <body>.

The function will be invoked by clicking a button. The HTML for button would be as follows

<button onclick="addValues()">Click to add</button>

Update

See the fiddle


Updated HTML

<ul>
<li>
<select name="radio" id="radio">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</li>
<li>
<select name="radio1" id="radio1">
<option value="1.2">1.2</option>
<option value="3.1">3.1</option>
<option value="0.3">0.3</option>
<option value="1.2">1.2</option>
</select>
</li>
</ul>
<button onclick="addValues()">Click to add</button>
<br/> Result=
<span id="result"></span>

Updated JS

function addValues() {
var a = 0,
b = 0;
var e = document.getElementById("radio");
a = e.options[e.selectedIndex].value;
var e1 = document.getElementById("radio1");
b = e1.options[e1.selectedIndex].value;
document.getElementById("result").innerHTML = (parseFloat(a) + parseFloat(b));
}

Update

If you don't want the button, you can just listen for the change event of the select.

See this fiddle

How to submit a selected value without a submit button?

You can call function inside the select dropdown.

 <select id="sel_id" name="sel_name"  onchange="this.form.submit();">
<option value="-1">Select</option>
<option value="6">kasper </option>
<option value="13">adad </option>
<option value="14">3204 </option>
</select>


Related Topics



Leave a reply



Submit