deepson2 Posted November 17, 2009 Share Posted November 17, 2009 I want to pass my exact li's value using AJAX. but it seems it passing only first value. e.g i have the following value apple banana when i click on banana it consider apple only. here is my code. <? $sqlChk = $op->runsql("SELECT * FROM `fruitlist` WHERE sell_id='$sellid'"); if(mysql_num_rows($sqlChk) > 0) { while($row = mysql_fetch_array($sqlChk)) { $fruitname=$row['fruitname']; $pid=$pdata->pid; //which is session value ?> <input type="hidden" name="fruitname" id="<?=$row['fruitname'];?>" value="<?=$list_name;?>"/> <input type="hidden" name="pid" id="pid" value="<?=$pid;?>"/> <div id="insert_response"></div> <li><span><a href="javascript: insert();" title="<?=$fruitname;?>" ><?=$fruitname;?></a></span></li> <? }} ?> my js code is here var nocache = 0; function insert() { document.getElementById('insert_response').innerHTML = "Just a second..." var list_name= encodeURI(document.getElementById('list_name').value); var blogid = encodeURI(document.getElementById('blogid').value); nocache = Math.random(); //alert("hello"); // Pass the login variables like URL variable http.open('get', 'insert.php?fruitname='+fruitname+'&pid='+pid+'&nocache = '+nocache); http.onreadystatechange = insertReply; http.send(null); } function insertReply() { if(http.readyState == 4){ var response = http.responseText; // else if login is ok show a message: " added+ site URL". document.getElementById('insert_response').innerHTML = ' added:'+response; } } Can anyone please tell me how can i get each li's value on click? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
deepson2 Posted November 18, 2009 Author Share Posted November 18, 2009 Any help??? Quote Link to comment Share on other sites More sharing options...
deepson2 Posted November 18, 2009 Author Share Posted November 18, 2009 I would like to tell here what i am donig with hoping that ll get some help. I have one select query which gives me fruit name simultaneously. like this apple babana and i am showing it in the following format. <li><span><a href="javascript: insert();" title="<?=$fruitname;?>"><?=$fruitname;?></a></span></li> And i am passing the fruit name's value in hidden format like this <input type="hidden" name="fruitname" id="fruitname" value="<?=$fruitname;?>"/> <input type="hidden" name="pid" id="pid" value="<?=$pid;?>"/> And these values are inside my while loop. so whenever i clicks on the apple, value gets stored. but when i click on the next value. it again stores the apples value. So, my question is why the second value is not getting inserted with its own name? I hope the problem is now understandable. any suggestion would be highly appreciated. Quote Link to comment Share on other sites More sharing options...
deepson2 Posted November 18, 2009 Author Share Posted November 18, 2009 If i am passing my id into <li>, something like this <li id='fruitname' ><span><a href="javascript: insert();" title="<?=$fruitname;?>"><?=$fruitname;?></a></span></li> It storing "-1" value. Can anyone please tell me how can pass exact <li>'s value on every onclick function? Quote Link to comment Share on other sites More sharing options...
premiso Posted November 18, 2009 Share Posted November 18, 2009 On your page do you have multiple <input type="hidden" name="fruitname" id="fruitname" value="<?=$fruitname;?>"/> If so, the name and ID are not unique and will need to be or else conflicts will be caused. To fix this issue, you are going to have to associate the fruit inside the button rather than a hidden input: <li><span><a href="javascript: insert('<?=$fruitname;?>', <?=$pid;?>);" title="<?=$fruitname;?>"><?=$fruitname;?></a></span></li> Doing this, will allow you to know what fruit and pid, then in your insert function: function insert(fruitname, pid) { document.getElementById('insert_response').innerHTML = "Just a second..." var list_name= encodeURI(document.getElementById('list_name').value); var blogid = encodeURI(document.getElementById('blogid').value); nocache = Math.random(); //alert("hello"); // Pass the login variables like URL variable http.open('get', 'insert.php?fruitname='+fruitname+'&pid='+pid+'&nocache = '+nocache); http.onreadystatechange = insertReply; http.send(null); } function insertReply() { if(http.readyState == 4){ var response = http.responseText; // else if login is ok show a message: " added+ site URL". document.getElementById('insert_response').innerHTML = ' added:'+response; } } Give that a shot and see if it works, that way make more sense then using a hidden textfield any ways Quote Link to comment Share on other sites More sharing options...
deepson2 Posted November 19, 2009 Author Share Posted November 19, 2009 You just made up my morning premiso, Its working!! Hats off to php guru..... Many many thanks to you. Quote Link to comment 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.