CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2003
    Posts
    162

    javascript and php

    hi, how can i send a confirmation dialog of javascript in php and get the respond of ok or cancel from the dialog??

    thanks alot ....
    0 error(s), 0 warning(s)

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: javascript and php

    You will have to pass it through a parameter or use AJAX.

    Code:
    <script type="text/javascript">
    var okcancel = confirm();
    var theResponse = (okcancel) ? 'yes' : 'no';
    window.location = 'test.php?response=' + theResponse
    </script>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Aug 2003
    Posts
    162

    Re: javascript and php

    !!! thank you very much
    got it to works hehe
    0 error(s), 0 warning(s)

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: javascript and php

    You're welcome. Good luck with the rest!
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Aug 2003
    Posts
    162

    Re: javascript and php

    erm.. could i continue to ask ... after i do a window.location...
    how can i grab the variable from the url and set it to the window.location??

    for example when the confirmation dialog is invoke the url is
    test.php?abc=3&ddd=4

    how can i retrieve the abc=3 and add it to the window.location?

    thanks
    0 error(s), 0 warning(s)

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: javascript and php

    Just use $_GET.

    Code:
    <?php
      $abc = $_GET['abc']; // value will equal 3
    ?>
    
    <script type="text/javascript">
      window.location = 'test.php?abc=<?php echo $abc; ?>';
    </script>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Aug 2003
    Posts
    162

    Re: javascript and php

    my javascript is a .js file... and the function is invoke onclick of a button..
    could i just add the php tag inside the .js file??
    0 error(s), 0 warning(s)

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: javascript and php

    No. The JavaScript file is interpreted on the client side. Therefore the PHP, being server-side, cannot read it.

    If you need to access the variable using JavaScript, here is a function I just made up that should help you.

    Code:
    function retrieveURLVariable(name){
      var vURL = window.location.search.substr(1);
      var vParts = vURL.split(name + "=");
      vParts = vParts[1].split('&');
      return vParts[0];
    }
    
    var test = retrieveURLVariable('abc');
    alert(test);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Aug 2003
    Posts
    162

    Re: javascript and php

    oh .. thats a good function~

    if for php say i have a <a href="..test.php?abc=3>

    after i do the processing how can i set back all the variable that has been passed over?
    i know i could do a redirection and set it back 1 by 1 ... but i have lots of variable to do ...
    is there any more efficient method?

    thanks in advance ~~
    0 error(s), 0 warning(s)

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    Re: javascript and php

    Well, in PHP you can grab the whole query string using $_SERVER['QUERY_STRING'].
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  11. #11
    Join Date
    Aug 2003
    Posts
    162

    Re: javascript and php

    >.< thanks alot for your help ... appreciate it. haha have just learn php for 3wks

    will try to continue my work and post if face any more problem thankS!
    0 error(s), 0 warning(s)

  12. #12
    Join Date
    May 2002
    Posts
    10,943

    Re: javascript and php

    You're welcome.

    If you have more problems, create a new thread. Each thread should be a different problem.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured