Passing data to server in remote validation

你说的曾经没有我的故事 提交于 2019-12-25 07:13:45

问题


Using jquery validation with remote on one of the fields. How do I send the data to the server of what the user entered into the field?

rules: { 
      "profile.userId": {
       required: true,
       minlength: 8,
       remote: {
                url: "/checkUniqueUserId",
                dataType: "json",
                type: "POST",
                data: {userId : '???'}
       }
 }

How do I extract the entered value in the input field? I've tried:

  • $("#userId").val()
  • userId
  • profile.userId
  • $(this).val()

Any suggestions?

This is in my jsp:

<div class="control-group">
   <div class="form-group has-success has-feedback">
      <form:input type="text" id="userId" name="userId" class="form-control" 
      path="profile.userId" placeholder="user name"/>
   </div>
</div>

回答1:


Quote OP:

"How do I send the data to the server of what the user entered into the field?"

You don't, because that is already sent by default.

Your code...

rules: { 
    "profile.userId": {  // <- this MUST be the 'name' attribute of the input
        required: true,
        minlength: 8,
        remote: {
            url: "/checkUniqueUserId",
            // dataType: "json",      // <- not needed, default
            type: "POST",
            // data: {userId : '???'} // <- not needed
        }
    }
}

If you just want the value of your profile.userId field, you don't need to do anything. That's the exact data already sent by default.

Using PHP as an example, it is simply accessed server-side with the $_POST array as $_POST['profile.userId']. (This assumes the rendered <input> element contains the name="profile.userId" attribute, otherwise none of this will work.)

You would only use the data option if you need to send additional data to the remote script. Example, to send an email address along with the user id.

See the documentation for more information and examples.




回答2:


In somecase example: edit email user you can use data:{}

rules: {

            txt_email: {
                required: true,
                email: true,
                remote: {
                    url  : "emailcheck.php",
                    type : "POST",
                    data: {type:'edit',old_email:$( "#old_email" ).val()}
                }
            }

}

in emailcheck.php handle

if($_POST['type'] == "edit") {
    // your business code
}


来源:https://stackoverflow.com/questions/24314813/passing-data-to-server-in-remote-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!