can i use include instead of session variables? - php

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).

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

PHP:How to save checkbox values temporary before submission

I'm a bit rusty in PHP,
I'm curently doing an PHP assignment where a user can select and save listed images/data/values into their own collection.
I have inputted all the data and printed it out in a repeat region with recordset paging.
I'm confused about how I am supposed to save a checked checkbox temporary before submission as there a more then 1 page as I'm using recordset paging to output the options.(Meaning: i have selected 2 values in the first page then i click next page to select balance values and finally submit my selection)
TIA
I have read an article on storing in session , that is the solution I guess, but I wonder how I'm supposed to send the value to the session when chaging the page (recordset paging generated by Dreamweaver)
To clarify the previous answers, you will most likely want to create a new $_SESSION variable for each check-box and associate a boolean with it.
You can store the result of a form post in PHP's $_SESSION variable.
Read this post for more information: Storing Form Data as a Session Variable
Also, there has to a tutorial or something in Google Land.
If you need to save the form results without submitting the form, try a JavaScript/AJAX approach. The idea is that you actually do submit the form, but in a behind-the-scenes kind of way (that is, the user never notices it). Essentially, you're going to want to build a "autosave" functionality. Look at these two posts:
Autosaving Form Input Using Prototype and PHP
AJAX Autosave functionality
They probably won't fit your needs exactly, but they should give you a good idea of what you should do.
Note: both of these posts use a timer to trigger their autosave functionality. I would suggest tweaking the trigger to detect any changes in your form instead.
Store them in _SESSION and process when needed.

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.

PHP: Pass non-form variables between pages?

I have a page. The user submits the page and sends it to a PHP results page. It works fine. Now, I want the results page to link to another page, and for the elements on that page to depend on what was on the results page. I know how to pass form variables to another page, but I don't know anything about passing non-form variables.
From my searching on the web, my best guess is that I should be passing by URL. Is this correct? If so, a possible problem: the page I want the results page to pass to will have a form, and the user will go to yet another results page by clicking submit (the form data will be sent by POST). Can I send the non-form data (the old results page variable) along with the form data, if the user is going to the other page using POST?
I strongly suggest using sessions. It's not that hard to learn, php makes it VERY easy using http://php.net/session_start and the $_SESSION variable.
Advantage is that you will not have to submit a form on every click, and no information will be displayed in plain text in the URL.
There are several options. However, the easiest may be to simply pass the data on using hidden input fields.
Other options would be using the session, storing to a database between forms, or some combination therein.
If you are going to use POST to go to the next page the most simple option is to include the data you want to send along using an input type="hidden" element in your form.
You might consider using a session to pass the data along.
You can embed the non-form data into the second form as hidden fields. To make sure that it is also passed in the link, you can just add it to the URL as a query string, like:
http://..../blah.php?var1=val1&var2=val2
as long as the data you're passing can fit into a URL. Be sure to urlencode() anything you're appending to the URL.
<?php
start_session();
$_SESSION['Foo'] = 'Bar' // Add stuff here.
?>

Getting $_POST variable from table

I'm trying to build a sort of resource allocation form. I'd like to be able to print a table from a database, and then allow users to click on each cell that they would like to reserve. Also, being able to drag and select multiple cells. Then send all of this via $_POST to another php script.
Problem is, I have no idea where to start.
Any suggestions?
The first and most critical thing you're going to need from what you described is a bunch of hidden fields to store the information you're interested in. You would have to write javascript code on the client side to store the users interaction with your page into these hidden fields.
To receive data via POST, you will need <input type="hidden" name"some_field"> for every bit of data you wish to "know" about that was changed on your page. Table information is not transmitted in a POST operation if it's just text, so you can't see the layout of the modified table on post back to the server.
If you don't have to POST this data to another form, it is probably a better idea to make callbacks via XMLHTTPREQUEST as the user interacts with your page, but I don't know the requirements of what you're trying to do.
I wrote one for my school recently; the trick is to either use buttons/links or addEventListener the cells to JavaScript. If you want the source code to my app, download this zip file:
http://azabani.com/files/busbook.zip
Edit:
My system works in the following way:
addEventListener to cell clicks, calling book()
book() then sets location to book.php
book.php does the database work
book.php sets the location header to immediately go back to the viewer
The system knows which week view to go back to based on session variables.

Categories