Jump to content

[SOLVED] pass different values according to on click through ajax


deepson2

Recommended Posts

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.

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.

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?

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.