Convert from curl to ajax

chris Source

I would like to get some help with my ajax code. I want to move the curl code in the index.html to work with ajax but I don't know how I can convert from curl to ajax code.

Index.html:

<script>
$button.click(function(e) 
{
   e.preventDefault();
   var emptyMail = true;
   var email = $emailInput.val().trim();
   var $emailInput_1 = $("#email").val();    

   $(document).ready(function()  
       $(this).val("SUBMITTING...");
       $("form").submit();

       $.ajax({
         url: "post.php",
         type: 'POST'
        });
    });
</script>

Here is the post.php

<?php
function _curl($url, $post = "", $headers = "")
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POST, ($post && is_array($post))? 1 :0);
    if ($post && is_array($post))
    {
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    }
    if ($headers && is_array($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/dev/null");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch);
    return $result;
    curl_close($ch);
}
if ($_POST)
{
    _curl("http://example.com/land/formFillReturnV5b.php", array(
    "name"=>"groups_name",
    "email"=>$_POST['email'],
    "phone"=>"",
    "ip"=> $_SERVER['SERVER_ADDR'],
    "reff"=>"",
    "page"=>"Q6",
    "link"=>"https://example.com/meme",
    "notes"=>"",
    "MID"=>"39918",
    "LID"=>"",
    "ARData"=>"meme",
    "cookie"=>""), array("X-NewRelic-ID" => "VQQBVl9aDRABUFJbAQkOUQ==", "X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"));
}
?>

I want to make it like this:

<script>
    $button.click(function(e) 
    {
       e.preventDefault();
       var emptyMail = true;
       var email = $emailInput.val().trim();
       var $emailInput_1 = $("#email").val();    

       $(document).ready(function()  
           $(this).val("SUBMITTING...");
           $("form").submit();

           $.ajax({
             url: "http://example.com/land/formFillReturnV5b.php",
             type: 'POST',
             "name"=>"groups_name",
             "email"=>$_POST['email'],
             "phone"=>"",
             "ip"=> $_SERVER['SERVER_ADDR'],
             "reff"=>"",
             "page"=>"Q6",
             "link"=>"https://example.onlinesalespro.com/meme",
             "notes"=>"",
             "MID"=>"39918",
             "LID"=>"",
             "ARData"=>"meme",
             "cookie"=>""), array("X-NewRelic-ID" => "VQQBVl9aDRABUFJbAQkOUQ==",      "X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"));
            });
        });
    </script>

When I tried it, it won't work. If you can show me an example how I can use ajax code to send the data to the server that would be great.

javascriptphpajaxcurl

Answers

answered 7 months ago Mykhailo Yatzyshyn #1

Used Documentation

Example code

<script>
$button.click(function(e) 
{
   e.preventDefault();
   var emptyMail = true;
   var email = $emailInput.val().trim();
   var $emailInput_1 = $("#email").val();    

   $(document).ready(function()  
       $(this).val("SUBMITTING...");
       $("form").submit();

       $.ajax({
         url: "http://example.com/land/formFillReturnV5b.php",
         type: 'POST',
         data: {name: "groups_name", email: "[email protected]" /* and other parametrs*/ }
         success: function(result) {
          console.log(result);
         }
        });
    });
</script>

comments powered by Disqus