i have code:
$search = $_REQUEST['search'];
if(!isset($_REQUEST['search'])){
if(is_null($_SESSION['ss_search']))
$search = 0;
else
$search = $_SESSION['ss_search'];
}
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
firstly I enter number 1 and submit, browser return 1. Then I enter number 2 and submit, browser return 2 but I refresh browser, it return 1. I don't know why session stored value 2 but when refresh it return old value. i used session_start() on top.
Please try this code
$search = $_REQUEST['search'];
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
Try this and let me know.
I am not sure but try to remove (NOT)! from the if statement. It worked for me.
change
if(!isset($_REQUEST['search'])){
to
if(isset($_REQUEST['search'])){
Please let me know if you face any problem.
I have tested your code and amend it as following:
$search ="";
if(!isset($_REQUEST['search'])){
if(is_null($_SESSION['ss_search']))
$search = 0;
else
$search = $_SESSION['ss_search'];
}else{
$search=$_REQUEST['search'];
}
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
using $_REQUEST['search'] without checking if it is set, cause it to generate undefined index notice. so now $_REQUEST['search'] is called only when it is set, Another cause is if you have set the default value on your form input field equal to 1, and when you refresh the page and allow it to resend the form, it may send the default value of your form.
Related
I am new to php, I need small help
I m creating a page in php named index.php
i want when someone view that page automatically a number or anything would be added to the url end
Like www.abc.com/index.php ---> www.abc.com/index.php?abc or ?132
when ever that index page is refreshed it should get a number or any variable in the end
Try this:
<?php
if (!isset($_GET["123"])) {
header("Location: " . $_SERVER["PHP_SELF"] ."?123");
}
$QS = $_SERVER["QUERY_STRING"];
$URL = "http://www.example.com/index.php";
// Check if Anything already assigned
if( empty($QS) ) {
// Generate Any RANDOM Number Here
$NUM = mt_rand(999, 9999);
// Reload Page and assign number
header("Location: {$URL}?{$NUM}");
}
Place this code in the very top of your page
Give this a go
<?php
$num = '123';
if(!isset($_GET[$num])){
header("Location: /path/to/page?$num");
}
Hi i've been curious on how to get the id in the url like php?pid=4 and use it in an update statement in sql. Well heres my code but cant get it worked because of undefined variable id which the value is in the url.
my function.php
function update_spot(){
$id=$_GET[pid];
if (isset($_POST['update'])){
$sql="UPDATE reports SET date_time_started='$_POST[date1]' ,
date_time_finished= '$_POST[date2]',
barangay='$_POST[brgy]',
street= '$_POST[street]',
owner='$_POST[owner]',
cause='$_POST[cause]',
motive='$_POST[motive]',
firfighter='$_POST[firefighter]'.
civilian='$_POST[civilian]',
ifirefighter='$_POST[ifirefighter]',
icivilian='$_POST[icivilian]',
occupancy='$_POST[occupancy]',
ed='$_POST[ed]',
alarm='$_POST[alarm]'
where id='".$id."' ";
if (!mysql_query($sql)){ die('Error: ' . mysql_error()); } ?>
<script type='text/javascript'>alert('sucessful changed try it next time you log
in.');window.location='view_inbox.php';</script> <?php
}
}
it seems i cant get id in the url. my url show like this in the form php?pid=5
It should simply be
$id = $_GET['pid'];
Use Quotes on your $_GET array access
$pid = $_GET['pid'];
Also, you are mixing $_GET and $_POST. You should use one or the other, depending on your form's method (GET or POST).
You also want to change all the areas you access it. IE
barangay = '$_POST[brgy]';
This should be, and all other lines after it
barangay = $_POST['brgy'];
$pid = isset($_GET['pid']) ? 0 : intval($_GET['pid']) //to avoid problems in this case
#Up, in the example above it still should work, but will cause php warning saying that undefined php constant will be assumed as string.
I would dump get global and see if variable is there, eg var_dump($_GET);
Got action with one waiting for parameter , and to run this action I need always one param. But also in this action I doing validation for other form and after this my first variable always disappears.
How I can keep this $var after isValid?
public function myAction(){
if ($this->getRequest()->isPost() || $this->getRequest()->getParam('number')){
//this is where got my number
$number = $this->getRequest()->getParam ('number');
//and use to display site.
if ($this->getRequest()->isPost()){
if($commentForm->isValid($this->getRequest()->getPost())){
//if I get Valid data I do upload or etc.
} else {
//but if form is inValid won't display everything one more time.
//but **$number is now Null**.
$this->view->data = $tUser->getCommentAndUserByTelephone($number);
$this->view->commentForm = $commentForm;
}
}
}
}
How I can keep this $number without repost?
You can try storing it with sessions.
http://www.w3schools.com/php/php_sessions.asp
$_SESSION['numbers'];
Pass that number to view $this->view->number = $number; and resend it instead of a new number. Simple!
I am redirecting to a different page with Querystring, say
header('location:abc.php?var=1');
I am able to display a message on the redirected page with the help of querystring value by using the following code, say
if (isset ($_GET['var']))
{
if ($_GET['var']==1)
{
echo 'Done';
}
}
But my problem is that the message keeps on displaying even on refreshing the page. Thus I want that the message should get removed on page refresh i.e. the value or the querystring should not exist in the url on refresh.
Thanks in advance.
You cannot "remove a query parameter on refresh". "Refresh" means the browser requests the same URL again, there's no specific event that is triggered on a refresh that would let you distinguish it from a regular page request.
Therefore, the only option to get rid of the query parameter is to redirect to a different URL after the message has been displayed. Say, using Javascript you redirect to a different page after 10 seconds or so. This significantly changes the user experience though and doesn't really solve the problem.
Option two is to save the message in a server-side session and display it once. E.g., something like:
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
This can cause confusion with parallel requests though, but is mostly negligible.
Option three would be a combination of both: you save the message in the session with some unique token, then pass that token in the URL, then display the message once. E.g.:
if (isset($_GET['message'], $_SESSION['messages'][$_GET['message']])) {
echo $_SESSION['messages'][$_GET['message']];
unset($_SESSION['messages'][$_GET['message']]);
}
Better use a session instead
Assign the value to a session var
$_SESSION['whatever'] = 1;
On the next page, use it and later unset it
if(isset($_SESSION['whatever']) && $_SESSION['whatever'] == 1) {
//Do whatever you want to do here
unset($_SESSION['whatever']); //And at the end you can unset the var
}
This will be a safer alternative as it will save you from sanitizing the get value and also the value will be hidden from the users
There's an elegant JavaScript solution. If the browser supports history.replaceState (http://caniuse.com/#feat=history) you can simply call window.history.replaceState(Object, Title, URL) and replace the current entry in the browser history with a clean URL. The querystring will no longer be used on either refresh or back/previous buttons.
When the message prompt ask for a non exsisting session. If false, show the message, if true, do nothing. session_start(); is only needed, if there is no one startet before.
session_start();
if ($_GET['var']==1 && !isset($_SESSION['message_shown']))
{
$_SESSION['message_shown'] = 1;
echo 'Done';
}
Try this way [Using Sessions]
<?php
//abc.php
session_start();
if (isset ($_GET['var']))
{
if ($_GET['var']==1)
{
if(isset($_SESSION['views']))
{
//$_SESSION['views']=1;
}
else
{
echo 'Done';
$_SESSION['views']=1;
}
}
}
?>
Think the question mean something like this?
$uri_req = trim($_SERVER['REQUEST_URI']);
if(!empty($_SERVER['REQUEST_URI'])){
$new_uri_req = str_replace('?avar=1', '?', $uri_req);
$new_uri_req = str_replace('&avar=1', '', $new_uri_req);
$pos = strpos($new_uri_req, '?&');
if ($pos !== false) {
$new_uri_req = str_replace('?&', '?', $new_uri_req);
}
}
if( strrchr($new_uri_req, "?") == '?' ){
$new_uri_req = substr($new_uri_req, 0, -1);
}
echo $new_uri_req; exit;
You can use then the url to redirect without vars. You can also do the same in js.
str_replace() can pass array of values to be replaced. First two calls to str_replace() can be unified, and filled with as many vars you like that needs to be removed. Also note that with preg_replace() you can use regexp that can so manage any passed var which value may change. Cheers!
I am running through a jQuery Ajax tutorial here:
http://www.charlieperrins.com/2011/03/ajax-jquery-101/
Everything works perfectly but I have a question about this piece of code:
<?php if ($_POST['user']) : ?>
<?php
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
?>
I am most concerned with the very first line. What does that line do? I am trying to keep all the code in 1 set of php tags but I don't know how to do that. If I knew what the first line does, I might be able to figure it out. Any help is appreciated. I am trying to reverse engineer this to fit it into my app but can't do it without knowing what that top line does.
Thanks.
All this does is continues the if block until endif.
There is no endif, so nothing in this script runs unless there is data in $_POST['user'] that doesn't evaluate to false.
I would write this a bit differently:
<?php
if (isset($_POST['user'])) {
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
}
?>
The first line tests if the $_POST array has a key user, and that key contains a "truthy" (non-empty, among other things) value, indicating that a form was posted to this script. If no form data was posted, the rest of the script won't execute, such as if someone browsed directly to this PHP script without using the expected form to post to it. It is a technique often used when a form posts back to the same PHP script. Upon first arriving at the script, the $_POST will be empty. When the form is posted back to the same script, different actions can be taken when it contains values.
There need only be one <?php tag:
<?php
if ($_POST['user']) {
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
}
?>
This is Alternative syntax for control structures
<?php if ($_POST['user']) : ?> means if $_POST['user'] evaluates to true, execute the following code.
It can be compressed down to this:
<?php if ($_POST['user']) :
$user_id = $_POST['user'];
....
Also,
if ($_POST['user']) :
should be
if (isset($_POST['user']) && !empty(trim($_POST['user']))) :
That makes sure that $_POST['user'] has been set (generally $_POST contains variables from a form), and that it is not empty even with white-space removed.
See
Alternative syntax for control structures
$_POST
empty
trim
The if ($_POST['user']) line is saying this:
If the variable $_POST['user'] exists and is set to a non-false value.
The above condition fails if $_POST['user'] is 0, false, or '' (empty string).
It also isn't safely checking that value.
You are better off using:
if (isset($_POST['user'])) && $_POST['user'] != '')
This way no warning is output when PHP has display_errors and notices turned on.