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,"
7
  • 1
    according to your var_dump its string, you need to explode it. Commented Jan 29, 2018 at 6:15
  • You are declaring the array with $studPerInfo = array(); but overwrite it with a string with $studPerInfo = $_GET['studRegInfo']; Commented Jan 29, 2018 at 6:16
  • Change $studLN = $studPerInfo[0]; in saveSAWorkSched.php to $studLN = $studPerInfo[0][0];, $studFN = $studPerInfo[1]; to $studFN = $studPerInfo[0][1]; and so forth... Commented Jan 29, 2018 at 6:19
  • From what I know you should use POST and JSON.stringify( ) cause you try to submit an array, see this answer: stackoverflow.com/a/12097160/2008111 Commented Jan 29, 2018 at 6:23
  • @marekful I did that but it return an error... something about the offset Commented Jan 30, 2018 at 5:15

3 Answers 3

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

1 Comment

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.
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']);

3 Comments

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?
no you can't . You can only accept one answer but give up vote to others if you find useful too.
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..
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 .

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.