changing value of isset - php

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.

Related

How to go back to filled fields in a form after a php form is submitted?

I have a php form
After filling fields I submit the form
After submitting form I have in the same window (a pop up) a print (via echo) of strings using variables values previously entered in the form
I need a way to go back to the form with fields filled by the values entered previously in order to modify or review them and then submit the form again
My form is created in a proprietary php framework. A field is so defined in a file named base.env:
$sf_vaccine['fields']['taxCode'] = new TextField('taxCode');
$sf_vaccine['fields']['taxCode']->label = 'Codice fiscale';
$sf_vaccine['fields']['taxCode']->addFlag(Field::NOT_EMPTY());
$sf_vaccine['sheet']->addField($sf_vaccine['fields']['taxCode']);
After a research on the web and differents attempts I did this. After submitting the form I print a link to the previous page:
echo "<center>Go Back</a></center></br></br></br>";
It allows me to back to the previous page but fields are empty and not filled as I need
I've also made an attempt using sessions: here what I did:
<?php
session_start();
echo "<center><a href=http://192.168.228.34/it/centre_rm/card_block/?_command=insert&prefill=yes><b>Go Back</b></a></center></br></br></br>";
$_GET['prefill']=='yes';
$_SESSION['taxCode'] = $record['taxCode'];
$fields['taxCode']->value = $_SESSION['taxCode'];
Don't use javascript history for this, but just go back to the page after submitting the form and add the values to the form.
Something like this:
<?php
$length = '';
$width = '';
if (!empty($_POST)) {
if (isset($_POST['length'])) {
$length = $_POST['length'];
}
if (isset($_POST['width'])) {
$width = $_POST['width'];
}
echo "Submitted length '$length' and width '$width'.";
}
?>
<form method="POST" action="">
Length: <input type="text" name="length" value="<?= $length ?>"/>
Width: <input type="text" name="width" value="<?= $width ?>"/>
<input type="submit">
</form>
I've added the echo-statement as sort of confirmation message but this can be removed if not needed.
Because of the action="" part, after clicking submit, the same page wil reload. De input values are sent and can be reused on the page reload.
Within the if-statements the submitted values are assigned to a variable to add these again in the input fields within the value tag so they are prefilled with the submitted data.
If you'd like to modify the submitted values before adding them to the inputs, you can do this within the if-statements and adjust the data accordingly.

Print_r not working in php [duplicate]

What is the best way of checking whether or not a form has been submitted to determine whether I should pass the form's variables to my validation class?
First I thought maybe:
isset($_POST)
But that will always return true as a superglobal is defined everywhere. I don't want to have to iterate through each element of my form with:
if(isset($_POST['element1']) || isset($_POST['element2']) || isset(...etc
Whilst writing this question I thought of a much more basic solution, add a hidden field to act as a flag that I can check.
Is there a 'cleaner' way to do it than adding my own flag?
For general check if there was a POST action use:
if ($_POST)
EDIT: As stated in the comments, this method won't work for in some cases (e.g. with check boxes and button without a name). You really should use:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
How about
if($_SERVER['REQUEST_METHOD'] == 'POST')
Actually, the submit button already performs this function.
Try in the FORM:
<form method="post">
<input type="submit" name="treasure" value="go!">
</form>
Then in the PHP handler:
if (isset($_POST['treasure'])){
echo "treasure will be set if the form has been submitted (to TRUE, I believe)";
}
Use
if(isset($_POST['submit'])) // name of your submit button
if ($_SERVER['REQUEST_METHOD'] == 'POST').
Try this
<form action="" method="POST" id="formaddtask">
Add Task: <input type="text"name="newtaskname" />
<input type="submit" value="Submit"/>
</form>
//Check if the form is submitted
if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['newtaskname'])){
}
On a different note, it is also always a good practice to add a token to your form and verify it to check if the data was not sent from outside. Here are the steps:
Generate a unique token (you can use hash) Ex:
$token = hash (string $algo , string $data [, bool $raw_output = FALSE ] );
Assign this token to a session variable. Ex:
$_SESSION['form_token'] = $token;
Add a hidden input to submit the token. Ex:
input type="hidden" name="token" value="{$token}"
then as part of your validation, check if the submitted token
matches the session var.
Ex: if ( $_POST['token'] === $_SESSION['form_token'] ) ....
I had the same problem - also make sure you add name="" in the input button. Well, that fix worked for me.
if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['add'])){
echo "stuff is happening now";
}
<input type="submit" name="add" value="Submit">
You could also use:
is_array($_POST)

can't be able to understand $_post in codeigniter

