简体   繁体   English

在javascript中结合php(未捕获的语法错误:无效或意外的令牌)

[英]Combining php in javascript (Uncaught SyntaxError: Invalid or unexpected token)

Currently, I want to retrieve data from the database by putting the php code in the javascript.目前,我想通过将 php 代码放在 javascript 中来从数据库中检索数据。 The javascript will have the form detail to ask user and one of the detail will require the data from the database.But the console prompt the error javascript将有表单详细信息询问用户,其中一个详细信息将需要数据库中的数据。但控制台提示错误

Uncaught SyntaxError: Invalid or unexpected token未捕获的语法错误:无效或意外的令牌

google.maps.event.addListener(marker,'click', function(event) {
                    //Edit form to be displayed with new marker
            infowindow.close();
            marker.setVisible(false);

            <?php

            ?>

                    var EditForm = '<p><div id="infowindow-content" class="marker-edit">'+
                    '<form action="ajax-save.php" method="POST" name="SaveMarker" id="SaveMarker">'+
                    '<label for="pName"><span>Place Name :</span><input type="text" name="pName" class="save-name" placeholder="Enter Title" maxlength="40" /></label>'+
                    '<label for="pDesc"><span>Description :</span><textarea name="pDesc" class="save-desc" value="place-address" maxlength="150"></textarea></label>'+'
                    <?php
            $db_username = 'root';
            $db_password = '';
            $db_name = 'mss';
            $db_host = 'localhost';
            $sql = "SELECT* FROM vendor";
            $result = mysqli_query($conn, $sql);
            while($row1=mysqli_fetch_assoc($result))
            {
             echo '<label for="pType"><span>Type :</span> <select name="pType" class="save-type"><option value ="'.$row1["vendorid"].'">"'.$row1["vendorid"].'"</option>';
            }   
    ?>'+
                    '</form>'+
                    '</div></p><button name="save-marker" class="save-marker">Save Marker Details</button>';

                    //Drop a new Marker with our Edit Form
                    create_marker(event.latLng, 'New Marker', EditForm, true, true, true, "pin_green.png", "");
                });   
          }

This error could be because of the PHP variable usage within the script tag without using the php tag.这个错误可能是因为在脚本标签中使用了PHP变量而不使用php标签。 If you want to use PHP variable inside the script, you must use "<?php ?>";如果你想在脚本中使用 PHP 变量,你必须使用"<?php ?>"; . .

For example, say you want to use php variable, $str = "My Name"; Then it will be:

<script>
   var name = "<?php echo $str; ?>";
</script>

Hope it solves the issue.希望它能解决问题。

Without seeing where your PHP tags start and the complete script, it's difficult to pin-point everything that may be going wrong, but there are a few things you can do to isolate and locate the error provided in the console.如果没有看到 PHP 标记的开始位置和完整的脚本,就很难确定可能出错的所有地方,但是您可以采取一些措施来隔离和定位控制台中提供的错误。 Here are some suggestions:以下是一些建议:

Remove your PHP blocks and check if you still have an uncaught syntax error .删除您的 PHP 块并检查您是否仍有未捕获的语法错误

 google.maps.event.addListener(marker,'click', function(event) { //Edit form to be displayed with new marker infowindow.close(); marker.setVisible(false); var EditForm = '<p><div id="infowindow-content" class="marker-edit">'+ '<form action="ajax-save.php" method="POST" name="SaveMarker" id="SaveMarker">'+ '<label for="pName"><span>Place Name :</span><input type="text" name="pName" class="save-name" placeholder="Enter Title" maxlength="40" /></label>'+ '<label for="pDesc"><span>Description :</span><textarea name="pDesc" class="save-desc" value="place-address" maxlength="150"></textarea></label>'+''+'</form>'+ '</div></p><button name="save-marker" class="save-marker">Save Marker Details</button>'; //Drop a new Marker with our Edit Form create_marker(event.latLng, 'New Marker', EditForm, true, true, true, "pin_green.png", ""); }); }

Assuming you shared the complete JavaScript block in your response, you have an extra curly bracket at the end, after your addListener method and anonymous function.假设您在响应中共享了完整的 JavaScript 块,则在 addListener 方法和匿名函数之后的末尾有一个额外的大括号。 How would you spot that error on your own?您如何自行发现该错误? If you open the code inspector or DevTools on Chrome (there's also an equivalent on Firefox), you'll notice that there are line numbers next to the error messages .如果您在 Chrome 上打开代码检查器或 DevTools(在 Firefox 上也有类似的工具),您会注意到错误消息旁边行号

If you select the line numbers, it will take you to the line that is causing the syntax error on your script, so you can better identify it.如果您选择行号,它会将您带到导致脚本语法错误的行,以便您更好地识别它。 For more information on how to use Chrome DevTools, refer to the following link: https://developers.google.com/web/tools/chrome-devtools/javascript/有关如何使用 Chrome DevTools 的更多信息,请参阅以下链接: https : //developers.google.com/web/tools/chrome-devtools/javascript/

If you use Firefox, here is their console debugging documentation: https://developer.mozilla.org/en-US/docs/Tools/Browser_Console如果您使用 Firefox,这里是他们的控制台调试文档: https : //developer.mozilla.org/en-US/docs/Tools/Browser_Console

I hope that helps!我希望这有帮助!

SIDE NOTE : Also, as pointed out by Aluan Haddad, your solution is very inelegant and does not apply a design principle called "separation of concerns."边注:另外,由Aluan哈达德指出,您的解决方案是非常不雅,不应用一个叫设计原理“的关注点分离。” You really should look into separating your HTML from your JavaScript and making calls to a separate PHP script by the use of AJAX.您确实应该考虑将 HTML 与 JavaScript 分开,并使用 AJAX 调用单独的 PHP 脚本。 This article should be a good starting point for you to look into how to implement that interaction in your code: https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript这篇文章应该是您研究如何在代码中实现该交互的一个很好的起点: https : //developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM