PHP - Updating form data. (on same page) - php

I need to be able to update my form data once the submit button is pressed on the same page.
I have a csv file. And at the moment, I have worked out how to edit data inside it, and save it in the csv file (through a form).
-(I have attached the main code below, but even any suggestions would be helpful)
The form and php execution is on the same page, but once the user edits the values and presses submit, the data goes back to the original data. So it updates in the csv file, but not in the form.
This is the form:
for ($i=0; $i<200; $i++) {
if(empty($SearchResults[$i][0]) == false){ //If its full
?>
<form method="post" action="">
Full Name: <input name = "Name2" type="text" value="<?php echo $SearchResults[$i][0] ?>"/>
Weight (kg): <input name="Weight2" type="number" value="<?php echo $SearchResults[$i][1] ?>"/>
Weight of belongings (kg): <input name="WeightOfBelongings2" type="number"value="<?php echo $SearchResults[$i][2] ?>"/>
<input name="submit" type="submit" />
</form>
<?php
$i++;
}else if (empty($SearchResults[$i][0]) == true){ //If it is empty
$i =201;
}
}
This is what happens when the submit button is pressed:
if (isset($_POST['submit'])) {
$FullName = $_POST['Name2'];
$Weight = $_POST['Weight2'];
$WeightOfBelongings = $_POST['WeightOfBelongings2'];
//Creates a new felon from the class in felen.php and assigns it to this variable $Felon
$Felen = new felen;
//This refers to a function in the class Felen. From this information it calculates the costs, ammounts etc. And saves it to a variable that can be called.
$Felen -> CreateFelen($FullName, $Weight, $WeightOfBelongings);
//This is a for loop that checks when there is an avaliable line to put the data into.
if ($FullName != "") {
$i = 0;
for ($i=0; $i<200; $i++) {
if(empty($csv[$i][0]) == false){ //If its full
if($csv[$i][0] == $_POST['Name2']){
//its only printing it if it is in the first row?
$csv[$i][0] = $FullName;
$csv[$i][1] = $Weight;
$csv[$i][2] = $WeightOfBelongings;
$csv[$i][3] = $Felen->FelenTotalWeight;
$csv[$i][4] = $Felen->UnassembliumRequired;
$csv[$i][5] = $Felen->ReassembliumRequired;
$csv[$i][6] = $Felen->CostOfUnassemblium;
$csv[$i][7] = $Felen->CostOfReassemblium;
$csv[$i][8] = $Felen->TotalCostOfJourney;
$i = 201;
}
}
}
//Saves the previous data and new changes to the csv file
//This opens to file as a write file
$fp = fopen('data.csv', 'w');
//This takes the array that has had data ddded to it and adds it to the file
foreach ($csv as $fields) {
fputcsv($fp, $fields);
}
//This closes the file
fclose($fp);
}
}
Thank you very much!

Related

PHP Array Size Loop

