Please help me on this problem. It is small but causing me lot of trouble.I have set the session variable in my index.php and when clicked on anchor tag , it is not changing the session variable.
Relevant PHP part:
session_start();
$_SESSION["amount"] = "99";
Now the html part (in same PHP file).
<a onclick="change('<?php echo $_SESSION['amount']='399'; ?>')" href="#"><h4>Order 399</h4></a>
<a onclick="change('<?php echo $_SESSION['amount']='200'; ?>')" href="#"><h4>Order 200</h4></a>
I am checking the value of Session variable in another div in same file.
<td>Amount: <?php echo $_SESSION["amount"] ?> </td>
the value shown is 200 though clicked on "Order 399".
How do I change session variable?Am I mixing Client side and server side?
Please help me and do suggest the shortest possible way to do it.
Actually you are mixing it up!
It could be done like this:
You need a js-function, which calls a PHP-File, which then will change the value of the Session Variable.
And don't forget: Locate all the values on the currently loaded page via JS, because they won't change instantly. For sure they will change on Page-Reload, but if you want to avoid a page reload, then change the values, which are currently on the page.
jQuery ajax and append will be a huge help here.
If you have tried this and have still problems, then just ask again, but please try it to do first by yourself!
The last value of your session parameter is 200, you need to send an ajax request and then retrieve the latest changed value of your parameter.
you shouldn't change in in 'view' port.
Related
I'm using a shortcode as part of my membership setup which allows users to upload files directly to their contact record in my CRM. This displays a multi-upload form which I have then styled:
Once the files are uploaded the page refreshes and a success message appears. On refresh I also need to display a hidden div (#hiddendiv) underneath the success message.
From what I've read there is a JS/PHP combination that could potentially achieve this (such as this example - although this is quite vague and difficult to adapt).
Can anyone assist me/point me in the right direction to achieve this?
Since your form page is refreshing then you obviously not using AJAX, therefore instead of putting the div as html what you can do is echo a assign a variable initially it should be empty then, then echo that variable where you wanna show the div, this will echo empty message at the begging... then upon successfully validating your form and on success then assign the content of that div to the empty variable once the page loads its gonna show the div content u just assign
<?php
$hiddenDiv = "";
if(isset($_POST['submitButton'])){
//validate what you need to validate on
//On success assign value to hidden div
$hiddenDiv="<div class=\"whatever\">What ever dv content you need to display</div>";
}
?>
<!-- the place you wanna show the div -->
<?= $hiddenDiv?>
You could use the jquery switch class. This will allow you to do CSS transitions to make the appearance of the element smoother. This could be part of 'a' ajax response.
You could also just do something as simple as
$('#hiddendiv').css({"display":"inline"})
EDIT ** changed the to 'a' in reference to ajax.
Try like below:
<?php
if(isset($_POST['whatever'])
{
$display='';
}
else
{
$display='none';
}
?>
<div style='display:<?=$display?>'></div>
I have a intranet-website here that checks if an employee has used the form correctly and (via php) calls then a JavaScript part:
<script type="text/javascript">alert("Error!"); history.back(-1);</script>
Now, there is a php-variable with a huge amount of text. how can I call the site and give the variable with it, instead of using a simple history.back, where every variables are deleted instantly? Someone an idea how to make this without jQuery?
I think this is what you need:
<script type="text/javascript">alert("Error!"); location.href="page.php?yourvar=<?php echo $var; ?>";</script>
This will show the alert box and then redirect user to "page.php", where you can get your variable value using $_GET['yourvar'].
Hmm, not entirely sure what you mean but you could try something like this?:
if ($error) {
echo '<script type="text/javascript">alert('.$error.');</script>';
header('Location: page.php?var='.$myVar);
}
They should still get alerted with the error and then get sent back to the previous page, if this is what you were asking.
I have a contact form which initially "onsubmit" calls a javascript function to validate the content. If this function returns true it then posts the data to a php file.
At the end of this file I would like to return to the contact page, currently I am using this:
header('Location: ' . $_SERVER['HTTP_REFERER']);
is there a better way?
I would also like to change the css on an element on the contact page to display when I return (a thank you message), is this possible? I can set it to display using jQuery in the initial javascript function but that gets wiped out when it goes to the php file.
Thanks
Post to self then you if statement after post has been handled to redirect.
Do you redirect like you have said, using headers.
So make it post to self on the action part of your form
<form action="yourpage.php">
Then added PHP to handle post on yourpage.php
if(isset($_POST)){
//Handle POSTed data.
//if handled correctly
{
header("Location: somewhere.com")
}
}
The HTTP_REFERER is not the best solution to accomplish a history -1 in PHP.
As stated in the documentation :
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
The best solution is to explicitly write the page to return in the header function.
For the second question, you can redirect with a query parameter:
header('Location: script.php?valid=1');
And then test it in your view:
if (isset($_GET['valid']) && $_GET['valid'] == '1') {
// display the message
}
Make the form submit the data to the same page of the contact form.
Then from the PHP script check if a form has been submitted, do your processing code and return a div with your thank you message.
Say the name of your contact page was index.php. You could setup your form with the correct action (although it should be this way by default) and then check in the header for a submission parameter. Code below
<?php
if (array_key_exists("submit_btn",$_REQUEST) {
//display a thank you
}
?>
<!-- more code here-->
<form action="index.php">
<input name="whatever" type="text">
<input type="submit" name="submit_btn">
</form>
I found a few ways to do this in the end thanks to various peoples answers. Thanks all.
The simplest way was just to re-direct to a contact thank you page using the following header:
header("Location: http://{$_SERVER['SERVER_NAME']}/contact-thankyou.html");
I changed to using SERVER_NAME as HTTP_REFERER wasn't a great solution.
The second solution, which was a bit uglier but did the job was to return to the same page with a header like this:
header("Location: http://{$_SERVER['SERVER_NAME']}/contact.html?submitted");
Then use the following Javascript to detect the query parameter and change the css:
if (window.location.search.substring(1) == "submitted")
{
$("#message-sent").css({"display": "block"});
}
This is NOT a great solution, as I'd prefer to get the query parameter using php but I'm restricted to html and Javascript on pages with the template system I'm having to build on.
Thanks all.
I have a one form and onclick i want to remove image id from array without page refresh and after submit form i will update it in the DB.
I am using following code for this but no any success.
PHP Code:-
$total = count($oldimage);
//print_r($oldimage);
for($i=0;$i<$total;$i++)
{
$fimgsql="select * from cc_tbl_img_vid_upload where id='$oldimage[$i]'";
$gimgname=mysqli_query($db,$fimgsql) or die('Error');
$rw=mysqli_fetch_assoc($gimgname);
?><b id="rm<?=$i?>">Remove<?=$rw['upload_url']?></b><br/><br/>
<script>$( "#rm<?=$i?>" ).click(function() {
<?php unset($oldimage[$i]);?>
alert(<?php echo $oldimage[$i]; ?>);
$( "#rm<?=$i?>" ).hide();
});</script>
<?php
}
sort($oldimage);
$oldimage=implode(',',$oldimage);
Unfortunately you can't directly execute PHP (server-side) based on a JavaScript (client-side) event like you have in your code.
You need to handle your arrays in JavaScript. You can json_encode a PHP array and use the JSON array in your JavaScript, which is probably the closest you will get to doing what you're trying to do.
It's not possible the way you want to do it.PHP is serverside - which means you need to send a request to the server and get the response back(refreshing/changing page) while js is client side, and manipulates the UI, in order to achive that you must use AJAX (making a request from behind, and not seen by the user and getting the response, and based on it's output change the UI for the user)
Read this link - http://api.jquery.com/jquery.ajax/ ajax isn't that hard, but you must understand it, if you have php/mysql/html/css/js/jquery knowledge you will get it in no time.
I am using codeigniter to build a web app. I am having some problems with the following scenario.
The user clicks on submit button on 'home'
He is redirected to home/ok where I retrieve his info
from home/ok he is redirected back to home#final
But the thing is, final is hidden by default and it is not displaying. I have a JavaScript function that toggles final and it is displayed.
Is there anyway I can make the ok function redirect to the JavaScript function? I cannot add it on body onload since I don't want the success message to appear beforehand.
How about doing something like:
<? $display = ($_SERVER['HTTP_REFERER'] == base_url() . 'home/ok')? 'block': 'none'; ?>
<div id="final" style="<?=$display?>;">
OK
</div>
To set the default display style based on which way the page is displayed.
You could also look in to Flash data in the Session class, see. http://codeigniter.com/user_guide/libraries/sessions.html which I think is used for what I think you want to achieve