Thread: PHP/Javascript

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13

    PHP/Javascript


    I thought I would post this problem in this forum as I think it is as much a PHP problem as Javascript.

    I have a script which shows customer info. This window is empty. What I want to do is select a customer ID in a new window and then refresh the main window with the customer ID.
    I have taken a stab at embeding some javascript in my PHP script, but it does not work. Can anyone point me in the right direction. Please bear in mind I have little javascript experience. Find the code for the child window:

    PHP Code:
    <?php 
    $query 
    "select * from customer where cust_name like '%$company%' "
    $queryResult mysql_query($query); 
    $i=0

    while(
    $row =mysql_Fetch_array($queryResult))  
    {  
         
        if (
    is_int($i/2)) 
        { 
             
            
    $bgcolor "#cccccc"
             
        } 
         
        else 
        { 
             
            
    $bgcolor "#ffcc99"
             
        } 
         
        print 
    "<tr>"
        print 
    "<td>"
        print 
    "<td bgcolor=\"$bgcolor\">$row[cust_id]</td>"
    print 
    "<td bgcolor=\"$bgcolor\"><a href=\"javascript:;\"window.opener.location.reload\"('customeradmin.php?cust_id=$row[cust_id]'\">$row[cust_name]</a></td>";  
        print 
    "<td bgcolor=\"$bgcolor\">$row[postcode]</td>"
        print 
    "<td bgcolor=\"$bgcolor\">$row[cust_phone]</td>"
        print 
    "</tr>"
        
    $i++; 
        

    ?>
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,947
    Rep Power
    4033
    Well, first off the problem it looks like is that you were missing a ) in your JS code. But it needs changed anyway. You should be using window.opener.location.href. The reload() method is for reloading the current page, not loading a new URL.

    Another thing you could do as a shortcut, is use the % operator for figureing out of a number is odd or even.

    Also, you need to use '' around key names in an array when they are not numbers. To do this and still embed them inside a string, use {} around the entire variable. This will make php accept the ' as a variable character instead of think that it is marking the end of the variable.

    Here is some revised code.

    PHP Code:
    $query "select * from customer where cust_name like '%$company%' "

    $queryResult mysql_query($query); 

    $i=0;
    while(
    $row=mysql_Fetch_array($queryResult)) 
    {
       
    // if $i%2 is 0, the number is even, otherwise it will be odd.  
       
    $bgcolor=($i%2==0)?"#cccccc":"#ffcc99";
        print 
    "<tr>"
        print 
    "<td>"
        print 
    "<td bgcolor=\"$bgcolor\">{$row['cust_id']}</td>";
        
        
    //There shouldn't be a space between java and script here either, but devshed keeps putting it in, must be a security thing. 
        
    print "<td bgcolor=\"$bgcolor\"><a href=\"javascript:\"window.opener.location.href='customeradmin.php?cust_id={$row['cust_id']}';\">{$row['cust_name']}</a></td>";  
        print 
    "<td bgcolor=\"$bgcolor\">{$row['postcode']}</td>"
        print 
    "<td bgcolor=\"$bgcolor\">{$row['cust_phone']}</td>"
        print 
    "</tr>"
        
    $i++;

    Last edited by kicken; November 4th, 2002 at 05:52 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    Kicken, thanx for the tips. I have tried you code without the space in javascript, but no joy. The window just sits there doing nothing. Not sure where to look. Is it worth putting the full URL in.
  6. #4
  7. No Profile Picture
    Please call me Joe ;)
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    the web
    Posts
    244
    Rep Power
    13
    Take the space out of "javascript" - VB automatically inserted it!

    Cheers,
    Joe of 4Life
    Cheers,
    Joe of 4Life

    Check out 4Life today!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    fourlife,

    Kicken, thanx for the tips. I have tried you code
    without the space in javascript,
    but no joy. The window just sits there doing nothing. Not sure where to look. Is it worth putting the full URL in.

    Any other ideas?
  10. #6
  11. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,947
    Rep Power
    4033
    One thing that slipped by me that would cause it not work is that you ended our URL before your JS code.

    PHP Code:
    print "<td bgcolor=\"$bgcolor\"><a href=\"java script:\"window.opener.location.href='customeradmin.php?cust_id={$row['cust_id']}';\">{$row['cust_name']}</a></td>"
    There is a \" after the javascript: so that would make the JS code after it be useless. Take that out and see if it works.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    I managed to sort most of it out myself with a bit of jiggery pokery and some informed reading.
    So in the main window you have some javascript in the header

    window.name = "customeradmin";




    you need to use javascript to open new window.

    The code in the new window goes like:

    PHP Code:
    $query "select * from customer where cust_name like '%$company%' ";  

    $queryResult mysql_query($query);  

    $i=0
    while(
    $row=mysql_Fetch_array($queryResult))  

       
    // if $i%2 is 0, the number is even, otherwise it will be odd.  
       
    $bgcolor=($i%2==0)?"#cccccc":"#ffcc99"
        print 
    "<tr>";  
        print 
    "<td>";  
        print 
    "<td bgcolor=\"$bgcolor\">{$row['cust_id']}</td>";  
    print 
    "<td bgcolor=\"$bgcolor\"><a href=\"customeradmin.php?cust_id={$row['cust_id']};\" TARGET=\"customeradmin\">{$row['cust_name']}</a></td>";   
        print 
    "<td bgcolor=\"$bgcolor\">{$row['postcode']}</td>";  
        print 
    "<td bgcolor=\"$bgcolor\">{$row['cust_phone']}</td>";  
        print 
    "</tr>";  
        
    $i++; 




    I could now do with a bit more help to also close the popup window once a cust_id is selected.
    Thanx for your help
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    kicken, will also give your idea a try
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    645
    Rep Power
    0
    try this:
    PHP Code:
    $query "select * from customer where cust_name like '%$company%' "

    $queryResult mysql_query($query); 

    $i=0;
    while(
    $row=mysql_fetch_array($queryResult)) 
    {
       
    // if $i%2 is 0, the number is even, otherwise it will be odd.  
       
    $bgcolor=($i%2==0)?"#cccccc":"#ffcc99";
        print 
    "<tr>"
        print 
    "<td>"
        print 
    "<td bgcolor=\"$bgcolor\">{$row['cust_id']}</td>";
        
        
    //There shouldn't be a space between java and script here either, but devshed keeps putting it in, must be a security thing. 
        
    print "<td bgcolor=\"$bgcolor\"><a href=\"java script:\"window.open('customeradmin.php?cust_id={$row['cust_id']}','test',width='400',height='400')\"\">{$row['cust_name']}</a></td>";  
        print 
    "<td bgcolor=\"$bgcolor\">{$row['postcode']}</td>"
        print 
    "<td bgcolor=\"$bgcolor\">{$row['cust_phone']}</td>"
        print 
    "</tr>"
        
    $i++;

    you had your window.open all wrong.

    also mysql_Fetch_array() needs to be mysql_fetch_array() notice the non-capitol F
  18. #10
  19. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,947
    Rep Power
    4033

    kinda weird


    just FYI, in php, function names are case insensitive, so mysql_fetch_array()==Mysql_Fetch_Array()==MYSQL_FETCH_ARRAY() ect. Variables arn't though. Kinda weird how they did that, but whatever works I guess.
  20. #11
  21. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    645
    Rep Power
    0
    that's true, but best not to create any bad habits
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    Scoutt and Kicken tried again with your solutions but still no love. I think i am going to continue with what is working, but still need help to close the child window. Any Ideas?
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    7
    Rep Power
    0
    Originally posted by kicken
    Another thing you could do as a shortcut, is use the % operator for figureing out of a number is odd or even.

    Here is some revised code.

    PHP Code:
    // ....
       
    $bgcolor=($i%2==0)?"#cccccc":"#ffcc99";
    // .... 
    Or you could have a quicker, bitwise comparison.

    PHP Code:
    // ....
       
    $bgcolor = ($i 1) ? "#cccccc" "#ffcc99";
    // .... 
    kicken, are you the same kicken from DALnet #php ?
  26. #14
  27. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    645
    Rep Power
    0
    try this one
    PHP Code:
    //There shouldn't be a space between java and script here either,  
        
    print "<td bgcolor=\"$bgcolor\"><a href=\"java script:window.open('customeradmin.php?cust_id={$row['cust_id']}','test',width='400',height='400')\">{$row['cust_name']}</a></td>"
    if that doesn't work tell us why? and if you use target="" they have to close the window with the close button in the top right hand corner. or you could use a button or link and do this

    <a href="window.close()">Close window</a> but you might get a confirmation popup which you can't get rid of.


    useing teh window.open you can use the link I just said without the confirmation.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    Hi scoutt. I got the old error 404 message. I have look at some tutorials and what you have done al looks good. weird. Just one thing what is 'test' for?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo