javascript - PHP variable stores different values? -
i clueless problem. have variable called $opts
. use fill select results database query (mysql).
<select id="user" name="user" > <?php $cons=array(); $opts=getoptions("user",$cons,0); logge(date("y-m-d h:i:s")." - ".$tag." - ".$fn." - opts are=".$opts); echo $opts;?> </select>
this working , select filled usernames:
<option value="1">admin</option> <option value="2">test</option>
i have select-multiples filled depending on username selected. here javascript in document ready function:
$(document).ready( function () { // rootloc returns root url var $_get = {}, args = location.href.substr(0).split("/"); var rootloc="http://"+args[2]+"/"+args[3]+"/"; $("select").change(function() { console.log("select on change called."); var elem=this.id; //this.id id of calling element console.log("element= " + elem); var seluserid=$(this).val(); // returns userid of selected user console.log(seluserid); if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { } } xmlhttp.open("get",rootloc+"dokumenteuserform.php?seluserid="+seluserid,true); xmlhttp.send(); }); //after first load fire change event fill lists console.log("call onchange first") obj = document.getelementbyid("user"); fireevent(obj,'change');
});
now filling 2 lists regarding selected user values form database: (back html , php)
<select id="dokumente" name="dokumente" multiple="multiple" size="10"> <?php if (isset($_get["seluserid"])){ $seluserid=$_get["seluserid"]; logge(date("y-m-d h:i:s")." - ".$tag." - ".$fn." - seluserid= ". $seluserid); $opts2=getlistoptions("dokumente",$seluserid,0); logge(date("y-m-d h:i:s")." - ".$tag." - ".$fn." - opts are=".$opts2); echo $opts2;} ?> </select>
the second list filled same way.
<select id="userdoumente" name="userdokumente" multiple="multiple" size="10"> <?php if (isset($_get["seluserid"])){ $opts3=""; $opts3=(string)$opts3; $seluserid=$_get["seluserid"]; logge(date("y-m-d h:i:s")." - ".$tag." - ".$fn." - seluserid= ".$seluserid); $opts3=getlistoptions("userdokumente",$seluserid,0); logge(date("y-m-d h:i:s")." - ".$tag." - ".$fn." - opts3 are= ".$opts3); $opts=$opts3; } logge(date("y-m-d h:i:s")." - ".$tag." - ".$fn." - opts are=".$opts); echo $opts; ?>
my problem right in last select-multiple list. logging built html code in different places. first in called funciton getlistoptions
. result there is:
2015-06-18 17:45:22 - table.php - getlistoptions - opts are=<option value="1">doks/uploads/</option>
now logging , can see above, right after saved in $opts3
:
2015-06-18 17:45:22 - doksform.php - mainhtml - opts3 are=<option value="1">doks/uploads/</option>
then putting $opts3
$opts
log says:
2015-06-18 17:45:22 - doksform.php - mainhtml - opts are=<option value="1">doks/uploads/</option>
but when echoing $opts values of first selects appear in box (html source code):
<select id="userdokumente" name="userdokumente" multiple="multiple" size="10"> <option value="1">admin</option> <option value="2">test</option> </select>
how can be? understand it, overwriting $opts new values. log say... have idea? dont know how describe problem or words google problem. tried explain here. if there further information needed, can provide it.
if use echo $opts3;
instead, nothing in list nor in html source code seems empty variable. php error saying $opts3
undefined. can variables inside isset if statement others outside? or can maybe xampp installation broken?(but else seems work) -> reinstalled xampp -> problem still there
the problem lies in xmlhttp request part. tried ajax request instead (replaced xmlhttp stuff that):
$.ajax({ type: 'post', url: 'dokumenteuserform.php?seluserid='+seluserid, // data: $('form').serialize(), success: function () { //alert('form submitted'); } });
i logged url before php builds html code options , says:
2015-06-19 16:35:28 - dokumenteuserform.php - mainhtml - absolute_url= http://localhost/testwebsite/dokumenteuserform.php?seluserid=2
so excpected. thing is, in browser url didn't change. still http://localhost/testwebsite/dokumenteuserform.php
before. think url has changed on server side, not on client side. maybe thats same $opts3
variable?
i got working, , think know reason didn't work before. if problem, it's simple answer , took me more 3 days find it.
my problem was, trying populate html select (multiple) replacing options inside select. didnt had div in there trying divs brought me thread: <div> , <select> tags
with detour on div came solution set elements inner html.
so have empty select element first:
<select id="dokumente" name="dokumente" multiple="multiple" size="10"> </select>
then in onchange javascript function ajax post , set inner html result:
var url="getmselectopts.php"; var p1 = $.post(url,{what:"dokumente",u:seluserid}); p1.done(function(data){ // console.log(data); document.getelementbyid("dokumente").innerhtml = data; });
and works.
Comments
Post a Comment