Php regular expression/string to array problem

It’s being sent from the javascript file:

// --------------------------------- start AJAX -------------------------------//

function postIt() {
alert(wstore);
var realname = myName;
var rightAnswers = correct;
var percentageScore = percentage;
var asked = numq;
var mins = minutes;
var secs = seconds;
var deliver = destination;
var testName = test;

  	dataString = wstore ; // array
  	var wrongArray = JSON.stringify(dataString);

$.ajax({
type: “POST”,
url: ‘…/mail/testSum.php’,
data: {wrongArray : wrongArray, realname:realname, rightAnswers:rightAnswers, percentageScore:percentageScore, asked:asked, mins:mins, secs:secs, deliver:deliver, testName:testName},
cache: false,
success: function(){
//alert(“OK”);
}
});
}

I’d guess this is where your issue is.

What should it be? I thought I had to convert the Array string to JSON

Stringify turns an existing JSON object into a string. You want to serialize your data first.

Here is a quick example of what I mean: https://jsfiddle.net/hvrx6910/

As I understand it, serialize applies to form data. My array has not been generated by a form, it is generated on the fly by incorrect answers to questions.

Here is a typical Array/string:

1. What is the volume in dm3 of an ideal gas, if a 0.216 mol sample has a temperature of 834 Kelvin at a pressure of 64 kPa ?

Answer = 23.39 dm3

,

2. What is the volume in cm3 of an ideal gas, if a 0.089 mol sample has a temperature of 625 Kelvin at a pressure of 130.1 kPa ?

Answer = 3550 cm3

,

3. What is the volume in cm3 of an ideal gas, if a 0.138 mol sample has a temperature of 175 °C at a pressure of 189.5 kPa ?

Answer = 2710 cm3

,

4. How many moles of an ideal gas are in a volume of 25.2 cm3 with a temperature of 154 °C and a pressure of 71.4 kPa ?

Answer = 0.51 mol

,

5. What is the volume in cm3 of an ideal gas, if a 0.089 mol sample has a temperature of 610 °C at a pressure of 195.9 kPa ?

Answer = 3330 cm3

Only with all of the HTML markup

Here is the result of the email:

You can see the square brackets at the start and end and the “,” between questions

Are there inputs? Where is the root place the data comes from?

I have the solution - thanks to everyone for your patience!

I had to send the data using

var wrongArray = JSON.stringify(wstore);

and then decode it at the server side in PHP

$myWrongArray=json_decode($_POST[‘wrongArray’], true);

I kind of understand how it all works. Thanks again for all your help.

Glad you figured it out, json_decode was the next step rather than that weird parsing you were trying to do to accomplish the same thing.

Sponsor our Newsletter | Privacy Policy | Terms of Service