Results 1 to 9 of 9
  1. #1

    Javascript and PHP

    Is there anyway to make this work?

    Code:
    <SCRIPT Language="Javascript">
    ponumber= prompt ("<?PHP echo mssql_num_rows($result)?> records found. How many do you wish to view?");
    document.write ("<?PHP $ponumber = " +ponumber+ " ?>");
    </SCRIPT>
    fraudservers.com - for sale!

  2. #2
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082
    No, by the time the javascript is executed the PHP is already completed. You can't intersperse them because one is running on the client and the other on the server; the communication is essentially one-way. The only way to do that would be to submit a form that then returns the desired results, or use more advanced Javascript to call PHP to get the data and then format it in JS.

  3. #3
    maybe you can do something like this

    echo '<SCRIPT Language="Javascript">
    ponumber= prompt ("<?PHP echo mssql_num_rows($result)?> records found. How many do you wish to view?");
    document.write ("<?PHP $ponumber = " +ponumber+ " ?>");
    </SCRIPT>
    ';

  4. #4
    Join Date
    Jan 2003
    Location
    USA, FL
    Posts
    251
    Here are some ideas on how to recall javascript and have php load in new inf

    Check out the code below to give you an idea.

    Code:
    <script language="javascript">
    
    <?php $mytickround = 5; ?>
    
    var myticksec = <?php echo 59 - strftime("%S"); ?>;
    var mytickmin = <?php echo $mytickround - 1 - (strftime("%M") - (floor(strftime("%M") / $mytickround) * $mytickround)); ?>;
    
    var rtsec = <? echo strftime("%S") + 1; ?>;
    var rtmin = <? echo strftime("%M"); ?>;
    
    function myticktime()
    {
    	if(myticksec <= 0 && mytickmin <= 0)
    	{
    		myticksec = 0;
    		mytickmin = <?php echo $mytickround; ?>;
    	}
    	if(myticksec <= 0)
    	{
    		mytickmin = mytickmin - 1;
    		myticksec = 60;
    	}
    
    	myticksec = myticksec - 1;
    
    	if(rtsec >= 59 && rtmin >= 59)
    	{
    		rtsec = 0;
    		rtmin = 0;
    	}
    	if(rtsec >= 59)
    	{
    		rtmin = rtmin + 1;
    		rtsec = 0;
    	}
    
    	rtsec = rtsec + 1;
    	
    	if(myticksec < 10) {document.getElementById('tick_timer').innerHTML = mytickmin + ":0" + myticksec + " - " + rtmin + ":" + rtsec};
    	if(mytickmin < 10) {document.getElementById('tick_timer').innerHTML = "0" + mytickmin + ":" + myticksec + " - " + rtmin + ":" + rtsec};
    	if(myticksec < 10 && mytickmin < 10) {document.getElementById('tick_timer').innerHTML = "0" + mytickmin + ":0" + myticksec + " - " + rtmin + ":" + rtsec};
    }
    
    window.setInterval("myticktime()", 1000);
    </script>
    The above script might be a bit complex but it basicly counts down from 5 mins to 0 and then starts over again and it does it according to the time that php has. Now, i am no rocket scientist but I would think that if you just place all this code and put the php information you are wanting to change within the actual function that does the look each "etc" amount of miliseconds. I would think you could pull out what ever php info you need each go around.

    If that doesnt work another idea is to make a mime type for php which has the extenion of js and then within the external js file put your php stuff. Then when you call the js script each time it will load the new information.

    Either way, give both of these a try and let me know how it works out.

  5. #5
    The fact that you asked that question shows that you need to understand PHP better, but when you do, you might want to take a look at AJAX (a framework for client side javascript applications that interface with server side languages using XML and HTTP).

    SAJAX is one solution for using AJAX with PHP.

  6. #6
    Originally posted by error404
    No, by the time the javascript is executed the PHP is already completed. You can't intersperse them because one is running on the client and the other on the server; the communication is essentially one-way. The only way to do that would be to submit a form that then returns the desired results, or use more advanced Javascript to call PHP to get the data and then format it in JS.

    True, you have to make sure to parse the JS 'dynamic' script before sending it to the client.
    *¨) ¤ BHS-D¤
    .·´ .·*¨) .·*·. *
    (¸.·´¸.·*¸ ¤ www.bhsdesign.com¤

  7. #7
    Join Date
    May 2004
    Location
    Mountain View, CA
    Posts
    226
    I'm learning C at the moment. About how fast would you think I could learn java/javascript? I also am looking to learn PHP as well. I know HTML already, so would that be pretty easy?
    DreamLogic Cult Film and Music Reviews
    STOPware - Visitor Management solutoins

  8. #8
    Join Date
    May 2005
    Posts
    38

    Re: Javascript and PHP

    Originally posted by Atomic Haven
    Is there anyway to make this work?...
    The way I got around this is to create a PHP script that fills in all the variables and then outputs the javascript.

    <script language="javascript" source="myscript.php">

    myscript.php basically is...

    <?php

    [php code that sets all the values of the variables]

    $sendout = <<< EOM

    [all your javascript code here, using PHP variables where needed]

    EOM;

    echo $sendout;

    ?>

    If you need to customize this based on the page the user is viewing, you can always use the $_SERVER['HTTP_REFERER'] variable to determine which page is sending the request to myscript.php and customize based on that. Or if you need to pass variables, you can call "myscript.php?variable1&&variable2" then use $_SERVER['QUERY_STRING'] to get the variables.

    - Greg

  9. #9
    Originally posted by Hurga
    I'm learning C at the moment. About how fast would you think I could learn java/javascript? I also am looking to learn PHP as well. I know HTML already, so would that be pretty easy?
    I leant C++ in college, I spend few days on HTML, PHP was a piece of cake to begin with. After it's like all programming language, it comes with experience and lot of practice.
    *¨) ¤ BHS-D¤
    .·´ .·*¨) .·*·. *
    (¸.·´¸.·*¸ ¤ www.bhsdesign.com¤

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •