doumowu7371
doumowu7371
2015-10-26 03:51

PHP语法中的JavaScript

已采纳

I think this is a simple problem of my not knowing my syntax very well yet, but I am trying to get a js variable in a PHP, which actually happens to be in another js. I keep getting unterminated string literal error amoungst others general not working errors.

Code is as follows taking out the unnecessary

<script>
        jQuery(document).ready(function() { 
                var caNumber = document.getElementById("doc_id").value;                         
                var uploader = new plupload.Uploader({
                    url: '<?php echo admin_url('admin-ajax.php?project='.$_GET["project"].'&action='.$_GET["action"].'&number=caNumber') ?>'
                 });};
</script>

The problem in question is how to get the CaNumber in the url: where I have show CaNumber.

Thank you for time and assistance.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • dongqiongzheng0615 dongqiongzheng0615 6年前

    You are merging both php and javascript. Keep them separate.

    url: "<?php echo admin_url('admin-ajax.php?project='.$_GET["project"].'&action='.$_GET["action"].'&number= ')?>"+caNumber;
    
    点赞 评论 复制链接分享
  • douguan8940 douguan8940 6年前

    As an additional note of equal importance, do NOT blindly dump PHP variables into a JavaScript string. Doesn't matter how trustworthy the source of that PHP variable is. In this case, you're using $_GET variables in it, which means you're opening the door wide for attacks - a malicious user can force the victim's browser to execute any JavaScript at all through the URL.

    Instead, use...

    url: <?php echo json_encode(admin_url(......)); ?> + caNumber;
    

    This will handle all kinds of escaping and wrapping the string in quotes for you, thus closing the security hole.

    点赞 评论 复制链接分享
  • doudi5892 doudi5892 6年前

    Keep caNumber out of your PHP statement, where it is unknown and has no meaning. Append it, with JS, to the string output by PHP:

    jQuery(document).ready(function() { 
      var caNumber = document.getElementById("doc_id").value;
      var upUrl = "<?php echo admin_url('admin-ajax.php?project='.$_GET["project"].'&action='.$_GET["action"].'&number=') ?>" +
          caNumber;
    
      var uploader = new plupload.Uploader({
        url: upUrl
      });
    };
    
    点赞 评论 复制链接分享
  • dpqjvoq9033 dpqjvoq9033 6年前

    You can't put a JavaScript variable into PHP code in that manner. Keep in mind that PHP is a server-side language whereas JavaScript is a client-side language. Meaning, they are executed on two different machines. - By the time JavaScript code is evaluated, the PHP code has long since completed it's job.

    If you want to get values from JavaScript to PHP, you need to send them with a new request (AJAX, usually), or by using something like the more modern WebSockets feature.

    However given that the PHP code is simply generating an URL, you might try adding a placeholder in the URL it returns, and then replacing it with your JavaScript value in the JS code.

    Example:

    <script>
        var cat = document.getElementById("catElem").value;
        var url = "<?php echo "/file.php?cat={{CAT}}"; ?>";
        url = url.replace("{{CAT}}", cat);
    
        console.log(url);
    </script>
    
    点赞 评论 复制链接分享