I have a code like this.
What I'm trying to do is:
Check whether post values is there, if yes - execute my query and display the result.
Or check for session value, if session is there play with session value and display the result.
When I print the session value, it is getting printed (i.e author_id is there in session)
But it is not coming inside the IF loop. Why?
In other words, I want to check for post values or session value, and display the record accordingly.
PS: If i remove the || condition (i.e isset ( $_SESSION ['author_id'] ) ) my code words perfect.
<?php
session_start();
print_r($_SESSION);
if (isset ( $_POST ['submit'] ) || isset ( $_SESSION ['author_id'] )) {
.. do something with my post values or session values ..
}
?>
<form method="post" id="myform1" name="myform1">
........... my form fields..............
<input type="submit" name="submit" value="Submit" />
</form>
Important: Instead of using session or cookie, is there a way to handle the form input field values in the same page?
Try check with Only $_POST
if (isset( $_POST)||isset ( $_SESSION ['author_id'] )) {
.. do something with my post values or session values ..
}
You can pass the value to java script function in onclick() and you can do certain amount of processing in that function
Related
I've already tried this stuff
$item=$_POST($val['item_id'])
And
$item=$_POST[$val['item_id']]
Any Idea on how to Post my inputted data ?
$_POST isn't a function, it is a special PHP array that reflects the data submitted from a form. So, the second line you got there can work only if the $val['item_id'] has a valid post name key. You should always first check if that key actually exists in the $_POST data array by using isset function like this:
if (isset($_POST[$val['item_id']]) {
$item = $_POST[$val['item_id']];
}
To debug and see all $_POST data, use this code:
<pre><?php
print_r($_POST);
?></pre>
1) form.html
Make sure your form uses POST method.
<form action="submit.php" method="post">
<input name="say" value="Hi">
<input name="to" value="Mom">
<input type="submit" value="Submit">
</form>
2) submit.php
var_export($_POST);
Will result in:
array (
'say' => 'Hi',
'to' => 'Mom',
)
$_POST is not a function, but an array superglobal, which means you can access submitted data thus:
print $_POST['field_name']
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())
Search page looks like this
<form method="post" action="response.php">
<input type="text" name="varianta" value="var_varianta">
<input type="submit">
</form>
<?php
session_start();
$var_varianta= $_POST['varianta'];
?>
The response page has the below line code:
$filters[] = array("filterType" => "resultLimit", "filterValue" => "varianta");
How i can i enter tn the "filterValue" => "**varianta**"); the $var_varianta that is inputed in the search field
Thanks
It's not clear what you're asking, but it seems like there's some confusion. $_POST contains the values that have been submitted, not the values that a user is entering. So, $_POST variables are set on the action page, not on the page showing the form. You can't set a $_POST variable in PHP on the page showing the form and expect the action page to know about that.
If I understand your question correctly, you should use $_POST['varianta'] on the action page to access whatever was submitted.
ok i have dynamically created form elements with unique names and when validated i want to store all the form data into SESSION.
This is the code to do it:
if(isset($_POST["address_submit"])){
insertSessionVars();
header("Location: http://localhost/project%20gallery/notes.php");
exit;
}
function insertSessionVars(){
foreach($_POST as $fieldname => $fieldvalue){
$_SESSION['formAddresses'][$fieldname] = $fieldvalue;
}
}
This works almost fine but stores also submit button value. I output it like this:
foreach($_SESSION['formAddresses'] as $value){
echo $value;
}
Any help will be greatful :)
Don't assign a name attribute to your submit button. If you assign a name then it is passed as part of the $_POST array.
<input type="submit" value="My Button" />
Since you no longer have the submit button in post, instead of checking if the submit button is set check to see if the post array contains data using count().
if(count($_POST) > 0)
The $_POST values are just array so after the for each why don't you use unset to drop the submit key from the array
That way you can check the form has been submitted and the either choose to not set the submit key or drop it rather then not giving it a name at all
Either way you can in your foreach do something like this (see *) and pass "$except" as parameter.
Where $except has to be the name of your submit button in insertSessionVars();
if(!in_array($key, $except)){...}
And ofc above your foreach something like this :
if(!is_array($except)) $except=array($except);
How do I maintain the $post value when a page is refreshed; In other words how do I refresh the page without losing the Post value
This in not possible without a page submit in the first place! Unless you somehow submitted the form fields back to the server i.e. Without Page Refresh using jQuery etc. Somesort of Auto Save Form script.
If this is for validation checks no need for sessions as suggested.
User fills in the form and submits back to self
Sever side validation fails
$_GET
<input type="hidden" name="first"
value="<?php echo htmlspecialchars($first, ENT_QUOTES); ?>" />
validation message, end.
alternatively as suggested save the whole post in a session, something like this, but again has to be first submitted to work....
$_POST
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
You can't do this. POST variables may not be re-sent, if they are, the browser usually does this when the user refreshes the page.
The POST variable will never be re-set if the user clicks a link to another page instead of refreshing.
If $post is a normal variable, then it will never be saved.
If you need to save something, you need to use cookies. $_SESSION is an implementation of cookies. Cookies are data that is stored on the user's browser, and are re-sent with every request.
Reference: http://php.net/manual/en/reserved.variables.session.php
The $_SESSION variable is just an associative array, so to use it, simply do something like:
$_SESSION['foo'] = $bar
You could save your $_POST values inside of $_SESSION's
Save your all $_POST's like this:
<?php
session_start();
$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];
// ETC...
echo "<input type='text' name='value1' value='".$_SESSION['value1']."' />";
echo "<input type='text' name='value2' value='".$_SESSION['value2']."' />";
?>
Actually in html forms it keeps post data.
this is valuble when you need to keep inserted data in the textboxes.
<form>
<input type="text" name="student_name" value="<?php echo
isset($_POST['student_name']) ? $_POST['student_name']:'';
?>">
</form>
put post values to session
session_start();
$_SESSION["POST_VARS"]=$_POST;
and you can fetch this value in another page like
session_start();
$_SESSION["POST_VARS"]["name"];
$_SESSION["POST_VARS"]["address"];
You can use the same value that you got in the POST inside the form, this way, when you submit it - it'll stay there.
An little example:
<?php
$var = mysql_real_escape_string($_POST['var']);
?>
<form id="1" name="1" action="/" method="post">
<input type="text" value="<?php print $var;?>"/>
<input type="submit" value="Submit" />
</form>
You can use file to save post data so the data will not will not be removed until someone remove the file and of-course you can modify the file easily
if($_POST['name'])
{
$file = fopen('poststored.txt','wb');
fwrite($file,''.$_POST['value'].'');
fclose($file);
}
if (file_exists('poststored.txt')) {
$file = fopen('ipSelected.txt', 'r');
$value = fgets($file);
fclose($file);
}
so your post value stored in $value.