简体   繁体   English

表单自动提交 ajax 不起作用

[英]Form auto submit ajax not working

I'm passing some variable from one file to another php file that contains a form via jQuery ajax.我将一些变量从一个文件传递到另一个 php 文件,该文件包含通过 jQuery ajax 的表单。 On The form page where data is being passed to have the following code in it, The values are getting passed in properly and and fields are getting populated with the correct entries, i'm able to very this with firebug response, but page is not automatically submitting.在传递数据以在其中包含以下代码的表单页面上,值正在正确传递,并且字段正在填充正确的条目,我可以通过萤火虫响应来做到这一点,但页面不是自动提交。 Is their anything i should be looking for that is preventing form from auto submitting.他们有什么我应该寻找的东西是防止表单自动提交。 If i access the form page directly, i can see auto submit works.如果我直接访问表单页面,我可以看到自动提交作品。

<?php 
$title = $_POST['title'];
$wrapper = $_POST['wrapper'];?>

<form action="test.php" method="post" id="publish">
  <input type="text" value="<?php echo $title ?>" name="title">
  <textarea name="wrapper"><?php echo $wrapper?></textarea>
  <input type="submit" value="Submit">
</form>

<script>
 window.onload = function(){
 document.getElementById('publish').submit();
}
</script>

ajax code that is sending the values looks like this发送值的 ajax 代码如下所示

$.ajax({
   type: "POST",
   url: "process.php",
      data: {
     title: 'test',
     wrapper: 'testing123'
     },
   success: function(msg){
     alert( "Data Saved: " + msg );
   } 
});

Spot the difference:指出不同:

getElementById('publishForm')

id="publish"

From what I see the auto submit is linked to the 'publishForm'从我看到的自动提交链接到'publishForm'

However, your form Id is "publish"但是,您的表单 ID 是“发布”

This is probably the cause of the code not working.这可能是代码不起作用的原因。

EDIT编辑

To break this down:要打破这一点:

  • Your code on SourcePage.php(I made up this name for reference) is posting data to process.php via an AJAX request您在 SourcePage.php 上的代码(我编了这个名称以供参考)正在通过 AJAX 请求将数据发布到 process.php
  • process.php then injects "title" & "wrapper" into the html markup and returns html with some javascript to SourcePage.php process.php then injects "title" & "wrapper" into the html markup and returns html with some javascript to SourcePage.php
  • You're then expecting that displaying the resulting string (msg) of the returned html on SourcePage.php will get the javascript in that string to execute.然后,您期望在 SourcePage.php 上显示返回的 html 的结果字符串 (msg) 将在该字符串中执行 javascript。

To get this working, you'll need to do a few things.要使其正常工作,您需要做一些事情。

  1. Parse out the incoming javascript from the html.从 html 中解析出传入的 javascript。
  2. Inject the incoming parsed HTML into SourcePage.php's markup.将传入的解析 HTML 注入到 SourcePage.php 的标记中。
  3. Pass the parsed out JavaScript into JavaScript's eval function.将解析出来的 JavaScript 传递给 JavaScript 的eval function。

Doing this should bring the page from the process.php and successfully execute the JavaScript code on SourcePage.php.这样做应该会从 process.php 中获取页面,并在 SourcePage.php 上成功执行 JavaScript 代码。

If you were expecting that the JavaScript would run on the server, then I'm afraid you're mistaken as the server(php runtime) will not execute the JavaScript on the server.如果您期望 JavaScript 将在服务器上运行,那么恐怕您误认为服务器(php 运行时)不会在服务器上执行 JavaScript。 Perhaps a redirect on the server will accomplish your goal (whatever that may be).也许服务器上的重定向将实现您的目标(无论是什么)。

Original原来的

Try this out: http://jsfiddle.net/NiceGuy4263/eJLMS/试试这个: http://jsfiddle.net/NiceGuy4263/eJLMS/

Perhaps you should show us the caller code instead of the handler code.也许您应该向我们展示调用者代码而不是处理程序代码。 Most likely what you're dealing with is the JS not being run during the AJAX call - the PHP page processing is server side.您正在处理的最有可能是在 AJAX 调用期间未运行 JS - PHP 页面处理是服务器端的。

You could look into sending the form using PHP Curl instead of JS?您可以考虑使用 PHP Curl 而不是 JS 发送表单? That would probably address the issue where it works loaded directly, but fails when called from another page.这可能会解决它直接加载但从另一个页面调用时失败的问题。

As far as I understood, that HTML is being loaded through AJAX, right?据我了解,HTML 正在通过 AJAX 加载,对吧? If so, then window.onload will not be fired since the page was already loaded (AJAX doesn't count).如果是这样,那么 window.onload 将不会被触发,因为页面已经加载(AJAX 不计算在内)。 Just do this:只需这样做:

<script type="text/javascript">
   document.getElementById('publish').submit();
</script>

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

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