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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.