We need to create a program that lets the user input the array size, their name, and age (depending on the array size the user entered). After that, we need to display all the elements of the array.
This is my code, but I'm having a problem adding a new element for another user and displaying it.
<html>
<head>
<title> PHP Array </title>
</head>
<body>
<form method="post" action="example.php">
<h3> Please enter the your information: </h3>
Array Size: <input type="text" name="arraysize"/> <br/><br>
Name: <input type="text" name="name" /><br/><br/>
Age: <input type="text" name="age"/> <br/><br/>
<input type="submit" name="submit" value="Submit"/>
<input type="reset" name="cancel" value="Cancel"/><br/><br/>
<?php
if(isset($_POST['submit'])){
if((!empty($_POST['name'])) && (!empty($_POST['age'])) && (!empty($_POST['arraysize']))){
$info = array($_POST['arraysize'], $_POST['name'], $_POST['g6ave']);
$arraylength = count($info);
for ($i=0; $i<=$arraylength ; $i++) {
$name = $_POST['name'];
for ($j=1; $j<=$i; $j++){
echo "User's Name" .$i. ": " .$name. [$j] ."<br>";
$age = $_POST['age'];
for($k=0; $k<=$i; $k++){
echo "User's Age: " .$age. [$k] ."<br/>";
}
}
}
}
}
?>
</body>
</html>
One approach (of other possible approaches) below should give you the main ideas. I also commented the aim of the each script part.
Approach below assumes that you'll use same URL for all your form pages. (1st, 2nd and the success page)
I hope this helps.
session_start(); //Start new or resume existing session
if (isset($_SESSION['form_success']) && $_SESSION['form_success'] === true)
{
require 'success_page.php';
unset($_SESSION['form_success']); // don't needed anymore
return; //not to continue to execute the code
}
// decide the page from user
if (isset($_POST['page']))
{
$page = $_POST['page'];
}
else
{
// display the first form page for the 1st time
require 'first_page_form.php';
return; //not to continue to execute the code
}
// if the first page was submitted.
if ($page === 'first') // or a specific POST flag from 1st page
{
//verify data from first page
$warnings = [];
if (first_page_data_valid() === true)
{
require 'second_page_form.php';
return; //not to continue to execute the code
}
// populate $warnings during first_page_data_valid()
//if first page data are invalid
print_r($warnings);
require 'first_page_form.php'; //display again
return; //not to continue to execute the code
}
// if the second page was submitted.
if ($page === 'second') // or a specific POST flag from 2nd page
{
//verify data from second page
$warnings = [];
if (second_page_data_valid() === true) // populate $warnings during second_page_data_valid()
{
// do things. ex: DB operations.
if (db_actions_success() === true)
{
$_SESSION['form_success'] = true; // defined and set to true.
// PHP permanent URL redirection. usage of 301 is important.
// it clears POST content. Prevents F5/refresh.
header("Location: https://www.url.com/form.php", true, 301);
exit; // good/recommended to use after redirect
}
else
{
echo 'System down or I have a bug. Try again later.';
return; //not to continue to execute the code
}
}
//if second page data is invalid
print_r($warnings);
require 'second_page_form.php'; //display again
return; //not to continue to execute the code
}

Problem with 'fputcsv' repeating the same variable

Iv been trying to have data from my array variables sent into a .txt file. the variables are working as intended since iv already managed to code in 3 products that a user can choose from, then use a form to submit the quantity and they all individually appear with the movie name and quantity separately. But when i cant seem to figure out a way to fputcsv send the movie names and quantity for each movie separately, i just repeats the last movie and quantity for every line, then when i submit the next movie and quantity it over-rides the last one.
My code for cart page (not showing all the code since most is css, the if/else statements were just me trying desperately to at least get the 2nd movie name out)
<?php
session_start();
?>
<?php
var_dump ($_POST);
if(!isset($_SESSION['cart'])) {
$_SESSION['cart']=[];
}
$cart = &$_SESSION['cart'];
$movieName = $_POST['movie_name'];
$quantity = $_POST['qty'];
if(isset($movieName)) {
$currentQuantity = $cart[$movieName];
$cart[$movieName] = $currentQuantity + $quantity;
} else {
$cart[$movieName] = $quantity;
}
var_dump($_SESSION);
?>
<?php
$list = array
(
"$movieName,$quantity",
"$movieName,$quantity",
"$movieName,$quantity",
);
$file = fopen("orders.txt","w");
foreach ($list as $line)
if ($movieName = 'Game of Thrones: Season 1') {
}
else if ($movieName = 'Friends Season 1') {
}
else if ($movieName = 'Inception') {
}
{
fputcsv($file,explode(',',$line));
}
fclose($file); ?>
one of the forms submitting data:
<form action="cart.php" method="post">
<input type="hidden" name="movie_name" value= "Game of Thrones: Season 1" />
<input type = "hidden" name = "id" value = "M01" />
<br>
<div class="widthc">
<button class="prod" id="minus">−</button>
<input type="number" name="qty" value="0" id="qty" min="0" max="15"/>
<button class="prod" id="plus">+</button>
<br><br>
<button class="prod" type="submit"> Submit</button>
</form>
If there is any other code u want/need to see just let me know and ill edit my post to include it. Thanks for taking the time to read.
So many little things in this code seem wrong or show just a lack of understanding. Some may be due to "sample" code.
Either way, let me go over it in chunks.
<?php
session_start();
?>
<?php
var_dump ($_POST);
Closing and opening a new PHP block is just pointless. This could be due to sample code for the question (or not). Either way it should be just :
<?php
session_start();
// debug
var_dump ($_POST);
Nothing really wrong on the next chunk. Personally I do not like it (ie; create a new var for something that already exists, even with a reference) but each to their own.
if(! isset($_SESSION['cart'])) {
$_SESSION['cart']=[];
}
$cart = &$_SESSION['cart'];
$movieName = $_POST['movie_name'];
$quantity = $_POST['qty'];
This next bit I'm not 100% confident on. You have created $movieName above from the value of $_POST['movie_name']. I believe even if that value is a blank string, your isset() will return true. Either way the result may still work as expect.
if(isset($movieName)) {
$currentQuantity = $cart[$movieName];
$cart[$movieName] = $currentQuantity + $quantity;
} else {
$cart[$movieName] = $quantity;
}
If the lodgic is sound, the following line change would remove a pointless creation of a var.
//$currentQuantity = $cart[$movieName];
$cart[$movieName] += $quantity;
Another close and open PHP block that could be removed
var_dump($_SESSION);
?>
<?php
Now to the real issue. You are creating an array with repeated elements:
$list = array (
"$movieName,$quantity",
"$movieName,$quantity",
"$movieName,$quantity",
);
The above code block would make 3 elements in the array, all with the same values - they would be all identical values of the current $movieName and $quantity.
I think what you really wanted to do was something more like:
foreach($_SESSION['cart'] as $k => $v) {
if ($v > 0) {
$list[] = array($k,$v);
}
}
This next part is interesting due to the errors. The first question is why do you hard-code a variable name?
And then the next question is did you really mean to use = instead of == in the if statement?
$file = fopen("orders.txt","w");
foreach ($list as $line) {
if ($movieName = 'Game of Thrones: Season 1') {
// do nothing? Really?
} else if ($movieName = 'Friends Season 1') {
// do nothing? Really?
} else if ($movieName = 'Inception') {
// do nothing? Really?
}
}
fputcsv($file,explode(',',$line));
fclose($file);
I think what you wanted here was more like:
// To overwrite, use 'w'
$fp = fopen('orders.csv', 'w');
// Or (as noted by Rob) to append to the file, use 'a'
//$fp = fopen('orders.csv', 'a');
// For other options see: http://php.net/manual/en/function.fopen.php
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);

