Php keep echo when you submit and add new one - php

i want to make a add cricket stats page (witch i have done) but when i fill in the form and press submit i made it say echo "$name stats have been added"; but when i add a new persons stats that dispersers and is replaced by a different the one i just made, how can i make it stay every time i add a new one so i can see who's stats i have added?

Create an array of names stored in $_SESSION and keep adding to it on each post. Then display them all each time.
session_start();
// Initialize the array
if (!isset($_SESSION['names'])) {
$_SESSION['names'] = array();
}
// Add the newest name to the array
$_SESSION['names'][] = $name;
// Display them all in a loop with linebreaks
foreach ($_SESSION['names'] as $cur_name) {
echo "$cur_name stats have been added<br />\n";
}
EDIT:
To reset them, pass ?action=reset in the URL querystring as www.example.com?action=reset
<form action='scriptname.php' method="get">
<input type="hidden" name="action" value="reset" />
<input type="submit" value="Reset list" />
</form>
// Remove the session array on reset.
if (isset($_GET['action']) && $_GET['action'] == "reset")
{
unset($_SESSION['names']);
}

why dont you just insert the new name you wanna add, into your DB ? and make a select when you wanna view some names

Related

Insertion of data with two buttons

Actually my problem is,I have fours forms in two pages. I have two buttons name save and continue. Here if I click on save button data submitted in one table and show list of details ,when I click on another button called continue it will go to another page .
Here is the following code -
if (isset($_POST["submit_x"]) || !empty($_POST["submit_y"])) {
//here submit_x is input name of save button and submit_y is input name of continue button
/* runs some code of insert query */
if($submit_y=="continue"){
header("Location: example.php");
}else{
header("Location: example.php?action=list");
}
}
Can any one help me please.
Thanks in advance
Create two submit inputs with the same name and different values:
<input type="submit" name="action" value="Continue" />
<input type="submit" name="action" value="Save" />
the form will send the value of the clicked button:
<?php
if (isset($_POST['action'])) {
if ($_POST['action'] == 'Continue') {
...
}
}
<?php
if (isset($_POST['action']) && !empty($_POST['action'])) {
if ($_POST['action'] == 'Continue') {
//write code for continue part
}
if ($_POST['action'] == 'Save'){
//write code for Save part
}
}
?>
Here you have to make clear that when you will submit the form you will insert the form step wise step or all at a time......if you will go all at a time then you have store your values in session or else if you want to go through step wise step then in second form you have to update the second step fields with the first step id.
if (isset($_POST["submit_x"]) || !empty($_POST["submit_y"])) {
$_SESSION['name']=$_POST['name'];
$_SESSION['address']=$_POST['address']
}
like this you need to store in session and take it to next step

How to forward information from one page, trough another, and then finally to the third

This is my first post, so excuse me if I will not provide the information correctly.
So my problem is the following:
This is the first form:
<h1>Modificare carti</h1>
<br />
<form action="UTLcrt.php" method="post">
Cod Carte: <br /><input type="numeric" name="cod"><br>
Nume: <br /><input type="text" name="nume"><br>
Autor: <br /><input type="text" name="autor"><br>
Editura: <br /><input type="text" name="editura"><br>
Disponibilitate: <br /><input type="text" name="disp"><br>
Pret: <br /><input type="numeric" name="pret"><br>
<select name="vmod">
<option value="mod">Modificare carte</option>
<option value="str">Sterge carte</option>
<option value="src" >Cauta carte</option>
</select>
<input type="submit">
</form>
The UTLcrt.php contains the following code:
<?php
if (isset($_POST['vmod'])) {
$urls = array(
'mod' => 'modcrt.php',
'str' => 'strcrt.php',
'src' => 'srccrt.php'
);
$url = $urls[$_POST['vmod']];
header("Location: " . $url);
}
?>
And each php page does the following:
modcrt.php changes the entry in our database with the same"cod" with the info provided in the first form
strcrt.php deletes the register in our database, if the "cod" we entered in the first form finds a match
srccrt.php searches in the database if the register with the "cod" provided in the first form was found and shows a possitive message.
My problem is the following: the information I put in the first form doesn't get in the modcrt.php,strcrt.php,src.php pages... the $_Post's are empty...
How to send the information from the first page, trough the second and then to the third?
You can keep them in Session, by using
$_SESSION['info1']=$info1;
The POST values are empty because the third page isn't receiving a POST request. The order of events is this:
User requests the first page.
User POSTs a form to the second page, with values.
Second page tells the user to issue a GET request to the third page.
User requests the third page.
There are a few different ways to keep the information in the chain. You can:
Add it to the query string for the redirect
Store it in session
Store it in a database
etc.
The first one might look like this:
header("Location: " . $url . "?key=value");
Where the key/value pair is similar to those in a POST. In this case the values would be available to the third page in GET:
$_GET['key']
If you use session, the values stay server-side. So in the second page you can set the value:
$_SESSION['key'] = $value;
And then retrieve it in the third page:
$value = $_SESSION['key'];
Note that these session values will continue to live on the server until the session times out. You may want to unset them from the session once you're done with them if it starts to add confusion to other pages the user visits which also make use of these values.
Page 1
<?php
// this starts the session
session_start();
// this sets variables in the session
$_SESSION['color']='red';
$_SESSION['size']='small';
$_SESSION['shape']='round';
?>
Page 2
<?php
$color = $_SESSION['color'];
$size = $_SESSION['size'];
$shape = $_SESSION['shape'];
?>
and so on...

