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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP + Javascript mix

  • 01-10-2005 5:16pm
    #1
    Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭


    Hey, trying to mix PHP and Javascript but for some reason cannot get my loop to increment. Have tried it two ways...


    1.)
    function makeString()
    {
    string="";

    for (var j = 1; j <= <? echo "$num"; ?>; j++)
    {
    box = eval("document.form1.C" + j);
    if (box.checked==true)
    {
    string = string + "\n <? echo mysql_result($result,"+(j)+","name"); ?>,\t\t\t <? echo mysql_result($result,"+(j)+","description"); ?>";

    }else
    {

    }
    }

    }



    2.)
    function makeString()
    {
    string="";

    <?
    $t=0;
    ?>

    for (var j = 1; j <= <? echo "$num"; ?>; j++)
    {
    box = eval("document.form1.C" + j);
    if (box.checked==true)
    {
    string = string + "\n <? echo mysql_result($result,$t,"name"); ?>,\t\t\t <? echo mysql_result($result,$t,"description"); ?>";

    }
    <?
    $t++;
    ?>
    }
    }


    Could anyone tell me why I cannot get the j (javascript) in 1.) or the $t (PHP) in 2.) to increment? Depending on how many checkboxes the same result (first result) gets added to the string that many times.
    Any solution?

    Thanks in advance


Comments

  • Registered Users, Registered Users 2 Posts: 1,726 ✭✭✭gerryk


    First off, can you wrap it in # (code) tags.
    Secondly, I'm pretty sure you can't just nest scripts like that.. not without splitting it up or escaping the tags.


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    javascript is run clientside.

    php is run serverside.

    This means that any php code you have is run once, when the page is requested and sent by the server, whereas the javascript code is run whenever the event happens that triggers the code.

    This further means that gerryk is right and the code cannot be mixed in the manner you are attempting.

    You will have to come up with a new solution. If you need some help explain what exactly it is you are trying to do, and perhaps we can help you.

    Baz_


  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


    What does the resulting Javascript say?
    for (var j = 1; j <= <? echo "$num"; ?>; j++)
    
    What does this look like when you look at the page source?

    I disagree with gerryk and Baz_, I see no reason why you cannot mix scripts. I've done it with perl where I've written CGI scripts to generate Javascript on the fly.
    Lads: Peter B asked about this yesterday and is implementing something I suggested there. I guess he has omitted the PHP code that generates '<script>' tag and the code for the HTML.


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    daymobrew wrote:
    I've done it with perl where I've written CGI scripts to generate Javascript on the fly.

    Generating the javascript from php at page request time is no problem, actually expecting the php code to work when embedded in the javascript is just not going to work.

    The php code will not work client side.

    You can generate any client side content you want with php, including the form that he is trying to generate as well as the javascript as already mentioned, but once the page is downloaded and displayed the php code will have been stripped out of the page and will not/cannot run. It seems from the code that that is exactly what he is trying to do.

    It's not crystal clear what he's trying to do though, and I leave myself open to correction...


  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


    Baz_ wrote:
    It seems from the code that that is exactly what he is trying to do.
    I think that Peter K knows that php=server/javascript=client.

    His PHP currently returns a list of results. He is adding a checkbox with each item. I think the code above is for a 'Select All/Deselect All' feature (see his original thread).

    I'm sure he could write generic (i.e. not specific to a result set) Javascript where he wouldn't have to generate it on the fly e.g. when the 'Select All' button/link/checkbox is clicked, the Javascript code would loop through ALL items in the form and check/uncheck those with a name that began with 'C'.
    Then the Javascript could be put in an external file and be in the browser cache. That's what I did in my CGI (I only generated Javascript code where I couldn't make it generic.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,726 ✭✭✭gerryk


    daymobrew wrote:
    I think that Peter K knows that php=server/javascript=client.

    Maybe, but I don't think he understands the implications of that.

    daymobrew wrote:
    I disagree with gerryk and Baz_, I see no reason why you cannot mix scripts.

    I didn't say you couldn't mix scripts, just not like he is doing. He's trying to get code to be recognised at both client and server side simultaneously.

    In the case of the first one, j is undefined (0?) at the point of execution of the PHP mysql function.

    In the case of the second... the PHP won't loop, because the loops are in Javascript, not PHP.

    Be better to create the page entirely using PHP and the refer to it using Javascript DOM.


  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    Thanks for the help.

    Yea now I notice that I have been stupid. Doh! When I looked at the code in the source I noticed what Javascript command was being completed and how there was no PHP there, hence the result would never change.

    What I am trying to do is introduce checkboxes to my search results table, one for each result, similar to Gmail. What I wanted to do was with my submit button in my form, make it have an onclick function to ask the user are they sure they would like to delete the products they selected.

    onclick="makeString(); javascript:return confirm('Are you sure you would like to delete these products? \n\n Name\t\t\t Description \n '+string)"

    If they do not press ok, no action is completed. On pressing ok, a PHP page would be called, which would send the query to the database to delete selected products.

    The problem is since the checkboxes names are c1 to cN, I use a javascript function (makestring) to run through them to check if they are checked and if they are it adds them to the String 'string'. If Checkbox 4 is checked, I need to somehow access the "name" and "description" of result index 3.

    I think this is not possible but does someone have any solution? Maybe I need to have the confirm javascript call on the php page which is called.

    I hope this makes some sense.....


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    I haven't really worked with the dom model for a while so please bear with me.

    Is it not possible to treat each input type as an array of that input type using dom and javascript ala:
    for (i=0;i<form.checkbox.count;i++)
    {
    if (form.checkbox(i)==checked)
    {
    tocheck += "." + form.checkbox(i).name;
    }
    }
    

    That way the checkbox names could be the item id and the tocheck variable could be a dot separated (i.e. parsable) string of id's to be displayed in the message box.

    Again I'm not sure if this is possible, but if memory serves me correctly it is...

    <edit>
    Unless my memory served me really well, which is unlikely, then that code is hella wrong, so don't copy it, you will have to look up some tutorials to get the correct methods and properties which need to be accessed.
    </edit>


  • Registered Users, Registered Users 2 Posts: 1,726 ✭✭✭gerryk


    use getElementById() and just refer to each item by its ID html tag.


Advertisement