Mark
Mark

Reputation: 73

My Array variable will return only the first character

Good day! My problem is not common here... there are plenty questions similar to my problem but as I compare one is identical but he did not declare his/her variable like this: $page = array();, but mine I declare it but still it return the first character of the value.

Source: PHP String in Array Only Returns First Character

Here's my code:

main php file

Script:

var saveStudRegInfo=[[],[]];

studInfoLN = document.getElementById('studRegLN').value;
studInfoFN = document.getElementById('studRegFN').value;
studInfoMN = document.getElementById('studRegMN').value;
studInfoGender = document.getElementById('studRegGender').value;
studInfoCourse = document.getElementById('studRegCourse').value;
studInfoID = document.getElementById('studRegID').value;
studInfoRFID = document.getElementById('studRegRFID').value;

saveStudRegInfo[0][0] = studInfoLN;
saveStudRegInfo[0][1] = studInfoFN;
saveStudRegInfo[0][2] = studInfoMN;
saveStudRegInfo[0][3] = studInfoGender;
saveStudRegInfo[0][4] = studInfoCourse;
saveStudRegInfo[0][5] = Number(studInfoID);
saveStudRegInfo[0][6] = studInfoRFID;
saveStudRegInfo[0][7] = studPicFilename;

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementById("saveWorkSched").innerHTML = this.responseText;
   }
};

xhttp.open("GET", "ajax/saveSAWorkSched.php?studRegInfo="+saveStudRegInfo+"&schedList="+getSAWorkSched+"&studInfoID="+studInfoID, true);
xhttp.send();

saveSAWorkSched.php

$studPerInfo = array();
$studPerInfo = $_GET['studRegInfo'];
$studLN=$studFN=$studMN=$studGender=$studCourse=$studID=$studRFID=$studPicFilename='';

if (!empty($studPerInfo)) {
    $studLN = $studPerInfo[0];
    $studFN = $studPerInfo[1];
    $studMN = $studPerInfo[2];
    $studGender = $studPerInfo[3];
    $studCourse = $studPerInfo[4];
    $studID = (int)$studPerInfo[5];
    $studRFID = $studPerInfo[6];
    $studPicFilename = $studPerInfo[7];
}

echo var_dump($studLN);

The Result will be:

string(1) "d"

Here's the var_dump for the whole array ($studPerInfo).

string(74) "Dela Cruz,Juan,Masipag,male,BSInfoTech,1234567890,2342342342342,dummy.jpg,"

Upvotes: 1

Views: 644

Answers (3)

Akhil Singh
Akhil Singh

Reputation: 730

This is string value , to use it as an array you need to explode it.

 $studPerInfo = explode(',', $_GET['studRegInfo']);

print_r($studPerInfo);

get the result in array .

Upvotes: 1

You Old Fool
You Old Fool

Reputation: 22940

My advice would be to handle this client side first. Without knowing the exact structure of your array or getting in to breaking it apart into separate query parameters, it's fairly easy to just encode the whole array as json and then decode it server side. Try something like this:

xhttp.open("GET", "ajax/saveSAWorkSched.php?studRegInfo="+encodeURIComponent(JSON.stringify(saveStudRegInfo)));

Then in php you can decode from json:

$studPerInfo = json_decode($_GET['studRegInfo'],1);

Upvotes: 1

Jigar Shah
Jigar Shah

Reputation: 6223

The value you are getting is string , to use it as an aaray you need to explode it

Use explode.

$studPerInfo = explode(',', $_GET['studRegInfo']);

Upvotes: 1

Related Questions