Send radio selection to function along with textbox

I've got some code working that takes the text from my input box and moves it across to a function. I'm now trying to change it so I add another form element, a radio button and I want to access the choice within my functions.php file.
This is my current code which works for the post name, but what if I want to also grab the colours boxes that was selected too?
main.php
<?php
if (isset($_POST['submit'])) {
$data = $_POST['name']; // the data from the form input.
}
?>
...
<form action="/" method="post">
<input type="text" name="name" placeholder="Acme Corp"/>
<input name="colour" type="radio" value="red">Red<br>
<input name="colour" type="radio" value="blue">Blue<br>
<input name="colour" type="radio" value="green">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
<img src="pngfile.php?data=<?php print urlencode($data);?>"
alt="png php file">
I guess I confused because currently it is calling this:
pngfile.php
<?php
require_once 'functions.php';
$inputData = urldecode($_GET['data']);
process($inputData);
exit;
?>
Which calls functions.php
<?php
function process($inputdata)
{
...
EDIT: What I have tried:
main.php [Change]
$data = $_POST['name'] && $_POST['colour']
But I'm not really sure how to progress.
Never trust user input. Sanitize and validate your inputs before using them.
This can be arranged better, but the basics are still true.
PHP Manual: filter_input_array()
PHP Manual: filter_var_array()
Small Function Library
function sanitizeArray($filterRules)
{
return filter_input_array(INPUT_POST, $filterRules, true)
}
function validateArray($filteredData, $validationRules)
{
return filter_var_array($filteredData, $validationRules, true);
}
function checkFilterResults(array $testArray, array &$errors)
{
if (!in_array(false, $testArray, true) || !in_array(null, $testArray, true)) {
foreach($testArray as $key => $value)
{
$errors[$key] = '';
}
return true;
}
if ($testArray['name'] !== true) { //You can make a function and do various test.
$errors['name'] = 'That is not a valid name.';
}
if ($testArray['clour'] !== true) { //You can make a function and do many test.
$errors['colour'] = 'That is not a valid colour.';
}
return false;
}
function processUserInput(array &$filteredData, array $filterRulesArray, array $validationRulesArray, array &$cleanData, array &$errors)
{
$filteredInput = null;
$tempData = sanitizeArray($filterRulesArray);
if (!$checkFilterResults($tempData, $errors)){
throw new UnexpectedValueException("An input value was unable to be sanitized.");
//Consider forcing the page to redraw.
}
$filteredData = $tempData;
$validatedData = validateArray($filteredData, $validationRulesArray);
if (!$checkFilterResults($validatedData, $errors)){
return false;
}
$errors['form'] = '';
$cleanData = $validatedData;
return true;
}
function htmlEscapeArray(array &$filteredData)
{
foreach($filteredData as $key => &$value)
{
$value = htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8', false);
}
return;
}
Basic Main Line
try {
$filterRulesArray = []; //You define this.
$filteredData = []; //A temporary array.
$validationRulesArray = []; //You define this.
$validatedData = null; //Another temporary array.
$results = null; //Input processing results: true or false.
$cleanData = null; //Filtered and validated input array.
$errors = []; //Any errors that have accumulated.
if (isset($_POST, $_POST['submit'], $_POST['colour']) && !empty($_POST)) {
$results = processUserInput($filteredData, $filterRulesArray, $validationRulesArray, $cleanData, $errors);
} else {
$errors['form'] = "You must fill out the form."
}
if ($results === true) {
$name = $cleanData['name']; //You can do what you want.
$colour = $cleanData['colour']; //You can do what you want.
//header("Location: http://url.com/registration/thankYou/")
//exit;
}
//Prepare user input for re-display in browser
htmlEscapeArray($filteredData);
} catch (Exception $e) {
header("Location: http://url.com/samePage/"); //Force a page reload.
}
Let the form redraw if input processing fails.
Use the $errors array to display error messages.
Use the $filteredData array to make the form sticky.
<html>
<head>
<title>Your Webpage</title>
</head>
<body>
<h1>My Form</h1>
<form action="/" method="post">
<!-- Make spots for error messages -->
<input type="text" name="name" placeholder="Acme Corp" value="PUT PHP HERE"/>
<!-- No time to display sticky radios! :-) -->
<input name="colour" type="radio" checked="checked" value="red">Red<br>
<input name="colour" type="radio" value="blue">Blue<br>
<input name="colour" type="radio" value="green">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Tip:
It may be better to submit numbers for radios, as opposed to longer string values like (red, green, blue). Numbers are easier to sanitize and validate. Naturally, then you must translate the input number into its corresponding string. You would do that after validation has finished, but before using the values. Good luck!
you can access this using array like this.
$data[] = $_POST['name'];
$data[] =$_POST['colour'];
Or combine both variable
$data = $_POST['name'].'&'.$_POST['colour'];
Use Array in php for this process as follows:
if (isset($_POST['submit'])) {
$array_val = array(
"name"=> $_POST['name'],
"color"=> $_POST['color']
);
}

