简体   繁体   English

javascript按钮ajax和php

[英]javascript button ajax and php

I have a small question, which I guess in my opinion will sound stupid. 我有一个小问题,我想我觉得这听起来很愚蠢。 I have actually this code 我实际上有这个代码

<script type="text/javascript">
$(document).ready(function(){

var email_value = prompt('Please enter your email address');
if(email_value !== null){
//post the field with ajax
$.ajax({
    url: '="/new/cfolder.php',
    type: 'POST',
    dataType: 'text',
    data: {data : email_value},
    success: function(response){ 
     //do anything with the response
      console.log(response);
    }       
}); 
}

});
</script>

I would like to link it to my button which does this 我想将它链接到我执行此操作的按钮

<form action="/new/cfolder.php" method="post">
    </font><input type="submit" value="Create Vdisk" class="myButton6" onClick="function()"> 
<br></form>

Is it actually possible to do this? 实际上可以这样做吗? thank you for any answer. 谢谢你的回答。

 <script type="text/javascript">
  $(document).ready(function(){
    $(document).on('click', '.myButton6', function(){
      var email_value = prompt('Please enter your email address');
      //post the field with ajax
      if(email_value.length > 0){
        $.ajax({
            url: '/new/cfolder.php',
            type: 'POST',
            dataType: 'text',
            data: {data : email_value},
            success: function(response){ 

              alert(response);
            }       
        }); 
      }
    )};
  }); 
</script>

try that way 试试这种方式

// this is the id of the form
$("#idForm").submit(function() {

var url = "path/to/your/script.php"; // the script where you handle the form input.

$.ajax({
       type: "POST",
       url: '="/new/cfolder.php',
       data: $("#idForm").serialize(), // serializes the form's elements.
       success: function(data)
       {
           alert(data); // show response from the php script.
       }
     });

return false; // avoid to execute the actual submit of the form.
});

give your form an id 给你的表格一个id

<form id="idForm" action="/Same/path/as/your/ajax/" method="post">
</font><input type="submit" value="Create Vdisk" class="myButton6" onClick="function()"> 
<br></form>
Yes of course you can do this . Like  this code and you will have to include jquery1.9.0.js or above.


 <script type="text/javascript">
    $(document).ready(function(){
    $("#myButton6").click(fuction(){
    var email_value = prompt('Please enter your email address');
    if(email_value !== null){
    //post the field with ajax
    $.ajax({
        url: '/new/cfolder.php',
        type: 'POST',
        dataType: 'text',
        data: {data : email_value},
        success: function(response){ 
         //do anything with the response
          console.log(response);
        }       
    }); 
    }

    });

    });
    </script>
    I would like to link it to my button which does this


 </font><input type="submit" value="Create Vdisk" class="myButton6" id="myButton6"> 
    <br>

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

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