LoneStarJack Posted September 18, 2011 Share Posted September 18, 2011 I have several radio buttons (dynamically generated) and I want to use the selected value in a MySql query WHERE clause. Is it possible? Quote Link to comment https://forums.phpfreaks.com/topic/247387-using-radio-buttons-values-in-where-clause-in-query/ Share on other sites More sharing options...
xyph Posted September 18, 2011 Share Posted September 18, 2011 Of course it's possible. You'll want to make sure the user hasn't designed their own form to submit a value you might not want them to. You could use an array filled with the values you have in the radio buttons, and check against that. <?php $radioValues = array( 'option1', 'option2', 'option3' ); if( !in_array($_POST['radioButton'], $radioValues) ) { // user has entered a bad value } else { $query = 'SELECT `columns` FROM `table` WHERE `column` = "' . $_POST['radioButton'] . '"'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247387-using-radio-buttons-values-in-where-clause-in-query/#findComment-1270449 Share on other sites More sharing options...
LoneStarJack Posted September 18, 2011 Author Share Posted September 18, 2011 I forgot to mention that I do not want to reload the page or have two pages to do this. Quote Link to comment https://forums.phpfreaks.com/topic/247387-using-radio-buttons-values-in-where-clause-in-query/#findComment-1270453 Share on other sites More sharing options...
Pandemikk Posted September 18, 2011 Share Posted September 18, 2011 I forgot to mention that I do not want to reload the page or have two pages to do this. If you want users to submit forms that don't refresh the page you'll need to use AJAX. Let's just concentrate getting your query working without AJAX first, since this is not the AJAX help forum. Quote Link to comment https://forums.phpfreaks.com/topic/247387-using-radio-buttons-values-in-where-clause-in-query/#findComment-1270454 Share on other sites More sharing options...
xyph Posted September 18, 2011 Share Posted September 18, 2011 You could use the query string to let the script know it's an AJAX call, but I really don't see the point. I find it nicer to separate the logic in to different files rather than have a single cluttered file that handles everything. That doesn't mean my solution isn't valid, so I'm not sure what you meant by your second post. Did you want coded examples? Because there's tons of these on the net already, just use google. If you want some help with your code, please post it so we can recommend changes or suggest more efficient ways to accomplish it. Quote Link to comment https://forums.phpfreaks.com/topic/247387-using-radio-buttons-values-in-where-clause-in-query/#findComment-1270458 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.