Can I store some data permanently in a variable once initialized? - php

I have a form which has two input field called firstname and lastname with a submit button called
submit. The input fields use twitter's typeahead.js with custom event trigger (typeahead:selected)
Thus, when the firstname is selected from the dropdown, I have a ajax call in the trigger which post the
enrollment no of the selected person to another php page(xyz.php).
Here on xyz.php
If I store that enrollment number in a variable
$link = POST['number'];
Now this works just fine.
But the problem is when I click my button I have another AJAX call to the same php file.
And now I cannot use $link. It says Notice: Undefined index: number
Is there anyway I can get my variable to store the data permanently?

You can use session on the server side to store data across requests.
You can find more information here:
http://www.php.net/manual/en/intro.session.php

You can use sessions or a text file or a cookie, it depends your needs.

Related

How to pass a variable from php to a modal

Initially, as can be seen in this unsuccessful attempt: Undefined index error when sending variable from jquery to php and this unsuccessful attempt:Passing Jquery variable to php within a modal
I was trying to pass a variable from php, to jquery and back to php.
Basically I am generating images form a database in a php loop. When the user selects an image, I want the id of that image to be passed to the modal so that I can query the database again (based on that id) and get more information.
Is there another way of doing this other than the methods outlined in the posts above. For some reason those do not return the result.
The difference between Send PHP variable value to JavaScript popup window in same page and my question is that I want it as a heading.. and as a variable so I can query the database again. I am not using this to input values into a form

can i use include instead of session variables?

I am trying to pass the values of variables from one php page to another php page. Is it possible to include the previous php page in the next php page using include statement and use those variables and values rather than using $_SESSION['variable']="value".
If not, is there any other way to pass the values? Thanks in advance!
No. You'll run the code in the included file again, from scratch. Any data produced based on user input will have been lost.
If not, is there any other way to pass the values?
Passing the entire value to the browser and having it send the entire value back to the new script. (via form data, query strings in links, cookies, etc).
I am trying to create a script where a user can post something they want to sell and then I am using while loop in php to display the information from the database. Other users can click on the contact button and send an email to them. I was told that if I use 'session', the same email id will be stored for every listing. So I am wondering if I can use the hidden field and if so, how do I assign that php variable value to the html hidden field?
You can't use hidden fields for this. You need to store persistent data on the server. Store the email address with the rest of the information in the database. (You'll probably want a separate user table and associate the For Sale item with a user using a foreign key).

Selecting all checkboxes and storing the names of selected checkbox in session. PHP,Javascript

I have a checkbox list generated using php depending upon number of coloumns in database,
for each checkbox i'm using a onchange function to store it's name in session variable if it' checked.
In order to select all checkbox i'm using a javascript....but in that way i'm not able to send checkboxes name to session.
How can i select all checkboxes at a time as well as store their names in session variable(only checked ones name must go out of all)??
If You are using AJAX function call on onChange="" then you must be executing a single function every time you click on a checkbox and setting a session variable, in that function you may place this conditionally like
UPDATE:
if (particular checkbox is checked/Unchecked?){
if(select all checkbox is checked?){
//send ajax request to php file to unset that particular session, as in this case all check box will be checked
}else{
//send ajax request to php file to set that particular session
}
}elseif(select all checkbox is checked?){
//send ajax request to php file to set all the checkbox sessions
}

Sending JavaScript variable to PHP

I have to send a value that is stored in a JavaScript variable to a PHP page. The PHP page is in a different folder than the JavaScript page.
This JavaScript variable is in a method that fires when we click a button.
How can I send that variable value to the PHP page?
(This is an Eclipse project.)
The easiest way to do this is to use jQuery's POST method to send serialized data to a PHP page that can process your request.
However, you didn't say if you want the client to cache the value and always remind server of the value for all future requests. If so, you can once again use JavaScript to store values inside the cookie, so that every time user click on something (GET / POST) it will also send out the cookie containing your javascript value to the server
Using Ajax
Do you submit a HTML form when the button is clicked. If yes then you can set the javascript variable value into a hidden field of the form on button click. After which your PHP code and read its value on the server-side.

Save value in PHP Session variable

I have an html table which contains records, comes from mysql db. Each row also contains id (PK) wrt db table record.
Now I want to save record id in PHP Session variable, when I click on a row.
To do this I used onclick property for each row & call a javascript function with record id as function parameter & it works fine but how could I save this id in PHP Session variable ?
Thanks.
That's not possible because PHP is server-side language. However, you can use ajax for that.
Here is an ajax tutorial:
http://www.w3schools.com/Ajax/Default.Asp
To do this you probably need to use ajax. Which is where on a click event you could probably call a different php to save your values in session

Categories