Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Probably a simple solution here, I'm confusing myself. I've got a <select> that populates via an array from a MySQL database of users. No problem there. I've also got a script that when a certain user is selected, I can via AJAX the user's user_id. My problem, is getting that user ID into a variable $assigned_to, to include in a URL.
I'm sorry the coding is so specific. If requested, I can break it all down into a much more simplified version. Here's what I've got so far:

<script type="text/javascript">
function showData(str)
{
if (str=="")
  {
  document.getElementById("showData").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("showData").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getdata.php?assign_to="+str,true);
xmlhttp.send();
}
</script>

//
//This gets the user_id value from the `<select>` when a user selects it.
//

<?php

//
//Here is the function that displays my users:
//

function userlist()
{               
echo "<select name=\"assigned_to\" onChange=\"showData(this.value)\">";
echo "<option default value=\"\">Assignee</option>";

$mysqli = new mysqli("xxx", "xxx", "xxx", "xxx");
$userlist = "SELECT * FROM users where user_level = 2";
$result = $mysqli->query($userlist);

while( $row = $result->fetch_assoc() ) {
echo "<option value=\"".$row['user_id']."\">".$row['first_name']." ".$row['last_name']."</option>";
}
echo "</select>&nbsp;";
}
echo "<p>";
echo userlist();

echo "<a id=\"show\" href=\"index.php?page=overview&location=advance&assignee=\"".$assign_to."\">Assign</a></p>";
?>
<!-- The line below **will** display the correct user_id value when selected -->
<div id="showData">User Id Shows Here</div>

That last URL variable $assign_to is what I want the value from $row['user_id'] when selected to return.

I understand the ideal method of going about all of this is to just post a form to a php page and using _POST to retrieve all these variables. Unfortunately in this circumstance I need the variable in the URL.

Thanks as always.

share|improve this question
what you are doing already will send the variable in the url. You can access it with $_GET["assign_to"] – Kevin B Feb 20 at 20:19
1  
try jquery and ajax, it's a lot easier! – jcho360 Feb 20 at 20:21
That doesn't seem to work on the current page. It will of course work in getdata.php. – Kevin O'Brien Feb 20 at 20:23
I'm briefly familiar with jquery and AJAX, it's possible to update automatically on select of the <select>? – Kevin O'Brien Feb 20 at 20:25
add comment (requires an account with 50 reputation)

1 Answer

up vote 0 down vote accepted

what you are doing here should already do what you want it to do. when the select-box is changed, your showData-function is called with the current selected value of the select-box:

<select name=\"assigned_to\" onChange=\"showData(this.value)\">

in the showData-function the retrieved argument 'str' contains this value, and you append it to the url:

xmlhttp.open("GET","getdata.php?assign_to="+str,true);

you should see it in the network tab of your preferred debugging tool (firebug, chrome developer tools), or you could just log it to the console/alert it

alert("getdata.php?assign_to="+str);
console.log("getdata.php?assign_to="+str);

and you will see it works

EDIT:

a redirect to the current page with the param could be done like that (even if i dont understand why you would like to do that...):

var current_url = document.location.href;
var separator = current_url.indexOf('?') === -1 ? '?' : '&'; 
document.location.href = current_url + separator + 'assign_to=' + str;

EDIT2:

add this function to your scripts:

function setLinkAssignee(assignee) {
  var i, newParams = [], link = document.getElementById('show'),
      href = link.getAttribute('href')
      linkArr = href.split('?')
      params = (linkArr[1] || '').split('&');

  for(i=0; i < params.length; i++) {
    if(params[i].indexOf('assignee=') === -1) {
      newParams.push(params[i]);
    }
  }
  link.setAttribute('href', linkArr[0] + '?' + newParams.join('&') + '&assignee='+assignee);
}

and call it like this if you dont want the ajax request to be done:

<select name=\"assigned_to\" onChange=\"setLinkAssignee(this.value)\">

or like this inside your showData:

function showData(str) {
  // your stuff here
  setLinkAssignee(str);
share|improve this answer
see the edit at the bottom of my answer – hereandnow78 Feb 20 at 21:26
Sorry I realized that didn't make sense. I'm just having trouble understanding how I can get the $_GET['assigned_to'] variable back on the original page that generated the request. – Kevin O'Brien Feb 20 at 21:27
but you ARE on that page, sorry i really do not understand where your problem is... – hereandnow78 Feb 20 at 21:39
I cannot figure out how to get that variable into this URL: <a id=\"show\" href=\"index.php?page=overview&location=advance&assignee=\"".$assign_to."\"> I've tried appending it in an onclick href as well, and for some reason it just returns index.php?page=overview&location=advance&assignee=+str – Kevin O'Brien Feb 20 at 21:42
see EDIT2, you need to set the link, but parse the url before, remove the current assignee-param, and set the new one – hereandnow78 Feb 20 at 22:06
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.