Save value in PHP Session variable - php

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

Related

How to update database with server-side information without reloading the page

I'm developing a like/dislike system using php and MySQL, but I have a problem. When I add a like, let's say, I do it by adding a row to the likes table, that contains the post id and the user id. The user id is stored in a $_SESSION, and I can't just pass it to the html and make a AJAX request to run some php code and add the like, because this way it's too easy to simply change the user id.
So basicaly I don't want to reload the page to first get the $_SESSION value but I don't now how to get it's value after the page has loaded. I don't know if this is doable with php only, if not, what language should I use
if you're using send ajax request for saving the like or dislike value. after saving the like/dislike value. you have to return a new like value count. in PHP simply echo the value that will automatically come to your ajax success if not simply return the new like/dislike info, encode with JSON and return the value.
get these values in the success function and target the like and dislike button or div and change the new value with an ajax response.
if you want to code answer please share your code here so it's better understood by everyone

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

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.

How to retrieve table row data in php by using an <a>(link)?

I want to implement an inventory table visible below. What I want to know is how to retrieve a table row data, for example when I click the "edit" link from the row of "Item One", I can retrieve it's Item ID "A00". Well, the rest will be up to me after I retrieved that row data. I just want to know first how to retrieve it.
Please be informed that I have already displayed the table shown using php, retrieving of table row data by clicking those <a> links is the only thing I need to know. Please give me a hint on how to do it, I really need it for our project. This is important to us, any help would be appreciated, thank you very much for your ample time!
Use jQuery Ajax and pass your ID as a parameter to the PHP file. Then your PHP file fetches the respective row from your database table.
Alternatively, you can just use jQuery/Javascript to load the content of the current row into the form and 'on update' you can update your database row with the new values. Ajax can also be used in this case so that the page doesn't refresh.
Try this:
Using jQuery ajax you can pass the ID of that record to php file.
Inside that file, You can retrieve the data of that row using ID you passed. For that, use SELECT query to get row data.
Then pass data to jQuery response to use it in the form to display.
Thanks

passing a dynamic html table to php as an array

I have a dynamic html table( rows are added dynamically using java script) and I want to pass whole table to a php script as an array.Is there a way to insert the table data to an array?
I have tried using phpTableExtractor but it dosen't extract dynamically added rows.
I don't see any other reliable solution but creating a HTML form along with the table and have the user click a submit button to save the table. Then you can read all content from $_POST and store it in a database.
Another solution would be to use AJAX requests to store the table content every time the focus changes or something like that. Will make your page dependent on JavaScript though.
What if you fill a JS array at the same time you create the table?
You will use the html just for display but behind you have the data you send to php.

Creating a table using jquery - can I then populate a field automatically once the first function is done?

I'm probably doing this all wrong but here goes....
I'm calling a php function with ajax to create a table with three columns. Currently the php function fetches all the data and creates a table which it loads into a div once the function is finished.
The third column can only be created using data from the first two columns.
Is there any way to populate this column automatically with another ajax call?
(i have another php function which i can feed the first two variables into)
Can anyone help???
EDIT: I can't populate the field in the first call - as the function will take significant time to process - i'd like to show the user the progress of this column being populated.
$('#yourtable tr').each(function(index)){
var col1=$(this).children('td.col1').text();
var col2=$(this).children('td.col2').text();
$(this).children('td.col3').load('yourscript.php?col1='+col1+'&col2='+col2+');
}
try this

Categories