Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP called from javascript help??????

    Hi.

    i was wondering if i could use JS to include a php script, so that when the focus is taken off a form item, the php runs, checking the form item, and displaying an error message.

    i don't need any code, im just too lazy to code and then figure out it does not work.

    thanks in advance,
    Kari

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    You're looking for AJAX, me thinks.

    Wiki on what AJAX is
    Patronizing AJAX tutorial
    Sample code
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #3
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    Depending on the type of check you need to perform, it may also be possible to do this via just JavaScript.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #4
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure it can.... take a simple example. Save the following code as "sample.php"
    Code:
    <html>
    <head><title>Js calling PHP variable</title>
    <script type="text/Javascript">
    var jsVar = <?php  require_once("include.php"); echo "'".$someVariableInPhp."'"; ?>
    // rest of the Js code here
    </script>
    </head>
    </html>

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so i could take that and run almost anything in between <?php ?>, and would this be any time, even after the page is loaded?

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    You have got to be joking. All php code on a page is executed when that page is requested from the web server. The php code that rochakchauhan posted was executed and the value was echoed at the time the page was sent to the browser.

    If you want php to act upon anything that is produced in the browser, you must get that information to the server in the form a http page request, which is why AJAX has already been mentioned as a possible way of accomplishing the validation.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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