简体   繁体   English

ajax 调用的 Php 文件未回显 javascript 代码

[英]Php file called by ajax isn't echoing javascript code

I'm trying to create a page where a user can upload a file and select the people they want to email it to.我正在尝试创建一个页面,用户可以在其中上传文件和 select 他们想要 email 的人。 Once they click submit, I prevent page refresh and reset their inputs in the form.一旦他们点击提交,我就会阻止页面刷新并重置他们在表单中的输入。 Now I want to place their previously entered information into a table on the same page (different section of the page).现在我想将他们之前输入的信息放入同一页面(页面的不同部分)上的表格中。

If you did want to proceed with this concept, you would capture the output of the PHP script in a done() function, insert it in an element on the page, and run eval() .如果您确实想继续这个概念,您可以在done() function 中捕获 PHP 脚本的 output,然后将其插入到元素页面上的eval() Like this...像这样...

$.ajax({
    type: "POST",
    url: "../FileDrop/dbSystem.php",
    data: {tags: JSON.stringify(tags), file: 
          $('input[name=fileName]').val()};
}).success(function(result) {
  $( '#element' ).html(result);
  $( '#element script' ).each( () => { $(this).html().eval() }) 
});

But it would make alot more sense to return data that you use to execute the javascript in your page - that is, keep all that logic together rather than splitting some of it off into a PHP file.但是,返回用于在页面中执行 javascript 的数据会更有意义 - 也就是说,将所有逻辑放在一起,而不是将其中一些逻辑拆分到 PHP 文件中。

<?php 
// php process that checks for 'valid'...
// create a json response
$output = array('valid' => 1);
echo json_encode($output);
?>

.... and in your JS ....在你的 JS 中

.success(function(result) {
  // result is a stringified JSON object. We need to convert it into an actual JSON object with parse()
  result = JSON.parse(result);
  // might not matter, but result.valid will be a number which JSON converts into a string. By adding the + right before the variable, that tells JS to use it as a number for the comparison
  if (+result.valid == 1) {
    $(".outputDiv").show();
  }
});

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

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