SitePoint Sponsor

User Tag List

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

    Unhappy pass php variable to javascript function

    Hello,

    i just want to pass two php variables into my JS functions. but it seems its passing one value and another is showing undefine. can anyone tell me why its happening?
    HTML Code:
    <input text="text" name="friends1" value="" id= "friends1"/><span><input type="hidden" name="id" value="<?=$row['id'];?>" /><a href="javascript:edit(<?php echo $row['id'];?>,friends1);"></span>Edit</a>
    edit.js

    Code JavaScript:
    function edit(id,friends1) {
     
    document.getElementById('insert_response').innerHTML = "Just a second..."
     
     var id= encodeURI(document.getElementById('id').value);
     var friends1 = encodeURI(document.getElementById('friends1').value);
     
     nocache = Math.random();
     alert("hello");
     
     http.open('get', 'edit.php?id='+id+'&friends=' +friends1+'&nocache = '+nocache);
     http.onreadystatechange = insertReply;
     http.send(null);
     }

    i am getting friends value but not getting id's value. please tell me how can i pass these two values?

    thanks in advance.

  2. #2
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now i am getting id perfectly. also i am able to update my first record. but for the second one id is changing but whatever i am putting in friends text box is replacing second text box's value.

    here is my code:

    Code JavaScript:
    function edit(id,friends1) {
     
          document.getElementById(id,friends1).innerHTML = "<em>Updating...</em>";
     
     //var id= encodeURI(document.getElementById('id').value);
     
    var friends1 = encodeURI(document.getElementById('friends1').value);
     
     nocache = Math.random();
     
     alert("hello");
     
     http.open('get', 'edit.php?id='+id+'&friends='+friends1+'&nocache = '+nocache);
     
     http.onreadystatechange = insertReply;
     
     http.send(null);
     }


    view.php

    PHP Code:
    while($row = $op->select($result)){ ?>
               <div  id='id'></div>

         <table width="100%" border="0" cellspacing="3" cellpadding="0">

          <tr bgcolor="#FDFAEE">

              <br/>
              <input type="text" name="friends"   value="<?=$row['friends'];?>"  id="friends1" size="25" maxlength=15 /><tr>
            <td width="30%" class="loginText"><span></td>
            <td width="70%">
            <span><input type="hidden" name="id" value="<?=$row['id'];?>" id ="<?=$row['id'];?>"/><a href="javascript:edit(<?=$row['id'];?>,friends1);"></span>Edit</a><span>|<a href="javascript:msgdelete(<?=$row['id'];?>);" title="Delete">Delete</a></span>

    <? ?>
    edit.php

    PHP Code:
    $friends1 =$_GET['friends'];
    $id $_GET['id'];
    echo 
    $friends1;

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


    //$id = $_GET['id'];
    $link mysql_connect('===''==''==')
      or die(
    'Could not connect: ' mysql_error());
      
    mysql_select_db('===') or die('Could not select database');
    echo 
    "UPDATE  friends set friends='".$friends1."' where id='$id' and  myid ='$userid'";

    $query "UPDATE  friends set friends='".$friends1."' where id='$id' and myid='$userid'";
     
    $result mysql_query($query) or die('Query failed: ' mysql_error());
    //echo "Updation is done"; 
    can you please help me?
    Last edited by deepM; May 3, 2009 at 22:01.


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
  •