-1

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.

chris
  • 41
  • 7
  • 2
    So look at the jQuery documentation for [data](http://api.jquery.com/jquery.ajax/) and the cookie should be set in the browser and sent to the server by default. – epascarello Feb 13 '18 at 15:56
  • 1
    "Won't work" is a useless problem statement. What happens? Are there errors in the javascript console? Have you looked at your browser's developer tools "network" tab? What is shown there? – random_user_name Feb 13 '18 at 15:56
  • 5
    There are too many things wrong here to know where to begin (voting to close as "too broad"). It doesn't help that you don't appear to understand JavaScript syntax and are trying to do a couple of things that are flat out impossible to do with client-side JS. You should read introductory tutorials for JavaScript, jQuery, and Ajax. – Quentin Feb 13 '18 at 15:56
  • try: https://stackoverflow.com/questions/6085649/how-to-send-multiple-data-fields-via-ajax/6085669#6085669 – Avitus Feb 13 '18 at 15:57
  • 2
    Tip: When programming, start with the smallest possible thing to get it working. For you, that's making a _simple AJAX call_ (NOT a complex one, and NOT to an off-site URL). Then, slowly add the features you need, one at a time, and when it breaks, you'll know _exactly why_, because it was the last thing you added. – random_user_name Feb 13 '18 at 15:58

1 Answers1

0

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: "text@email.ru" /* and other parametrs*/ }
         success: function(result) {
          console.log(result);
         }
        });
    });
</script>
  • thank you very much for this so what about the cookie? – chris Feb 13 '18 at 18:01
  • On my experience with Ajax, there was never a problem with passing cookies (if you make a request to your server and not to the server) they are always passed automatically. About all the other parameters that you transfigure in the headers look in the documentation or google – Mykhailo YATSYSHYN Feb 15 '18 at 14:25