1

I am passing an array of data from a select like so:

<div style="margin:10px;">
<select name="selectlist[]" multiple style="min-width: 200px;" id="selectlist">
  <option value="userid">User ID</option>
  <option value="username">User Name</option>
</select></div>
<div><input type="submit" value="Submit" id="btnYes" onclick="Scriptit()"></div>
<div id="displaypart"></div>
<script>
function Scriptit(){
var selectlist = $('#selectlist').val();
jQuery.ajax({ 
url: "<?php echo JURI::base();?>Terminator.php",
type: 'POST',
dataType: "html",
data: { selectlist: selectlist},
success : function(result) {
 $('#displaypart').empty();
$('#displaypart').append(result);
} ,
error: function(){}});}
</script>

Now on my page Terminator.php how do I check what elements were selected (I.E. in the array) so I know how to build my query string. Something like this (pseudocdoe not php)

$selectlist = implode(',',$_REQUEST['selectlist']);

$sql = "Select";
if $selectlist.contains("userid") then $sql = .$sql & a.userid;
if $selectlist.contains("username") then $sql = .$sql & b.username;

if $selectlist.contains("userid") then $sql .$sql & from useridtable a;
if $selectlist.contains("username") then $sql = .$sql & inner join usernametable b on a.usralra = b.usralra;

1 Answer 1

1

You do not have to implode the array and then search in a string. Just search into the array you have posted.

Get the data as $_POST['selectdata'].

Then search in array

if(in_array('username', $selectdata)) $sql ='';
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent advice! What is the proper syntax to combine the multiple $sql variables I will be creating?
If you have the sting in order you can use $sql .= 'query string'
@Pratyush, I know, but that's not the question.
@SimoneCabrino Yepp. I should have tagged the asker that his code is prone to sql injection. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.