mikerg Posted January 28, 2010 Share Posted January 28, 2010 Hi All, I am quite new to PHP and Javascript but have written an Online Shop (Mainly for Fun and education) It uses MySQL for the Shop Item Data and a 'Username.txt' file for each users cart. (Formated as 'ItemID1xQty1, ItemID2xQty2' etc.... From the Cart View window, the user can edit the Quantity (Form text field). When the 'Update Cart' Form button is clicked, a Javascript function; re-calculates the monetry values displays the changes Creates a String containing the new cart data Calls a PHP page (cart_update.php), that writes the Data to the UserName.txt file This all works fine, except...... cart_update.php needs to do it's stuff and close. - But it doesn't close so the user is left with a blank window that has to be closed manually. The cart data is posted from hidden form elements to cart_update.php. using javascript to close the window results in the 'Do you want To Close this Window' message.. using JS History or $_SERVER['HTTP_REFERER'] refreshes the Cart View window to its pre-edited state Is there a way round this..? Can I put the cart_update.php code somewhere other than a .php page? So close but so far.. Any help will be greatly appreciated.. ~Mike~ Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/ Share on other sites More sharing options...
ignace Posted January 28, 2010 Share Posted January 28, 2010 Why do you store cart data in a text file if you are also using a database? Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1002949 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 If it's a blank page with no output then you could easily get the page to send them to another page using the following code in your cart_update.php. echo header("Location: yourfile.php"); Just a tip, it'd probably be easier to store the cart contents in a session variable instead. Edit: To be a bit more helpful, you could put the code inside cart_update.php in a function and call it from your form page when it's sent. Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1002952 Share on other sites More sharing options...
jl5501 Posted January 28, 2010 Share Posted January 28, 2010 Like ignace says, I am also confused as to why you are using a text file for the cart data when you have a database. If you need to have something on the server update to hold current cart status etc, like you are doing with your text file, and ideally should be a table in the database, then the best way to do this, is to use javascript the way you are, and then have it call the php file with an ajax call. You can then have the output of the php file used to update a cart info section of your page, which will be handled by javascript when the php files ends. All of this will happen without a page reload, so your blank page issue will no longer be an issue Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1002954 Share on other sites More sharing options...
mikerg Posted January 28, 2010 Author Share Posted January 28, 2010 Why do you store cart data in a text file if you are also using a database? Thanks for the lighning reply... I am new to PHP and Javascript also to MySQL. My coding experience has all been with VB6 on Local workstations. My first experience with Web databasing was using delimited text files for the data, controlled by CGI scripts (Not my own). I felt that the cart data, being only two fields per item would be simpler as one line of delimited text. (Probably wrongly) I may well look into changing to MySQL for the cart data, but would like to see if I can cure this problem with the existing method first.. Thanks for your help ~Mike~ Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1002958 Share on other sites More sharing options...
mikerg Posted January 28, 2010 Author Share Posted January 28, 2010 To be a bit more helpful, you could put the code inside cart_update.php in a function and call it from your form page when it's sent. This is what I was trying to work out... With JS, a Functions.js file housing various functions can be included via a link.. How is this done with PHP? ~Mike~ Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1002971 Share on other sites More sharing options...
Yucky Posted January 28, 2010 Share Posted January 28, 2010 You can simply include it. include('yourpage.php'); From there on you can access any functions that reside in that file. Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1002972 Share on other sites More sharing options...
mikerg Posted January 29, 2010 Author Share Posted January 29, 2010 Hi All, Thanks for all the replies.. I am now reading up on session variables and will then be looking at converting the cart data from delimited text to MySQL. Whilst experimenting with session variables, I have already come accross some conflicting info... Version 1; session_start(); session_register ("new_variable"); $new_variable = "whatever"; // This doesn't work Version 2: (This Works well but results in long variable names session_start(); $_SESSION['new_variable']='whatever'; ---------- php Page 2------- session_start(); echo "Our new_variable value is ".$_SESSION['new_variable']."<br>"; Displays; Our new_variable value is Whatever Version 3 (This Works well but results in long variable names) session_start(); session_register ("new_variable"); $_SESSION['new_variable']='Whatever'; ---------- php Page 2------- session_start(); echo "Our new_variable value is ".$_SESSION['new_variable']."<br>"; Displays; Our new_variable value is Whatever ....................................... Question; Is $_SESSION['new_variable'] the shortest way of referencing session variables other than re-assigning to a new variable.. ie; $NV = $_SESSION['new_variable']; I shall carry on and post back here as I go. Thanks for your advice and assistance, ~Mike~ Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1003539 Share on other sites More sharing options...
mikerg Posted February 1, 2010 Author Share Posted February 1, 2010 Hello again.. I have re-written the key pages now, replacing Form.Post with a mixture of session variables and local cookies for transferring data from page to page. Using Session variables for User Info has made the site far more easy to manage.. They are set in the starting page (index.php) and are then available for all pages as needed. Dynamic data variables (User Selections), such as Search Criteria, Item ID's etc. are not so easy, as PHP code is only run on page load making session variables un-usable... I have used Javascript to save these into short term local cookies from the source page, retrieving them via PHP in the target page.. This is all working very well and reasonably fast.. All I have to do now is convert the remaining 5 pages and get some more data added. Thank you all for pointing me in the right direction with your valuable advice ~Mike~ Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1004897 Share on other sites More sharing options...
jl5501 Posted February 1, 2010 Share Posted February 1, 2010 It is quite common practice to carry things like user selections and search criteria in the urlstring as get variables, which are then availabale as $_GET variables in the target page. If you need to get values from the server on the same page as you are on, you then need to consider using ajax to do that. Good luck in your project Quote Link to comment https://forums.phpfreaks.com/topic/190093-php-function-page-advice/#findComment-1004898 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.