SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing two values in java script

    hello,


    can we send two variables in one java script function?

    like this:
    href="javascript: edit(<?=$row['id'];?>,<?=$row['friends'];?>);"
    so i want to pass two values onto my php page.so

    add.js is

    Code JavaScript:
    function edit(id,friends){
     
    document.getElementById(id,friends).innerHTML = "<em>editing...</em>";
      alert("hello");
     
     
                  http.open('GET', 'edit.php?id='+id+'&friends='+friends);
     	     http.onreadystatechange = insertReply1;
                  http.send(null);
     
     
         }

    so i ll be able to update my php code.like this

    PHP Code:
    $id $_REQUEST['id'];
    $friends =$_REQUEST['friends'];
    //$id = $_GET['id'];

    if (isset($id) && isset($friends))
    {

    $query "UPDATE  friends set friends='".$friends."' where id='$id'";
     
    $result mysql_query($query) or die('Query failed: ' mysql_error());

    please help me.
    thanks in advance.

  2. #2
    SitePoint Zealot bgil's Avatar
    Join Date
    Apr 2009
    Location
    So. California
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can pass to a function as many parameters as you need.
    -Brad

  3. #3
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks bgil,

    can you tell me in my code how can i do that?

  4. #4
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Your code looks about right to me.

    But i dnt think you can comma separate .getElementById

    and if its string values do it like this: href="javascript: edit('<?=$row['id'];?>','<?=$row['friends'];?>');"


    and before posting it you might wana encode it:
    http.open('GET', 'edit.php?id='+encodeUriComponent(id)+'&friends='+encodeUriComponent(friends));

  5. #5
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply NightStalker-DNS,
    because its badly needed. i am trying this since long

    But i dnt think you can comma separate .getElementById
    even i think the same the question is how to send two value then?

    yaah. i have tried whatever you have suggested. but its not working.

    please help me.

  6. #6
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    function edit(){
     
     document.getElementById('insert_response').innerHTML = "Updating...";
      alert("hello");
       nocache = Math.random();
     
     
    if(document.getElementById('id').value !="" && document.getElementById('friends1').value !="" ){
     
    http.open('get', 'edit.php?id='+id+'&friends='+friends1+'&nocache = '+nocache);
     
     	     http.onreadystatechange = insertReply;
                  http.send(null);
      }
     
         }


    HTML Code:
    <input type="text" name="friends"   value="<?=$row['friends'];?>" id ="friends1"   size="25" maxlength=15 /><span>&nbsp;&nbsp; &nbsp;<a href="javascript: edit();" title="edit">Edit</a></span>|
    	 <input type="hidden" name="id" value="<?=$row['id'];?>" id ="<?=$row['id'];?>"/><span><a href="javascript:msgdelete(<?=$row['id'];?>);" title="Delete">Delete</a></span>
    tried this also not working

  7. #7
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Sorry, that was a type, should have read cant comma seprate IDs.

    Your function needs to accept 2 vars: function edit(id, friends)

    then use them in ur code without the ' wrapper. Just check the source code to make sure the Php is writing out something

  8. #8
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    viewfriends.php
    HTML Code:
    <input type="text" name="friends"   value="<?=$row['friends'];?>" id ="friends1"   size="25" maxlength=15 /><span>&nbsp;&nbsp; &nbsp;<a href="javascript: edit();" title="edit">Edit</a></span>|
    	 <input type="hidden" name="id" value="<?=$row['id'];?>" id ="<?=$row['id'];?>"/><span><a href="javascript:msgdelete(<?=$row['id'];?>);" title="Delete">Delete</a></span>
    Code JavaScript:
    function edit(){
     
     document.getElementById('insert_response').innerHTML = "Updating...";
      alert("hello");
       nocache = Math.random();
     
     
    if(document.getElementById('id').value !="" && document.getElementById('friends1').value !="" ){
     
    http.open('get', 'edit.php?id='+id+'&friends='+friends1+'&nocache = '+nocache);
     
     	     http.onreadystatechange = insertReply;
                  http.send(null);
      }
     
         }

    edit.php
    PHP Code:
    $id $_REQUEST['id'];
    $friends1 =$_REQUEST['friends'];
    //$friends1 =$_GET['friends'];
    $id $_GET['id'];

    if (isset(
    $id) && isset($friends1))
    {

    echo 
    "idiot's not working".$_REQUEST['id'];
    $link mysql_connect('10.0.0.4''root''gk123')
      or die(
    'Could not connect: ' mysql_error());
      
    mysql_select_db('deepali') or die('Could not select database');


    $query "UPDATE  friends set friends='".$friends1."' where id='$id'";
     
    $result mysql_query($query) or die('Query failed: ' mysql_error());
    //echo "Updation is done";
      // Printing results in HTML
       
    if(mysql_affected_rows() > 0){

     echo 
    "Updation is done"mysql_affected_rows();

       }else{

     echo 
    "not done";

     }

    have tried this. now its going into function. but getting this error.
    idiot's not working[object HTMLDivElement]updation's not done
    that means php code is not getting values. could you please have look and tell me what's wrong here. because while removing/adding its working fine though that time i had to sed only one value, but here have to send two

    please help me.

  9. #9
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Ur html inputs have no ids

    <input type="text" name="friends"
    to
    <input type="text" name="friends" id="friends"

    and

    <input type="text" name="id"
    to
    <input type="text" name="id" id="id"

    PS. U might want to change those ids. id="id" seems a bit dodgy to me

  10. #10
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    muqtada123, thanks for your inspirational support.


    Quote Originally Posted by NightStalker-DNS View Post
    Ur html inputs have no ids

    <input type="text" name="friends"
    to
    <input type="text" name="friends" id="friends"

    and

    <input type="text" name="id"
    to
    <input type="text" name="id" id="id"

    PS. U might want to change those ids. id="id" seems a bit dodgy to me
    NightStalker-DNS,

    I have tried this changing values or id. but still its not working.
    now the status is page is not passing any values. getting somthing like this:
    [object HTMLDivElement]
    that means values is not passing. what is your suggestion ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •