Отправка формы Ajax, как заставить это работать - PullRequest
0 голосов
/ 30 мая 2011

Помогите, я пытаюсь получить форму для отправки - стиль ajax.

По сути, я хотел бы иметь возможность "отправить" форму на другую страницу, не перенаправляя пользователя на вторую страницу. В идеале на странице форм должно отображаться какое-либо сообщение об успехе или неудаче.

Код ниже не работает. Я не уверен, в чем проблема.

Я протестировал свой process.php, разместив и перенаправив пользователя на страницу, и он работает безупречно. Любой совет, что я могу сделать, чтобы заставить это работать с ajax? (Я использую jquery)

<script>
    $(function() { // wait for the DOM to be ready
    $('#personal_email').submit(function() { // bind function to submit event of form
       $.ajax({
          type: $(this).attr('method'), // get type of request from 'method'
          url: $(this).attr('action'), // get url of request from 'action'
          data: $(this).serialize(), // serialize the form's data
          success: function(responseText) {
              // if everything goes well, update the div with the response
              $('#result').html(responseText);
          }
      });
    return false; // important: prevent the form from submitting
    });
   });
</script>

....

<form  name="personal_email" id="personal_email" action="proccess.php" method="post">
 <fieldset>
     <input type="text" id="newsletteremail" name="newsletteremail" value="my email address" onfocus="if(this.value=='my email address')this.value=''"/>
     <input type="submit" value="Subscribe" />
   </p>
 </fieldset>
</form>

1 Ответ

0 голосов
/ 30 мая 2011

У меня работает следующее:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" ></script>
<script type="text/javascript">
    $(document).ready(function() { // wait for the DOM to be ready
    $('#personal_email').submit(function() { // bind function to submit event of form
       $.ajax({
          type: $(this).attr('method'), // get type of request from 'method'
          url: $(this).attr('action'), // get url of request from 'action'
          data: $(this).serialize(), // serialize the form's data
          success: function(responseText) {
              // if everything goes well, update the div with the response
              $('#result').html(responseText);
          }
      });
    return false; // important: prevent the form from submitting
    });
   });
</script>
</head>
<body>
<div id="result"></div>
<form  name="personal_email" id="personal_email" action="process.php" method="post">
 <fieldset>
     <input type="text" id="newsletteremail" name="newsletteremail" value="my email address" onfocus="if(this.value=='my email address')this.value=''"/>
     <input type="submit" value="Subscribe" />
   </p>
 </fieldset>
</form>
</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...