'I am unable to post data using ajax on url friendly blog detailing page where I am already using url value in Query

I know that it may be so tricky!

In detail:

on the blog detailing page(blog-single.php/title) I have a subscription form this subscription form is working fine on another page with the same PHP action file and ajax

and blog-single.php/title is also working fine until I did not submit the form

On this page in the starting, I have bellow query

<?php
    $query_head="SELECT * FROM blog_post  WHERE friendly_url = '{$_GET['url']}' ";

    $result_head= $fetchPostData->runBaseQuery($query_head);

    foreach ($result_head as $k0 => $v0) 
   {
      //----- some echo
   }

?>

and my subscription form code:

<form action="" method="post" class="subscribe_frm" id="subscribe_frm2">
   <input type="email" placeholder="Enter email here" name="email" id="subscribe_eml2">
   <button type="button"  id="subscribe2">Subscribe</button>
</form>

and ajax code is bellow:

    $(document).ready(function() {

$("#subscribe2").click( function() {
  subscribe_frm_val2 = false;

        /*email  validation*/
        var emailReg2 = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        if ($("#subscribe_eml2").val().length <= 0) {
          $("#subscribe_eml_Err2").html("Required field");

          //console.log("Required");

           subscribe_frm_val2 = false;
        }
        else if(!emailReg2.test($("#subscribe_eml2").val()))
            {
              $("#subscribe_eml_Err2").html("Enter a valid email");
            }
        
        else{

          $("#subscribe_eml2").html("");
          subscribe_frm_val2 = true;
          //console.log("final else");
        
          if(subscribe_frm_val2 == true)
          {
            console.log("frm true");
            var form = $('#subscribe_frm2')[0];
            var data = new FormData(form);

            $.ajax({
                  type: "POST",
                  enctype: 'multipart/form-data',
                  url: "updation/subscribe_action.php",
                  data: data,
                  processData: false,
                  contentType: false,
                  cache: false,
                  timeout: 6000000,
              beforeSend: function(){
                  // Show image container
                  $("#submition_loader").show();
                  //alert ("yyy");
                 },

              success: function (data) {
                     // console.log();
                      $(document).ajaxStop(function(){
                          $("#subscribe_eml_Err2").html(data);
                      });
                  },
              complete:function(data){
                 // Hide image container
                 $("#submition_loader").hide();
               }
            }); 
          }
          else{
            alert('Please fill all required field !');
          }
        }
        

    });
});

When I submit my form above the first query is giving a warning like below: Warning: Invalid argument supplied for foreach() in D:\xamp\htdocs\my\bootstrapfriendly\category.PHP on line 13

and after warning page doing misbehave

I think the error because of URL passing but I am not sure how to solve it

Please help me with solving it. Thank's



Solution 1:[1]

I got the solution

its very simple just converted my relative URL into an absolute URL

I just created a PHP function for base URL

function base_url(){
    if(isset($_SERVER['HTTPS'])){
        $protocol = ($_SERVER['HTTPS'] != "off") ? "https" : "http";
    }
        else{
            $protocol = 'http';
        }
      return $protocol . "://" . $_SERVER['HTTP_HOST'];
    }

and then using this base URL function inside script like this

$.ajax({
    ----
       url: "<?php echo base_url()?>/updation/subscribe_action.php",
    -----
 }); 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abdul