开发者

Form auto submit ajax not working

I'm pa开发者_JS百科ssing some variable from one file to another php file that contains a form via 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({
   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'

However, your form Id is "publish"

This is probably the cause of the code not working.


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.

You could look into sending the form using PHP Curl instead of 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? If so, then window.onload will not be fired since the page was already loaded (AJAX doesn't count). Just do this:

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


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
  • 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.

To get this working, you'll need to do a few things.

  1. Parse out the incoming javascript from the html.
  2. Inject the incoming parsed HTML into SourcePage.php's markup.
  3. Pass the parsed out JavaScript into JavaScript's eval function.

Doing this should bring the page from the process.php and successfully execute the JavaScript code on SourcePage.php.

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. Perhaps a redirect on the server will accomplish your goal (whatever that may be).

Original

Try this out: http://jsfiddle.net/NiceGuy4263/eJLMS/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