2

So, in js, you can have a variable outside of a function which push can be used to add values to it and access it anywhere else.

var names = [];
function my_function_js(){
    alert(names);
}

In php, can a variable act in the similar manner?

For example, let say I have an ajax function with a variable. Then I want to know if I can add different values to this variable using ajax at multiple occasions.

$names   = array();
function my_function(){
   $names[]   = $_POST['names'];
}

Let say, for the first ajax call, mike was passed. Then steve for second call and sean for the last.

Would each value override the previous value or would it be saved like in js?

(In other words, I would like to know if I can add values to a php variable using ajax multiple calls).

Thanks.

EDIT:

It was pointed out that ajax variable (in this case $names) will be reset every time a new ajax call is made.

Then, how about have another variable that does not get affected by the ajax call and simply push the ajax value to it?

For example:

$FULL_NAMES   = array();
function my_function(){
   $names  = $_POST['names'];
   $FULL_NAMES[]   = $names;
}

Would something like this work?

7
  • (In other words, I would like to know if I can add values to a php variable using ajax multiple calls). Well no. Each time an ajax call gets called the variable is.. well, let's say reset for instance. If you want to persistenly store such a value then you can either decide to save that to a file (NOT RECOMMENDED) or to use a database and query that each time using a particular criteria maybe. A possible alternative is to make a unique ajax call by passing all the names, but I have no idea of what html + js structure you have and what you effectively need to accomplish. Commented Feb 19, 2016 at 18:01
  • How about having a separate variable apart from the ajax variable and save each value into it? (I will add an edit shortly here) Commented Feb 19, 2016 at 18:03
  • I added an edit and was wondering if you can take a look at it and see if it will work. Commented Feb 19, 2016 at 18:06
  • Nonono let's clarify: each time you perform the ajax call, FULL_NAMES will be declared empty again. Unless you are passing a single array of values ($_POST['names']) containing all the names you are forced to use a database or some kind of storage, can you please explain us what is the current background? (your html + javascript, in a nutshell) What is the current goal you have to achieve? Commented Feb 19, 2016 at 18:08
  • 1
    Yes, apart from being easier it's also way better, mostly because the tasks will be automatically queued already. Just be careful to prevent SQL injections when you query the username inside the database, I suggest you to either use the mysqli_* prototypes or directly apporaching to PDO (prepared statements). Commented Feb 19, 2016 at 18:15

3 Answers 3

4

There is the option of storing the value in the $_SESSION array.

You would need to add

session_start();

to the top the page

and use.

$_SESSION['varname'][] = "whateveryouwant";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I might give this a try. =)
3

If you only need the array for the current user and you don't need to persist the data longer that the user session.You could use a session variable for that.

$_SESSION['names'][] = $_POST['names'];

Like other user have said, don't forget to add session_start() somewhere in the beginning of your code.

Edit: I updated my answer with Adam Copley comment

2 Comments

Welcome to stack overflow! This wouldn't work as it would reset the array every Ajax post. I think you meant to put an exclamation before isset, however if you did it is redundant anyway.
Thanks ! Yeah that was what I wanted to write. I removed useless isset check
2

Yes, you can add items to array the way you are doing it or using push but keep in mind that PHP is not persistent, so if you are talking about an ajax call only the values you added during that call are going to exist.

<?php 

$names = array();
$names[] = "Mark";
$names[] = "Franklin";
$names[] = "Sam";

var_dump($names);

Output:

array(3) {
  [0]=>
  string(4) "Mark"
  [1]=>
  string(8) "Franklin"
  [2]=>
  string(3) "Sam"
}

If you want persistence, you're going to have to store the values in a cookie, db, filesystem, memory cache like redis, or some other place.

2 Comments

I see. so you are saying that previously added value via ajax will not be saved in the array? (In my case, there could be multiple ajax calls and I want to save the value in the variable). I guess I will have to save each value in db.
I added an edit and was wondering if you can take a look at it and see if it will work.

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.