Insertion of data with two buttons - php

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

Related

post isset($_POST['next'] is not set after submit /refresh page. Need to be press twice for it to be set how to fix?

<script>
function refreshPage()
{
document.forms[0].submit();
}
</script>
<?php
foreach($values as $value)
{
?>
<input type = "checkbox" name = "food[]" value = "<?php echo($value['dinner']);?>"
<?php if(isset($_POST['food'])) echo "checked='checked'"; ?> > // note this is still part of
input
<?php
}
?>
<button name = "pass" onClick = "refreshPage()">refresh</button>
<?php
if(isset($_POST['pass'])
{
// do a lot more stuff but I have this for temp info
echo("hello");
// I am printing all the check box values in here I do not
// have the code for it yet but I think ik how to do it
// but since i do not have code for it now it is just empty
}
?>
hi so everytime I click on the button refresh. the isset($_POST['pass']) does not work. I have to click on it a second for it to be set which would then print the hello and table of check items part.
I want it so that if you click on it once it will print the hello. Not twice. How do i fix my code? FYI I know you could do isset($_POST['food']) for it to work. But that will break other parts of my code.
I need the button to set the isset($_POST['pass']) to be True (after one click) after I press the refresh button one time.

PHP - Check if page refresh or post data on the same page

Is there any way to know if the page was refreshed or data was posted data on the same page?
To be little more specific:
I have to post data on the same page.
This affects the where condition of the query.
If the page was refreshed, then the where condition must be 1.
Otherwise, where condition contains some id to get specific data from
the table.
Your best bet is to use PHP sessions, along with your submitted data in $_POST. Let's presume for this example you have the following form:
<form action="this_page.php" method="post">
<input type="text" name="important-info" />
<input type="submit" value="Submit" />
</form>
Then elsewhere in the same page is the PHP code:
<?php
// example code
session_start();
if (!isset($_SESSION['previousVisitor']) && isset($_POST['important-info'])) {
// this is a new visitor who has submitted the form
$_SESSION['previousVisitor'] = true;
// where is based on $_POST['important-info']
} else () {
// where is 1
}
// close the session after you do what you need - this stops large pages causing hang
session_destroy();
Please note that they can clear this session variable by deleting their cookies.
on the top of the page just include
if(isset($_POST['name']) && $_POST['name']!=''){
//your code goes here
}
I suggest you to check request
//Here goes the code
session_start();
$counter = 0;
$counter = (isset($_SESSION['param'])) ? $counter++ : 0;
if($counter == 0)
echo "data GET or POST";
else
echo "refreshed";
** If you want only POST param, use $_POST instead of $_REQUEST

changing value of isset

I'm wondering if it's possible to manually change the value of an isset value. That is, to do something like this:
isset($_POST['search_user']) = true;
Why I want to do this: I have two different "submit" forms on one page. When one form is submitted, I want to capture all the values of that form into SESSION variables. However, when the other form is submitted, the SESSION variables are wiped out (since the first form is not, technically, submitted anymore).
My idea was that, if the second form is submitted, then automatically set the value of the first form to true
If I understand your question correctly, if a second form is submitted, why not just destroy the current session and start new sessions using the variables posted from the new form?
http://php.net/manual/en/function.session-destroy.php
session_destroy();
...Or, you can set another session variable if the second form is submitted:
if (isset($_POST['search_user'])) {
$_SESSION['search_user'] = "true";
}
if ($_SESSION['search_user'] == "true") {
// Second form was submitted
}
You can try to define a name and a value for each submit button, so you retrieve this in the PHP file and do what you want, according you need. For instance:
HTML to the first form:
<form name="form1" action="page2.php" method="post">
<input type="submit" value="1" name="button01">
</form>
HTML to the second form:
<form name="form2" action="page2.php" method="post">
<input type="submit" value="1" name="button02">
</form>
Then you can detect the form thas was submited doing this in page2.php:
if($_POST['button01'] == "1")
{
// Do what you need based on form1 submit
}
elseif($_POST['button02'] == "1")
{
// Do what you need based on form2 submit
}
Try this and then leave some comment telling if it helps you.

How do I detect which submit button was pressed on a Zend Framework form?

I have a Zend Framework form that has two submit buttons
$changes = new Zend_Form_Element_Submit('save_changes');
$changes->setLabel('Save Changes');
$delete = new Zend_Form_Element_Submit('delete');
$delete->setLabel('Delete');
Which renders HTML like such:
<input type="submit" name="save_changes" id="user_save_changes" value="Save Changes" >
<input type="submit" name="delete" id="user_delete" value="Delete" >
In the controller, how do I determine which button the user pressed?
In your case you should just be able to check
if(isset($_POST['save_changes'])
// or
if(isset($_POST['delete'])
Since only the value of the clicked button will be submitted.
Usually you give both buttons the same name (e.g. action) and then set the
value to the action you want to perform. Unfortunately that doesn't work very well
with IE. Check this page for more information about different solutions for multiple
submit buttons.
Since you are using Zend I would recommend a more Zend-ish approach.
You can call elements directly by theirs names and Zend has a method for form buttons (buttons, reset, submits) called isChecked().
in your code it would be:
if ($form->save_changes->isChecked()) {
// Saving ...
else if ($form->delete->isChecked()) {
// Removing ...
Actually you can get this by:
if($this->getRequest()->getPost('save_changes'){
//Code here
}
if($this->getRequest()->getPost('delete'){
//Code here
}
The reason I made two if condition because you can't do if else because one you load that page even though you didn't click any submit button, the other condition will be executed.
Example:
if($this->getRequest()->getPost('save_changes'){
//once you load this will become true because you didn't click this
}else{
//once you load this page this will become true because you didn't click the save_changes submit button
}
True story.
$data = $this->getRequest()->getPost();
if (array_key_exists('save_changes', $data)) {
..
} else if (array_key_exists('delete', $data)) {
..
}
$formData = $this->getRequest()->getPost();
if($formData['submit']=='save_changes'){ // echo "save chanes" ; }
if($formData['submit']=='delete'){ // echo "delete";}

Php keep echo when you submit and add new one

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

Categories