I have a home.php through which i send some data which gets inserted into DB, which is done by making an ajax call to another php page, lets say function.php and a unique id is generated against the entry and stored in a variable $id.
When the user leaves the home.php page I want to delete the particular entry that was inserted into the DB, by making another ajax call to same function.php, but the problem is that the $id that was previously generated is lost as function.php is reloaded. Declaring a global in home.php doesn't work.
Any idea how can I store the id, so i can retrieve it, when needed.
Some code would be great.
Abhi
Try this way:
Put this on your pages to start a session: session_start(). Then after you inserted data into DB do $_SESSION['sql_insert_id'] = $id - this is assuming you need just one insert per visitor.
Then when you need to delete from the DB you have the $id stored in $_SESSION['sql_insert_id']
You can also close the session after the delete with:
session_unset($_SESSION['sql_insert_id']);
session_destroy();
Related
I created forms that automatically generates an ID for a user. I did most of the work with that variable in phpadmin not php. I put NULL for it in php. However, I am creating two pages, first one asks the user to register and then asks him when he is entering a place and leaving. I wish to make it such that when the user registers and is moved to the next page, his ID is also called in that file. This is how I tried to put the variable in the first table:
$_SESSION['idofuser'] = $_GET["id"];
How I put it in the 2nd file that is to call the value of this variable:
$ids=$_SESSION['idofuser'];
Can someone identify the problem?
from PHP documentation, you need to start the session using session_start() or session.auto_start is set to 1 in php.ini to make it accesssible to the next page or until the session die.
see more details in PHP Doc about session.
i made a (php) form, and used php session and post to transfer the form data to another page, which gives the user an option to have a look at the data and if wrong he shall move to the main form..
now here is what i did
on page1 i made a main form say form1, when user clicks on submit, i saved the data to DB and added that id to a session, and sent that session to next page using the same id
on next page i called from db matching the same id and displayed data accordingly..
now i displayed the data (on the second page) inside a div styled with css, the logic for this second page is giving the user the choice if the data is wrong he can edit, and bottom of this div i added a button with code
Edit
now my question is this, what shall be next? my target is, if user clicks on this edit button he shall be taken to main form (i.e. page1, form1) and there he shall has the option of editing the data in the same id. How can i achieve this friends?
Also i used
<?php session_start(); ?>
on top of both the pages...
Just change your code on the form1 page to; load the data from the database if the id of the record is set in your session variable.
Then if the user decides the data is wrong he just gets sent back to form1 and has the possibility to make changes.
Use something similar to: value="<?php echo $data['name']" in your input fields.
Then check for the id and load the data:
if(isset($_SESSION['record_id'])) {
// Do a query with `where id = record_id
// Fetch the data in the $data variable
}
IF you dont want use GET then use SESSION.
in file.php set
$_SESSION['your_id'] = $theid ;
and in your edit.php retrieve it like that
$id = $_SESSION['your_id'] ;
of course you must session_start in both files.
If you already have all the data in your session you can simply link back to the first page. All you have to do is set all of your fields on the first page to pre-fill with the session data as long as it isset. This way, when you go back the forms will auto fill with the data the user has already entered and they can simply change what is needed.
I'm having a hard time wrapping my idea into text so basically this is how I want to do this.
I have form.html once completed it tells the user thanks for filling out and goes back to the home page. then the data taken using php posts the data to gallery.html where their pic and info are posted.
How do I go about accomplishing this? How do I post it to a specific location in gallery.html?
You don't need to all my work its just I cant seem to find it on here or anywhere else it might be really simple.
Your best bet would be to setup a simple database to store the data in, making sure that you are using safe practices for data insertion into the database. And then on the gallery page you can call a specific id from the url to display the proper data. Like gallery.php?profile=5
On the gallery.php page it would query the database looking for id #5 and get all the data out of it the database and echo it on the page.
You are wrong in understanding how php works , Here are the mistakes that I could identify in your posting ,
your calling a webpage as gallery.html , but without .php extension , Apache wont call php interpreter when it receives a request , so to accomplish this you need to rename this to gallery.php .
to accomplish your task
here is the approach
1 > in your form.html page set attribute of form tag action=store.php method = post
2 > within store.php connect to mysql database and store all data in mysql data base ,
after successfully storing data display a thank you message and redirect to home page
3 > in your gallery.php file based on the requested id display the profile
NOTE : - you need to design your database in mysql with a table and column to store profile details
Form.html
User fills in the form
User clicks submit
Form data will go for processing to a page, say thanks.php
Thanks.php
Collect form data
Store it in database
Redirect to homepage.php after successful insertion
Homepage.php
Show message saying 'Thank you for filling out the form'
Give a link in this page to view the details of user
Something like this View Profile
Gallery.php
Grab the id using $_GET $rowid = $_GET['rowid'];
Use $rowid in WHERE clause of your database query $q = "SELECT * FROM <tablename> WHERE <primary key column name> = '".$rowid."'";
Use the returned set to show that particular user's data.
I have the following snippet:
foreach($_POST['status'] as &$status){
mysql_real_escape_string($status);
}
How do I wrap this in a session like $_SESSION['status'] or something so I can insert this into the database? This code is situated on page 2 of a 5 page form so thats why I can't just insert it using $_POST['status'].
As you have a five page form you need the status variable to be inserted either in the session or in database so that you can use them till the end...
Both the things are quite easy..And there exists one third method also..
To store it in session create a session variable like $_SESSION['status'] and insert the value you have got for the status and on all subsequent pages call them as:
if(isset($_SESSION['status']) && $_SESSION['status'])
To insert into database just insert this info along with what you have got in first page, but I won't suggest that, as user may abort registration after first page..
Third method is to make them as
">
By this you can get the values on next form submit place also with $_POST['status_again']..
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