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
3
  • Refer this stackoverflow.com/questions/9328743/… Commented Nov 9, 2017 at 10:35
  • var positions = [$("#user1").val(), $("#user2").val()]; and data: {'positions':positions} Commented Nov 9, 2017 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; Commented Nov 9, 2017 at 11:04

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.