1

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,"
Mark
  • 73
  • 2
  • 12
  • 1
    according to your var_dump its string, you need to `explode` it. – Jigar Shah Jan 29 '18 at 06:15
  • You are declaring the array with `$studPerInfo = array();` but overwrite it with a string with `$studPerInfo = $_GET['studRegInfo'];` – SpazzMarticus Jan 29 '18 at 06:16
  • Change `$studLN = $studPerInfo[0];` in saveSAWorkSched.php to `$studLN = $studPerInfo[0][0];`, `$studFN = $studPerInfo[1];` to `$studFN = $studPerInfo[0][1];` and so forth... – marekful Jan 29 '18 at 06:19
  • From what I know you should use `POST` and `JSON.stringify( )` cause you try to submit an array, see this answer: https://stackoverflow.com/a/12097160/2008111 – caramba Jan 29 '18 at 06:23
  • @marekful I did that but it return an error... something about the offset – Mark Jan 30 '18 at 05:15
  • @caramba thanks for the info... I use "get" because it's not a vital information. I will try it but I will try the answer on this post :-) – Mark Jan 30 '18 at 05:16
  • @Mark no worries. The answer does the same thing just without the `POST` request. The problem with `GET` is there is a maximum size of the string, which in this case might be ok. – caramba Jan 30 '18 at 06:19

3 Answers3

1

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

Use explode.

$studPerInfo = explode(',', $_GET['studRegInfo']);
Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
  • this answer works for me... I will use this one since this is the first code that I use... I will try the other answers here... by the way can I accept multiple answers? – Mark Jan 30 '18 at 05:58
  • no you can't . You can only accept one answer but give up vote to others if you find useful too. – Jigar Shah Jan 30 '18 at 06:00
  • thanks :-) but sorry to say that your code works for me but I will use the other one because those code fit on my problem.. – Mark Jan 30 '18 at 06:10
1

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 .

Akhil Singh
  • 700
  • 6
  • 17
1

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);
  • your code works.. thanks for sharing your answers.. I think I will use this code because when decode it's a multidimensional array since I use multidimensional array on the main php. – Mark Jan 30 '18 at 06:07