Processing a PHP page using AJAX in MVC

I have a form, and I want to add AJAX functionality to this form.

Architecture: MVC

I have a basic form:

<form id="customForm" method="post"> <input type="text" id="name" name="zone_name" value="" class="input-block-level" /> <button type="submit" id="save" >Save</button> </form> 

I have JS on my MVC-View :

 $('#save').click(function() { var name = $('#name').val(); $.ajax ({ type: 'POST', url: 'http://localhost/myApp/process', data: "{name:"+name+"}", success: function (result) { alert(result); }, error: function () { alert('fail'); } }); }); 

I have a class process , which is in the controller and has this code inside

  class process {
     function __construct () {
         echo 'Constructor';
     }
 }

But all this gives me an Error message via AJAX. Why is this happening? Is there any other way to do this. Below is a snapshot:

enter image description here

Here is my .HTACCESS rule

  RewriteCond% {REQUEST_FILENAME}! -D
 RewriteCond% {REQUEST_FILENAME}! -F
 RewriteCond% {REQUEST_FILENAME}! -L

 RewriteRule ^ (. +) $ Index.php? Url = $ 1 [QSA, L]

Therefore, when I am directly in my process class, it works. But not with AJAX. What would be a possible reason for this? Below is a screenshot:

enter image description here

+4
source share
2 answers

your button should not send the form, enter its button like:

 <button type="button" id="save" >Save</button> 
+2
source

What is ajax status code? Does this cause an error message? The status code can be obtained as:

 $('#save').click(function(e) { e.preventDefault(); //prevent default button action var name = $('#name').val(); $.ajax ({ type: 'POST', url: 'http://localhost/myApp/process', data: "{name:"+name+"}", success: function (result) { alert(result); }, error: function (xhr, status, errorMessage) { alert(xhr.status); } }); }); 

If the status code is zero, I’m denied access to the network. I see localhost in your ajax url and 192.168.1.6 in your warning window.

0
source

Source: https://habr.com/ru/post/1492885/


All Articles