PHP: Trying to upload multiple files, or a single file

protected function upload($name) {
$files = array();
// if block 1
if(!empty($_FILES[$name]['tmp_name'])) {
for($i = 0; $i < count($_FILES[$name]['tmp_name']); $i++) {
// if block #2
if(!empty($_FILES[$name]['tmp_name']) && is_uploaded_file($_FILES[$name]['tmp_name'][$i])) {
# we're dealing with multiple uploads
$handle['key'] = $name;
$handle['name'] = $_FILES[$name]['name'][$i];
$handle['size'] = $_FILES[$name]['size'][$i];
$handle['type'] = $_FILES[$name]['type'][$i];
$handle['tmp_name'] = $_FILES[$name]['tmp_name'][$i];
// put each array into the $files array
array_push($files,$this->_process_image($handle));
}
#block 3...
}
return $files;
}
return false;
}
I'm trying to create a function that will handle multiple upload scenarios: multiple file uploads per submit, and one file upload per submit, using a variety of file inputs.
The above method...
works as expected with two inputs
<input name="multiple[]" type="file">
<input name="multiple[]" type="file">
and a single input using multiple...
<input name="multiple[]" type="file" multiple>
but not a single input using a single upload
<input name="single" type="file">
Now, I noticed that if I change the name of single to single[] it works. So I copied block #2 and added it as another block (where #3 is), but this time without using [$i] and it worked for the single input, but failed if i had multiple inputs but only used one input to upload one file (not uploading 2 files using both inputs).
My brain hurts. How can I get this to work for all these scenarios?
Edit: When I say it's not working, I mean it's not even processing. No output, no errors, nothing. I think it's just being skipped over, which is why I added block #3...
HTML
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:
<br />
<input name="userfile[]" type="file" />
<br />
<input name="userfile[]" type="file" />
<br />
<input type="submit" value="Send files" />
PHP(file-upload.php)
<?php
if ($_FILES['upload']) {
$file_ary = reArrayFiles($_FILES['userfile']);
foreach ($file_ary as $file) {
if(intval($file['error'])==4){
continue;
}
//If you upload a single file this will execute only once
print 'File Name: ' . $file['name'];
print 'File Type: ' . $file['type'];
print 'File Size: ' . $file['size'];
}
}
function reArrayFiles($file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
?>
Files Output :
Figured it out and it was really easy. I needed to check if the $_FILES array contained more than one file and process accordingly. probably a little bit more code than I'd like, but it's working exactly how I wanted.
protected function upload($name) {
$files = array();
if(!empty($_FILES[$name]['tmp_name'])) {
# *** THIS IF BLOCK SOLVED THE PROBLEM ***
# JUST CHECK IF $_FILES HAS MORE THAN ONE FILE
if(count($_FILES[$name]['tmp_name']) > 1) {
for($i = 0; $i < count($_FILES[$name]['tmp_name']); $i++) {
if(!empty($_FILES[$name]['tmp_name']) && is_uploaded_file($_FILES[$name]['tmp_name'][$i])) {
# we're dealing with multiple uploads
$handle['key'] = $name;
$handle['name'] = $_FILES[$name]['name'][$i];
$handle['size'] = $_FILES[$name]['size'][$i];
$handle['type'] = $_FILES[$name]['type'][$i];
$handle['tmp_name'] = $_FILES[$name]['tmp_name'][$i];
array_push($files,$this->_process_image($handle));
}
}
return $files;
} else {
if(!empty($_FILES[$name]['tmp_name']) && is_uploaded_file($_FILES[$name]['tmp_name'])) {
# we're handling a single upload
$handle['key'] = $name;
$handle['name'] = $_FILES[$name]['name'];
$handle['size'] = $_FILES[$name]['size'];
$handle['type'] = $_FILES[$name]['type'];
$handle['tmp_name'] = $_FILES[$name]['tmp_name'];
return $this->_process_image($handle);
}
}
}
return false;
}

Server Side Form Validation PHP with multi-file Upload

HTML:
Owner: input type="text" name="owner[]" />
Category:
<select name="cat[]">
<option value="clothes">Clothes</option>
<option value="shoes">Shoes</option>
<option value="accessories">Accessories</option>
</select>
Upload: <input type="file" name="image[]" />
whith function that clone the same fields when click on "+ button"
I count the POST field with:
$num = count($_FILES['image']['name']);
because i want to know how many times the end user clone the fields.
what i want is Make sure that the user has to fill all fields which he opend with "+ button" i cant check all the hidden fields i want to check just the field he opend.
so what can i do ?
i cant do like this:
$owner = $_POST['owner'][$i];
$cat = $_POST['cat'][$i];
$file = $_FILES['image'][$i];
if ($owner && $cat && $file)
echo "bla bla bla";
else
echo "fill all the fields!";
can anyone help me ?
thank you
There are some points which you need to make sure beforehand. Whenever you are using any input field's name attribute as "owner[]" or "cat[]" or "image[]", you will get an array then. But since, input file's property accessing capability is already 2D array by default, so now you will be able to access those properties as a 3D array.
When you have added a "[]" for the input file field's name attribute, you will now get the name of the 1st file as "$_FILES['image'][0]['name']", because array indices start with 0. As per your question, you can validate using the following way:-
<?php
$numOwners = count($_POST['owner']);
$numCats = count($_POST['cat']);
$numFiles = count($_FILES['image']);
// Check to see if the number of Fields for each (Owners, Categories & Files) are the same
if ($numFiles === $numCats && $numFiles === $numOwners) {
$boolInconsistencyOwners = FALSE;
$boolInconsistencyCats = FALSE;
$boolInconsistencyFiles = FALSE;
for ($i = 0; $i < $numFiles; $i++) {
if (empty($_POST['owner'][$i])) {
$boolInconsistencyOwners = TRUE;
break;
}
if (empty($_POST['cat'][$i])) {
$boolInconsistencyCats = TRUE;
break;
}
if (!is_uploaded_file($_FILES['image'][$i]['tmp_name'])) {
$boolInconsistencyFiles = TRUE;
break;
}
}
if ($boolInconsistencyOwners || $boolInconsistencyCats || $boolInconsistencyFiles) {
echo "I knew that there will be some problems with users' mentality!";
// Redirect with proper Error Messages
}
else {
echo "Wow, Users have improved & have become quite obedient!";
// Proceed with normal requirements
}
}
else {
echo "Something fishy is going on!";
}
?>
Hope it helps.

Categories