SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php pages and ajax

    I'm trying to create a page that pulls values from sql and puts them into a table within a form. I have been using an ajax script that i have to listen for changes to items within the page.

    anyways, each row of the sql result gets put into a row on the table and each row has 1 textfield with ids of textfield[num] where num starts at 0 and goes to the number of rows in the table - 1. im wondering how to make the ajax script listen to all of them without having to put a million objects in the script since they number of rows will be different each time the page is called. can i have it listen to textfield[] as if it's an array?

    if that is not possible, can you tell me any other ideas of how to make this work?

    Thanks in advance.

    here is the code i would like called after any textfield[] box is changed.
    Code:
    // JavaScript Document
    
    Event.observe(window, 'load', init, false);
    
    
    
    function init(){
    	 $('updateButton').style.display = 'none';
    	 Event.observe('quantity','change', updateCart, false);
    	 
    }
    
     function updateCart(){
    	var url = 'updateCart.php';
    	var pars = 'itemID='+escape($F('itemID'))+'&quantity='+escape($F('quantity'));
    	var target = 'cartDiv';
    	var myAjax = new Ajax.Updater(target,url, {method: 'get',parameters: pars});
    	
    }
    Last edited by LHOR; Apr 13, 2007 at 01:24. Reason: added code

  2. #2
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any help?

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    someone has got to know something... please help

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually I don't really get you, but maybe you can try to list all the object and assign the listeners to those object like having to call getElementsByClassName() to grab the collection of input you intent to update it with ajax.

  5. #5
    SitePoint Enthusiast firmaterra's Avatar
    Join Date
    Dec 2006
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if this is exactly what you're looking for but here goes;

    I use Smarty templating engine to loop throught results rather than using PHP direct. I presume if its a form that the webpage will be refreshed (i'll get to ajax in a minute). So after the sql query assign the result to an array and then in the template file loop through the results (using smarty) assigning each of them to a table cell. EG:

    //execute the sql query
    $sql="SELECT * from USERS where age=23";
    $rs=$conn->execute($sql);

    assign results required to variable
    $users=$rs->getrows();

    //assign to smarty engine
    STemplate::assign('users', $users);

    In your tpl file you then loop through the results with smarty;

    //loop though $users - can leave out start and max if not required
    {section name=i loop $users start=0 max=10}
    //start making table etc. abbreviated here.
    <table>
    <tr><td align="center">
    //presuming there are rows in the sql query called 'name' and age !
    1st user is {$users.name}</td>
    <td>age is {$users.age}</td>
    </tr>
    <td>2nd user is ..etc





    Its simple enough in practise. I can forward you demo code if you like. the only other way to accomplish this is to loop through them inside php code and 'echo' the table. You could then 'ajax' the table into a div on your webpage.

    Hope this helps

    *edit* forgot to mention that if you want the form fields prepopulated just assign the value of that field to the smarty value

  6. #6
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, i appreciate the help so far. Let me try to explain once more.

    i call a table within mysql and output each row using a while loop into table rows

    one of the elements within each row of the table is a textfield which i have named as if they were an array (tF[0], tF[1]...)

    I would like my javascript to listen to all of the textfields in the entire table without having to enter

    Code:
    Event.observe('tF[]','change', updateCart, false);
    as mentioned above for each textfield because i will never know how any tF[]'s there actually are because it will all depend on the mysql_query

    is there anyway i can make it listen to all of the tF[]s dynamically without having to add the Event.observe() function for each array.

    i dont want to have to do this
    Code:
    Event.observe('tF[0]','change', updateCart, false);
    Event.observe('tF[1]','change', updateCart, false);
    Event.observe('tF[2]','change', updateCart, false);
    Event.observe('tF[3]','change', updateCart, false);
    ...
    Event.observe('tF[n]','change', updateCart, false);
    Thanks again for the help. I hope the explanation is better.
    Last edited by LHOR; Apr 16, 2007 at 12:22. Reason: code

  7. #7
    SitePoint Enthusiast firmaterra's Avatar
    Join Date
    Dec 2006
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At the risk of sounding a bit silly and I may be gone off on the wrong tangent (again!!) if you are creating the table and fields dynamically within the while loop (I presuming your using php to pull results from the sql query) you could echo the java code for the text field from within that same loop?? That way you've a one line javascript code no matter how many tF[]'s you have.

  8. #8
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try that but as of right now, my javascript is in a different file


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
  •