2011-08-01 4 views
2

이 모든 페이지를 새로 고치지 않고이 양식을 자동으로 제출하려면 어떻게합니까?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <title>JQuery Form Example</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script><!--need at least one instance of jquery--> 
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script><!--ajax to use with jquery--> 
    <script type="text/javascript"> 
    $(document).ready(function(){//when the document has loaded 
     $("#myform").validate({//Q: what is validate?, myform gets validated 
      debug: false, 
      rules: { 
       name: "required",//name and email are the only things passed 
       email: { 
        required: true, 
        email: true//I guess this defines the input as an email 
       } 
      }, 
      messages: {//at what point is this message displayed? 
       name: "Please let us know who you are.", 
       email: "A valid email will help us get in touch with you.", 
      }, 
      submitHandler: function(form) {//submit the form 
       // do other stuff for a valid form 
       $.post('process.php', $("#myform").serialize(), function(data) {//Q:what is serialization? myform gets serialized and assigned an action 
        $('#results').html(data);//what is this? 
       }); 
      } 
     }); 
    }); 

    window.onload=function() 
    { 
    } 


    </script> 
    <style> 
    label.error { width: 250px; display: inline; color: red;} 
    </style> 
</head> 
<body> 

<form name="myform" id="myform" action="" method="POST"> 
<!-- The Name form field --> 
    <label for="name" id="name_label">Name</label> 
    <input type="text" name="name" id="name" size="30" value="Charlie"/> 
    <br> 
<!-- The Email form field --> 
    <label for="email" id="email_label">Email</label> 
    <input type="text" name="email" id="email" size="30" value="[email protected]"/> 
    <br> 
<!-- The Submit button --> 
    <input type="submit" name="submit" value="Submit"> 
</form> 
<!-- We will output the results from process.php here --> 
<div id="results"><div> 
</body> 
</html> 

<!--ok, so I want to first try some stuff out to get a feel for how the form works--> 
<!--upload the form to the site somewhere--> 
<!--access and play around with it--> 
<!--then figure out how to submit to multiple php forms--> 

그래서 나는 JQuery와 및 Ajax를 사용하고 PHP 파일에, 나는 자동으로 양식을 제출해야합니다.

나는 document.myform.submit과 같은 것을 사용하려고 시도했지만 작동하지 않는 것 같습니다. 그것은 아약스를 사용하지 않을 때 작동하지만, 그 페이지는 PHP 파일의 대상으로 새로 고치게됩니다.

저는 일부 구문이 누락되었다고 생각합니다. 검색을 수행하여 다음과 같은 몇 가지 스레드를 찾았습니다. Ajax auto form submit

그러나 저는 취미 애호가이기 때문에 더 이상 말하고 있지 않습니다. 나는 다른 사람들에게 내 코드를 수정하는 일을 맡기지 않을 것이지만, 나는 그 변화가 사소하다는 것을 확신하고 있으며, 이것을 알아 내려고 노력하고있다. 도움이되는 사람에게 많은 감사를드립니다! 방향을 자세히 설명해 주시면 코드 자체에서 직접 수정 사항을 제공 할 수 있다면 좋을 것입니다.

편집 : 내 의견에 너무 열심히 웃지 마세요. 코드를 이해하고 (많이 검색 한 후) 훨씬 좋았습니다. 몇 시간 전부터 수정되었습니다.

double edit : 여기 process.php라는 샘플 PHP 스크립트가 있습니다. 자동 제출이 제대로 작동하는지 보려면 편집해야합니다. 위에서 언급했듯이, 당신은 MySQL 데이터베이스로 이것을 할 수 있습니다.

print "Form submitted successfully: <br>Your name is <b>".$_POST['name']."</b> and your email is <b>".$_POST['email']."</b><br>"; 

답변

1

: 검증 무엇인가? myForm을가

를 확인됩니다

A : validate 메소드를 사용하면, 필드의 데이터가 유효한지 확인합니다 양식이 제출되기 전에.

Q :이 메시지는 언제 표시됩니까?

A : 유효성 검사가 통과하지 않는 경우. 예를 들어 이름을 "필수"로 지정했으며 전자 메일은 몇 줄 앞부분에 "전자 메일"이어야합니다.

Q : 직렬화 란 무엇입니까? myform이 직렬화되고 작업이 할당됩니다.

A : Serialize는 양식의 모든 필드 값을 읽고 GET 및 POST에 적합한 쿼리 문자열을 구성합니다. 귀하의 경우, 직렬화 후 POST 데이터가 될 것입니다 이름 = 찰리 & [email protected] "

Q :이

A는 무엇 :"데이터 "는 매개 변수로 전달됩니다 콜백 함수.이 경우 프로세스의 출력.PHP는 #의 innerHTML ('# 결과')에 기록 될 것입니다

+0

와우 감사합니다. 모든 답변이 작동한다고 생각하고 jquery 양식 플러그인을 좋아합니다. 나는 그것을 일찍 사용하고 있었다. 이 답변은 가장 간결하지만 직렬화에 대해 좀 더 자세히 가르쳐 줬습니다. 나는 폼이 작동했다는 것을 알았고, 자동 제출을하지 않았고, 다음 번에이 질문을 할 때 좀 더 구체적이고 덜 피곤하도록 노력할 것입니다. 감사! – obesechicken13

1

당신은 양식이 제출 될 때 확인() 메소드를 제출합니다. jQuery를 $ ('양식')를 사용할 수 있습니다, 그리고 당신은 모든 AJAX 물건을 한 후, 당신은 return false; 기본 제출을 방지합니다. 나는이

<script> 

$(document).ready(function(){ 
     submit_form(); 
}); 

function submit_form() { 
    $.post('process.php', $("#myform").serialize(), function(data) { 
     $('#results').html(data); 
    }); 
} 

</script> 

Q 작동합니다 생각 최소한의 노력에 대한

1

이 jQuery 플러그인 페이지를 새로 고치지 않고

$('#myform').submit(function() 
{ 
    $('#myform').ajaxForm(); 
    return false; 
}); 

과 양식이 submited됩니다 http://malsup.com/jquery/form/

그냥 간단한 작업을해야한다.

1

스크립트가 제대로 작동하고 있습니다. 나는 그것을 테스트하고 잘 작동합니다.

$("#myform").submit(); 

그것은 페이지로드에 자동으로 양식을 제출합니다 는 기능을 windo.onload 자동으로이 줄을 추가해야합니다 양식을 제출하십시오.

페이지를 테스트하고 양식 제출 결과를 보려면 간단한 html 파일 (일부 단어 포함)에 페이지를 제출 한 다음 PHP 파일로 이동하는 것이 좋습니다. (내 경우 텍스트 파일 만 단어 안녕 target.html 자세한 및 (가) 포함되어 있습니다) 페이지에서

submitHandler: function(form) {//submit the form 
    // do other stuff for a valid form 
    $.post('target.html', $("#myform").serialize(), function(data) {//Q:what is serialization? myform gets serialized and assigned an action 
     alert(data); 
     $('#results').html(data);//what is this? 
    }); 
} 

: 당신이 페이지를 제출하는 HTML 파일의 이름에 따라

그리고 당신을 변경 sumbmitHandler 당신은 당신의 HTML 페이지의 내용을보고 경고 할 것이고, div 결과는 또한이 내용으로 채워질 것입니다.

도움이 되었기를 바랍니다.

주세요.

관련 문제