简体   繁体   English

如何在不使用HTML的情况下发送AJAX表单 <form> 标签

[英]How to send AJAX form without using HTML <form> tag

I want to send data to my PHP using AJAX but without using the HTML <form> tag because in my case if I have the form I have a loop of sending the form and AJAX PHP came into looping so I think I would not use form but how can I apply these if AJAX had a method of POST ? 我想使用AJAX将数据发送到我的PHP,但不使用HTML <form>标记,因为在我的情况下,如果我有表单,则有一个发送表单的循环,而AJAX PHP进入了循环,所以我认为我不会使用表单但是,如果AJAX具有POST方法,该如何应用?

$(document).on('click', '#myButton', function(){  
    $.ajax({
        url: "insert.php",
        method: "post",
        data:{data},    
        dataType:"text",
        success:function(data){
            alert('Successfully')
        }
    })
})

and my HTML is: 而我的HTML是:

<div class = "myDiv1">
      <input type = "text">
      <input type = "button" id = "myButton">
</div>

Is there a possible way to do this? 有没有办法做到这一点? I'm just asking. 我只是问问。 Thank you :) 谢谢 :)

You can assign values to data in ajax like 您可以为ajax中的数据分配值,例如

var formData = {
value1 = $(element1).val();
value2 = $(element2).val();
// and so on
}

Pass the formData to ajax 将formData传递给ajax

Javascript 使用Javascript

$(document).on('click', '#myButton', function(){  
    $.ajax({
        url: "insert.php",
        method: "post",
        data:{
            text: $('.text-value').val()
        },    
        dataType:"text",
        success:function(data){
            alert('Successfully')
        }
    })
})

HTML HTML

<div class = "myDiv1">
    <input type = "text" class="text-value">
    <input type = "button" id = "myButton">
</div>

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

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