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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    javascript function in redirection possible?

    Is this possible?
    PHP Code:
    header("Location: Javascript: addToDb();"); 
    Compare bible texts (and other tools):
    TheWheelofGod

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Not to the best of my knowledge.
    Header forces a redirection header (in this instance). You can't put javascript into it because JS is a part of the output body (even though its within the 'head' section of HTML, its not the same as a header).
    So... no I don't believe you can use any type of JS function within the header. I'd be pretty leary of using it even if it would work, but you could try outputting the HTML page before sending the header and use output buffering. I still don't think it will work, but only one way to find out! Post you're results.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder byuhobbes85's Avatar
    Join Date
    Oct 2006
    Location
    Ames, Iowa, USA
    Posts
    116
    Thanks
    9
    Thanked 4 Times in 4 Posts
    You can call a JavaScript function from the href attribute of an anchor tag using the technique you're asking about.
    Code:
    <a href="javascript:addToDb();">Add to database</a>
    However, what you are trying will not work with PHP. PHP is executed on the server, while JavaScript is executed on the client. In other words, PHP has returned an HTML document to the client before any JavaScript is executed. You cannot put calls to JavaScript functions in PHP functions, and I expect that your header function is throwing an error.

    That being said, there may be a way to accomplish what you're trying. Some more details may be helpful.
    Last edited by byuhobbes85; 03-03-2009 at 07:21 PM. Reason: formatting fix
    -- </byuhobbes>

  • #4
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    You can do this with an onload in your html body tag:

    Code:
    <body onload="addToDb()">


  •  

    Posting Permissions

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