How to auto refresh list of session array items on submit?

I am working on a school comparing website. For this I need a plugin that handles that function. I save session data as school IDs so it can be passed on in the comparing table after choosing the schools.
Tasks that I have trouble with:
"Add button" - add post/school ID to session array - $_SESSION['schools']
Dashboard at the top - echo $_SESSION['schools'] values (just for user experience list the schools that are currently in the list)
when "Add button" is pressed update the dashboard list automaticly. Preferably not the whole page.
My attempt so far:
First of all I have I commented PHP form action:
<?php session_start();
$schools = array('post_id');
//If form not submitted, display form.
if (!isset($_POST['submit_school'])){
//If form submitted, process input.
} else {
//Retrieve established school array.
$schools=($_POST['school']);
//Convert user input string into an array.
$added=explode(',',$_POST['added']);
//Add to the established array.
array_splice($schools, count($schools), 0, $added);
//This could also be written $schools=array_merge($schools, $added);
}
$_SESSION['schools'] = $schools;
?>
Next up is the form itself:
<form method="post" action="http://henrijeret.ee/7788/temp_add_button.php" id="add_school">
<input type="hidden" name="added" value="Value" size="80" />
<?php
//Send current school array as hidden form data.
foreach ($schools as $s){
echo "<input type=\"hidden\" name=\"school[]\" value=\"$s\" />\n";
}
?>
<input type="submit" name="submit_school" value="Lisa võrdlusesse" />
</form>
And for the dashboard I use:
<?php
foreach($_SESSION['schools'] as $key => $value){
// and print out the values
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
?>
This is just a prototype to get my head wrapper around the task ahead of me...
Problems
Something does not feel right.. :P
When I submit the form, then the first change is not made. When I press it the second time, then it will update the the list leaving out the very last string. When refreshing then whole page, then the last one pops up
I very appriciate the advice on this long topic.. Maby I do not know where to look, but I am a little stuck with searching a solution..
Link to my running code http://henrijeret.ee/7788/
You're submitting the form on the first run.. if you will check it your URL changes and on the next run.. since you got this
//If form not submitted, display form.
if (!isset($_POST['submit_school'])){
//If form submitted, process input.
} else {
//Retrieve established school array.
$schools=($_POST['school']);
//Convert user input string into an array.
$added=explode(',',$_POST['added']);
//Add to the established array.
array_splice($schools, count($schools), 0, $added);
//This could also be written $schools=array_merge($schools, $added);
}
it will go to else statement because POST is already set.
Try this:
//If form not submitted, display form.
if (!isset($_POST['submit_school'])){
//If form submitted, process input.
} else {
//Retrieve established school array.
$schools=($_POST['school']);
//Convert user input string into an array.
$added=explode(',',$_POST['added']);
//Add to the established array.
array_splice($schools, count($schools), 0, $added);
//This could also be written $schools=array_merge($schools, $added);
$_SESSION['schools'] = $schools;
}

PHP avoiding a long POST

This is more of a technique question rather than maybe code. I am having a php form with many fields (items to select). Naturally some of the items might be selected and some not. How do I know which ones are selected when i post the data from page 1 to page 2? I thought of testing each one if empty or not, but there are just too many fields and it doesn't feel at all efficient to use or code.
Thanks,
UPDATE EDIT:
I've tried the following and maybe it will get me somewhere before I carry on testing the repliers solutions...
<html>
<body>
<form name="test" id="name" action="testprocess.php" method="POST">
<input type="text" name="choices[shirt]">
<input type="text" name="choices[pants]">
<input type="text" name="choices[tie]">
<input type="text" name="choices[socks]">
<input type="submit" value="submit data" />
</form>
</body>
</html>
and then second page:
<?php
$names = $_POST['choices'];
echo "Names are: <br>";
print_r($names);
?>
This gives out the following:
Names are: Array ( [shirt] => sdjalskdjlk [pants] => lkjlkjlk [tie]
=> jlk [socks] => lkjlkjl )
Now what I am going to try to do is iterate over the array, and since the values in my case are numbers, I will just check which of the fields are > 0 given the default is 0. I hope this works...if not then I will let you know :)
I think what you're looking for is this:
<form action="submit.php" method="POST">
<input type="checkbox" name="checkboxes[]" value="this" /> This
<input type="checkbox" name="checkboxes[]" value="might" /> might
<input type="checkbox" name="checkboxes[]" value="work" /> work
<input type="submit" />
</form>
And then in submit.php, you simply write:
<?php
foreach($_POST['checkboxes'] as $value) {
echo "{$value} was checked!";
}
?>
The square brackets in the name of the checkbox elements tell PHP to put all elements with this name into the same array, in this case $_POST['checkboxes'], though you could call the checkboxes anything you like, of course.
You should post your code so we would better understand what you want to do.
But from what I understood you are making a form with check boxes. If you want to see if the check boxes are selected, you can go like this:
if(!$_POST['checkbox1'] && !$_POST['checkbox2'] && !$_POST['checkbox3'])
This looks if all the three check boxes are empty.
Just an idea:
Create a hidden input field within your form with no value. Whenever any of the forms fields is filled/selected, you add the name attribute of that field in this hidden field (Field names are saved with a comma separator).
On doing a POST, you can read this variable and only those fields present in this have been selected/filled in the form.
Hope this helps.
Try this.....
<?php
function checkvalue($val) {
if($val != "") return true;
else return false;
}
if(isset($_POST['submit'])) {
$values = array_filter(($_POST), "checkvalue");
$set_values = array_keys($values);
}
?>
In this manner you can get all the values that has been set in an array..
I'm not exactly sure to understand your intention. I assume that you have multiple form fields you'd like to part into different Web pages (e.g. a typical survey form).
If this is the case use sessions to store the different data of your forms until the "final submit button" (e.g. on the last page) has been pressed.
How do I know which ones are selected when i post the data from page 1 to page 2?
is a different question from how to avoid a large POST to PHP.
Assuming this is a table of data...
Just update everything regardless (if you've got the primary / unique keys set correctly)
Use Ajax to update individual rows as they are changed at the front end
Use Javascript to set a flag within each row when the data in that row is modified
Or store a representation of the existing data for each row as a hidden field for the row, on submission e.g.
print "<form....><table>\n";
foreach ($row as $id=>$r) {
print "<tr><td><input type='hidden' name='prev[$id]' value='"
. md5(serialize($r)) . "'>...
}
...at the receiving end...
foreach ($_POST['prev'] as $id=>$prev) {
$sent_back=array( /* the field values in the row */ );
if (md5(serialize($sent_back)) != $prev) {
// data has changed
update_record($id, $sent_back);
}
}

Help with a function php

I want a function that prints those 2 "print" in the database ( insert intro ) when a button is pressed.
Here's the code:
<?php
$id2name=array();
$x=mysql_query("SELECT id,name FROM products WHERE id IN(".implode(',',array_keys($_SESSION['cart'])).")");
while($y=mysql_fetch_assoc($x)){
$id2name[$y['id']]=$y['name'];
}
foreach($_SESSION['cart'] as $k=>$v){
print "<br>[".$id2name[$k]."]\t".$v."\n <br>";
}
print "<br>$total<br>";
?>
How can I make that a function, to print it in the database when a button is pressed?
Not sure if I got you right, but as far as I understand, you want to write something to the database by pressing a button, right?
Well, to trigger an action by pressing a button, you need a form:
<form action="page_to_process_the_db_request.php" method="post">
<input type="hidden" value="<?php echo $total;?>" name="total" />
<input type="submit" value="Wirite to DB!" />
</form>
In this example, I assume you want to write the variable $total to DB.
So you post the data to the processing page (which also can be the same one you're on) and there, you look if there's something in the $_POST-array:
<?php
if(isset($_POST['total'])) {
mysql_query("UPDATE products SET total = ". $total); //or something like that
}
?>
Not sure though if this is what you're looking for...
//edit
referring to your comment, I guess you want to write the output of the loop to DB...
At first, you have to create an appropriate structure, like an array:
foreach($_SESSION['cart'] as $k=>$v){
$id2name[$k]['name'] = $v;
}
now you can turn the array into a simple string with
$serialized_array = serialize($id2name);
And now you can write this string to db. And when you read it from db, you can turn it back into an array again with:
$id2name_array = unserialize($serialized_array);

Categories