0

This is my first time to experience this. I have a jquery script that submit form data to the server(PHP). This script work perfectly while I was using another template but on switching to another template the script stop working correctly. In my effort to know what exactly is wrong I noticed that the $.post is not sending any data to the url.

        <script>
  $(document).ready(function() {
  $("#addvotemate").click(function() {
    var vuniq_userid = $("#vuniq_userid").val();
    var vmuniq_userid = $("#vmuniq_userid").val();
   //alert("I work to this point");
    if (vuniq_userid == '' || vmuniq_userid == '' ) {
   alert("Request Failed Try Again");
    } else {
   // alert(vuniq_userid +' '+ vmuniq_userid);

     $.post("<?php echo site_url('user/addvotemate'); ?>", {
       vuniq_userid1: vuniq_userid,
       vmuniq_userid1: vmuniq_userid


      }, function() {
     alert("Message Sent");


      $('#votreq')[0].reset(); // To reset form fields

    });
    }
    });
   });
   </script>

Am using Codeigniter 3X, Template bootstrap 3, jquery.1.11.1.min.js View:

      <div id="votm">
                    <form id="votreq" name="form" method="POST">

                        <input type="hidden" id="vuniq_userid" 
        name="vuniq_userid" value="<?php echo $_SESSION['uniqueID'] ?>">
                        <input type="hidden" id="vmuniq_userid" 
    name="vmuniq_userid" value="<?php echo $pid ?>" >

                        <btn class="btn btn-sm btn-azure btn-icon" 
   id="addvotemate"><i class="fa fa-fw fa-users"></i> Add Votemate</btn>
                    </form>
                </div>

I need your kind help as I don't even understand how to solve the problem. I have tried different approach but still no luck. I have use jsfidle. Thanks

SholleyonlineJava
  • 77
  • 1
  • 2
  • 12
  • Can you please try to capture the error or use an alternative to $.post shown below: https://stackoverflow.com/a/2833968/3254405 – boateng May 30 '18 at 19:34
  • There is no error message. But if there is a way to do that, kindly put me through. – SholleyonlineJava May 30 '18 at 20:05
  • Can you use your browser's developer's tool to see what, if any, URL `$post()` is calling? – DFriend May 30 '18 at 20:07
  • Yes, I think it does. I have these: "http://[::1]/votemate/index.php/user/addvotemate" – SholleyonlineJava May 30 '18 at 20:15
  • 2
    The `[::1]votemate/` part tells me you have not set a value for `$config['base_url']` – DFriend May 30 '18 at 20:22
  • @DFriend Thanks, I have done that now like this:$config['base_url'] = 'http://localhost/votemate/'; So I check and saw this: http://localhost/votemate/user/addvotemate – SholleyonlineJava May 31 '18 at 01:42
  • @DFriend The problem not solved yet. – SholleyonlineJava May 31 '18 at 01:48
  • The value should include the protocol, i.e. `$config['base_url'] = 'http://localhost/';` If you have given you virtual host a server name then use that instead of `localhost`. – DFriend May 31 '18 at 03:45
  • @DFriend That's exactly what I did. Is stackoverflow that is stripping the http off here. – SholleyonlineJava May 31 '18 at 06:01
  • You should be using the codeigniter form method. Form_open(user/addvotemate, $args) – Brad Jun 01 '18 at 01:54
  • @Brad I am trying to prevent the whole page refresh and make it as dynamic as possible. The only way to get that done is by using Ajax. – SholleyonlineJava Jun 01 '18 at 16:03
  • using the codeigniter form is just how you structure the form not use ajax. It is much more secure. The $args is an array of your form ID that triggers ajax – Brad Jun 02 '18 at 11:39
  • @Brad Ok, can you show me an examle of how to use $args – SholleyonlineJava Jun 03 '18 at 13:03
  • I will put it in the answer box, but you dont have to treat it as an answer – Brad Jun 04 '18 at 01:02
  • @DFriend I noticed this is being added automatically to my form. Hoping this could be the reason for the problem. Do I have to create a row for it in my database? – SholleyonlineJava Jun 04 '18 at 03:36
  • Not sure it's your problem. If it is then the response from the ajax call will be a 403 Not allowed error. The field is the CSRF protection field and the value will be stripped from `$_POST` data before it gets to the controller. So, no you do not need to store it in your database. Read about [CSRF here.](https://www.codeigniter.com/user_guide/libraries/security.html#cross-site-request-forgery-csrf) – DFriend Jun 04 '18 at 16:52
  • @DFriend No, I did not store it in my database but I include it in my Ajax script since its an input with value and it worked. You are very correct. Thanks – SholleyonlineJava Jun 05 '18 at 12:05

2 Answers2

1

Using codeigniter forms

$args = [
'id' => 'votreq',
'name' => 'form',
];
echo form_open('user/addvotemate', $args);

This replaces:

<form id="votreq" name="form" method="POST">

This automatically escapes your form inputs. Much more secure. Please use the codeigniter manual, its very helpful.

Brad
  • 1,685
  • 1
  • 27
  • 38
0

This is how I figured it out. Simply because am using the codeigniter CSRF Security feature, an hidden input field is being added to my form:

    <input type="hidden" name="csrf_test_name" 
     value="cc0294deffcc52f34bcdbcxxxxxxxxx" />

I detected this through the developer tool of firefox browser debugger. The following is my initial script which does not include the csrf hidden field:

     <script>
      $(document).ready(function() {
      $("#addvotemate").click(function() {
      var vuniq_userid = $("#vuniq_userid").val();
     var vmuniq_userid = $("#vmuniq_userid").val();
    //alert("I work to this point");
    if (vuniq_userid == '' || vmuniq_userid == '' ) {
    alert("Request Failed Try Again");
     } else {
   // alert(vuniq_userid +' '+ vmuniq_userid);

 $.post("<?php echo site_url('user/addvotemate'); ?>", {
   vuniq_userid1: vuniq_userid,
   vmuniq_userid1: vmuniq_userid


  }, function() {
 alert("Message Sent");


  $('#votreq')[0].reset(); // To reset form fields

  });
    }
    });
    });
  </script>

So, I decided to include the hidden field in my script like this:

      <script>
       $(document).ready(function() {
       $("#addvotemate").click(function(e) {

       e.preventDefault();
       var csrf = $('[name="csrf_test_name"]').val();    
        var vuniq_userid = $("#vuniq_userid").val();
       var vmuniq_userid = $("#vmuniq_userid").val();

           if (vuniq_userid == '' || vmuniq_userid == '' ) {
           alert("Request Failed Try Again");
          } else {

         $.post("<?php echo site_url('user/addvotemate'); ?>", {
        csrf_test_name: csrf,
         vuniq_userid1: vuniq_userid,
        vmuniq_userid1: vmuniq_userid

            }, function() {
           alert("Message Sent");


            $('#votreq')[0].reset(); // To reset form fields

         });
            }
       });
      });
     </script>

After I did that my Ajax script begins to work properly.

SholleyonlineJava
  • 77
  • 1
  • 2
  • 12