Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ajax probs

Options
  • 15-10-2008 10:20pm
    #1
    Registered Users Posts: 8,070 ✭✭✭


    basically using Mootools Mocha, to have NICE popups,

    i have a small forum inside it but onSubmit, it open page in a different window.
    So need to use ajax/XHR

    i previously had it working but have no idea how i passed my phpvariable into the javascript var. Using echo, im getting weird results.

    [PHP]


    <script type="text/javascript">



    <!--
    //Browser Support Code
    function ajaxFunction(){

    var ajaxRequest; // The variable that makes Ajax possible!


    try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
    } catch (e){
    // Internet Explorer Browsers
    try{
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try{
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e){
    // Something went wrong
    alert("Your browser broke!");
    return false;
    }
    }
    }





    var q1;
    var queryString = "?q1=" <?php echo $tolist; ?>;

    ajaxRequest.open("GET", "check2.php" + queryString, true);
    ajaxRequest.send(null);

    }

    //-->
    </script>

    <?php
    //connect code

    $checkB3 = mysql_num_rows($executeB2);


    $queryFwdC = "SELECT artfwd FROM buddy WHERE regid='$reg_id'";
    $executeFC = mysql_query($queryFwdC) or die("Cant execute query: ".$queryFwdC." ".mysql_error());
    $rowC5 = mysql_fetch_array($executeFC);
    $artFwd = $rowC5;

    ?>


    <form>
    <SELECT name="listbox[]" rows=" <?php echo($checkB3);?>" size="6" multiple="multiple" STYLE="Width:130px">




    <?php
    while ($resultB2 = mysql_fetch_array($executeB2)){
    $buddyfname = $resultB2;
    $buddylname = $resultB2;
    $buddyemail = $resultB2;
    ?>


    <OPTION value="<?php echo($buddyemail);?>"><?php echo($buddyfname).",".($buddylname);?></OPTION>



    <?php

    }
    ?>
    </SELECT> <br>
    <font face='arial' size='1' color='#ffffff'><b>To select more than one friend, press <br> and hold CTRL and select friend</b></font>
    <br><br>
    <input type="submit" class="buttonS" onSubmit="xo();"/>
    </form>
    <br>


    <?php


    $addlist=$_REQUEST;
    if ($addlist){
    foreach ($addlist as $t)
    {
    $tolist .= $t.",";
    }
    }
    $tolist = substr($tolist,0,strlen($tolist)-1); // remove the trailing comma


    else
    {}
    ?>
    [/PHP]

    basically tolist HAS all the SELECTED ITEMs from the LISTBOX, need to pass them to q1.

    i tried using dom to get all the elements but codes bit hazy right now
    <html>
    <head>
    <script type="text/javascript">
    function getOptions()
    {
    var x=document.getElementById("y");
    txt=""
    for (i=0;i<x.length;i++)
    {
    txt=txt + "\," + x.options[x.selectedIndex].value;
    }
    alert(txt);
    }
    </script>
    </head>
    <body>

    <form>
    Select your favorite fruit:
    <SELECT name="listbox[]" rows=" 7" size="3" multiple="multiple" id="y">


    <option value="john@abc.net"> John </option>
    <option value="farz@abc.net"> Farz </option>

    <br /><br />
    <input type="button" onclick="getOptions()"
    value="Output all options">
    </form>

    </body>
    </html>

    Thanks a million


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 8,952 Mod ✭✭✭✭mewso


    function getOptions()
    {
    var x=document.getElementById("y");
    var txt = "";
    for (i=0;i<x.options.length;i++)
    {
       var opt = x.options[i];
       if (opt.selected == true)   {
          txt=txt + "\," + opt.value;
       }
    }
    alert(txt);
    }
    

    As to the php echo maybe it should be:-

    var queryString = "?q1=<?php echo $tolist; ?>" ;

    not a php man so can't be sure.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Hi OP, I'm a little confused on what you are trying to achieve with this code. Could you explain what you want to happen (without any reference to the code you have provided please - just in your own words what you want to happen) and maybe there might be an easier way to get the same effect.

    Regards,
    RD


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    that wont make a different, thanks though, basically this is what i see in the url so the parameters are passing but very oddly.

    eg if i select 2 items in the listbox

    buddy3.php?listbox[]=some.name%40cognitivecorp.com&listbox[]=user%40tcd.ie


  • Moderators, Science, Health & Environment Moderators Posts: 8,952 Mod ✭✭✭✭mewso


    Placebo wrote: »
    that wont make a different, thanks though, basically this is what i see in the url so the parameters are passing but very oddly.

    eg if i select 2 items in the listbox

    buddy3.php?listbox[]=some.name%40cognitivecorp.com&listbox[]=user%40tcd.ie

    Well the script should work. I fixed a few issues with it.


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    Hi OP, I'm a little confused on what you are trying to achieve with this code. Could you explain what you want to happen (without any reference to the code you have provided please - just in your own words what you want to happen) and maybe there might be an easier way to get the same effect.

    Regards,
    RD

    Basically its a friends list so you can forward articles to your friends.
    -one other script add friends to the DB
    -this script reads from that DB and populates a LISTBOX
    -each friend has a 'value' of an email and name as the 'option'

    -it works fine on a normal php/submit refresh page.
    BUT im trying to get this to work in a special popup, which will only allow ajax.


    so all i need is to pass my variables on to the php page via ajax,
    the variable would contain just the emails OF the selected names.

    it does that but its sending other characters so now i think its best i get the selected names straight through javascript [so il try that now musician, i missed your code there, i was trying to add an if/else statement there but i wasnt sure if 'selected' attribute existed - is there anywhere i can get the DOM/javascript API'ish]

    thanks


    EDIT: that javascript works perfect THANKS [minus the extra , i get at the end or start]

    BUT despite this, the url is still showing
    buddy3.php?listbox[]=my.name%40hotmail.com

    why is the listbox's name coming up !!!?


  • Advertisement
  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Because that's the name of the field. :D Even though it's the same field each one has to have a name/id. I'd also suggest against the [] in the name of the field. This looks confusingly like an array (which may be what you had in mind but this is not the way to go with this).


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    well thats the ONLY way to populate the list via a db :/


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Yes and no - your mixing up client side and server side coding logic here. Yes to create an array/list you would use the [] to show it's an array - but the name of the tag shouldn't have the square brackets. Server side we'd use the [] to designate that the data coming in was a list but client side no need for the square brackets - in fact I think this would probably cause all kinds of problems.

    I can see what you're doing - you're taking a list of people and selecting them. Does this list change? If not it might make more sense to store the list of friends in a database. If it does change a better way to get the effect you're looking for might be to have a hidden field on the html form that stores the list of friends as the person selects them and then it is this field that is submitted to the server. No need for messing around with arrays then.


Advertisement