using codeigniter mvc pattern I create form in view that take only two values form user
<form action="<?php base_url(); ?> blogs/new_post" method="POST">
<label>Title</label>
<input type="text" name="post_title" />
<label>discription</label>
<input type="text" name="post_detail" />
<input type="submit" value="post" />
</form>
now when i submit the form, data goes to the controller now here confusion created in my code that i can't able to understand i use three cases in controller fist is if i use !empty($_POST) in controller and in view weather i fill the form or not fill the form message displayed in controller is post
my question is why always displaying post why not displaying not a post when i fill nothing in the form
if(!empty($_POST)) {
echo "post";
} else {
echo "not a post";
}
my second question is same related to the first condition now i use isset instead of !empty
public function new_Post() {
if(isset($_POST)) {
echo "post";
} else {
echo "not a post";
}
}
in this case either i fill form or not fill form when i submit the form the result always same that is "post"
and in third case if i use !isset the the result is always not a post eiter i fill or not fill the form
hope so you will understand my problem when i comes to if(!empty($_POST)) this condition then my mind is confuse what is the purpose of $_post
This is because you are using the whole global variable $_POST to check empty and isset(). $_POST is not empty when you click on the button. You just Print_r the $_POST it will have the value of submit button. You need to print the value of $_POST on click and see the values in the array
Well, in CodeIgniter (and most of the framework) doesn't allow you to access $_POST directly due to security reason.
You must access $_POST values through $this->input->post()
For more information read Input Class from CodeIgniter's docs
https://www.codeigniter.com/user_guide/libraries/input.html
$_POST is exist when you click on the button:
so empty and isset cant work Correctly
the value at the first is
Array
(
)
SO U should check
if($_POST)
instead
if(!empty($_POST)) OR if(isset($_POST))
AND best way is u check
if ($this->input->post())

PHP Redirect to new page after form submission

I have a form, which redirects the user to "page1.php" after the form is submitted. What I want to do is to redirect the user to "page2.php" after the form is submitted, but I need to make sure that the POST request was sent. Example:
<form action="page1.php" method="POST">
<input type="text" name="username" />
<input type="text" name="age" />
<input type="submit" value="" />
</form>
When the user clicks on Submit, it redirects him to page1.php. I want to redirect him to page2.php, but I need to make sure that the data is sent to the server. I can't use AJAX. Is there any way to do it with cURL or something like that? Any examples?
Thanks!
I guess this works !!
In page1.php
<?php
//do establish session
//and check for the input fields obtained via $_POST
if(isset($_POST['name_of_your_field']) && !empty($_POST['name_of_your_field'])){
if(!mail($to,$subject,$message)){
header('location:form.php?msg=error');
}else{
header('location:page2.php?msg=succes');
}
}
?>
You can check if the POST request was sent with something like:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// do something...
}
You can create a hidden input in your form and send additional info about the form that is submitted, e.g. action.
Inside you will do your magic and redirect user with:
header('Location: page2.php');
exit();
In your 'page1.php' processor, add a 'header' redirect to 'page2.php'.
header("Location: page2.php");
exit;
you could do a check if query is complete
example
<?php
$query=mysqli_query(".......your query statement")or trigger_error(mysqli_error());
if($query){
header("Location:page2.php");
}
else{
die('error');
}
?>
In your situation, you can just simple check for the posted data. Like
$username = $_POST['username'];
$age = $_POST['age'];
if($username&&$age){ /* This is to check if the variables are not empty */
// redirect to page2
}
It is logical that if those are not empty, meaning they are posted. Even if they are empty, as long as you get there, that means it was posted. There is no need to check if posted or not, what needs to be checked was, if posted data was there.
I hope I made it clear. ^_^ but making sure is not bad at all. Happy coding friend.

isset function doesn't work on my form and form is nod getting submitted

I am submitting form to page and checking if submit button value InsertMe isset but non of the code inside is executed
<?php
if (isset($_POST['InsertMe'])){
//code to execute
echo "Test";
}
?>
and insert buttons looks like that
<input style="float:right;" name="InsertMe" id="InsertMe" type="submit" class="rectangular-button" value="Add User" />
if (!isset($_POST['InsertMe']) || empty($_POST['InsertMe'])) {
// error message here
} else {
// What you want to do if not empty and is set.
}
This code will check if the variables is set and if if it's empty.
"||" is the PHP operator to check or. So in this case it's checking if it's set OR empty.
Make sure you have the form tag set to post.
<form method="post">
you are selecting Id I think we should be use name tag parameters in isset()

Categories