0

JS Code:

$('#addResults').submit(function(e) {
        var user1       = $("#user1").val();
            user2       = $("#user2").val();

        e.preventDefault();
         $.ajax({
             type: "POST",
             async: false,
             url: url,
             data: "form=addResults&user1=" + user1 + "&user2=" + user2
           }).success(function( msg ) {
                  $('.success').css("display", "");
                  $(".success").fadeIn(1000, "linear");
                  $('.success_text').fadeIn("slow");
                  $('.success_text').html(msg);
                  setTimeout(function(){location.reload()},1200);
          });

    });

submit.php

         case 'addResults':
            $positions         = $_POST['positions'];
             $ez->addResults($positions);
         break;

function.php

        function addResults($positions) {

         $this->link->query("INSERT INTO `" . $this->prefix . "result` SET race_id = '$league', event = '$event', involved_id = '$involved',

                    evidence_link = '$evidence', description = '$description', reporter_id = '$name', status = '0'");
         echo 'Results saved';
         return;
    }

My question: I'd like to store all Users data into an array and push this to the PHP function so I can insert it into the DB there.

Something like:

var positions= $("#user1").val(), $("#user2").val();

I'm not sure how I format the Data line.

data: "form=addResults&user1=" + user1 + "&user2=" + user2
Jay Zamsol
  • 1,173
  • 8
  • 14
Mitch M
  • 157
  • 11
  • Refer this https://stackoverflow.com/questions/9328743/sending-multiple-data-parameters-with-jquery-ajax – KMS Nov 09 '17 at 10:35
  • `var positions = [$("#user1").val(), $("#user2").val()];` and `data: {'positions':positions}` – Alive to die - Anant Nov 09 '17 at 10:36
  • How would I retrieve this in the PHP function? $positions seems empty when I try this solution. case 'addResults': $positions = $_POST['positions']; $ez->addResults($positions); break; – Mitch M Nov 09 '17 at 11:04

2 Answers2

0

in PHP:

$string = "business_type,cafe|business_type_plural,cafes|sample_tag,couch|business_name,couch cafe";

$finalArray = array();

$asArr = explode( '|', $string );

foreach( $asArr as $val ){
  $tmp = explode( '=', $val );
  $finalArray[ $tmp[0] ] = $tmp[1];
}

print_r( $finalArray );

so your JS output could be just one string:

data: "form=addResults&user1=" + user1 + "|user2=" + user2
Radek Mezuláník
  • 351
  • 1
  • 5
  • 15
0

The data you pass via ajax seems to be the url you want to send the datas. If you want to pass your data via the url, you should use GET method instead of POST.

In addition you don't use $position argument you passed to addResults in your function which is weird.

ultrajohn
  • 2,527
  • 4
  • 31
  • 56
K0d1Lu
  • 1
  • 2