Load a page, give a variable on alert of JavaScript - javascript

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.

Related

Display hidden div after successful form upload

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>

how to redirect with js and alert if turn in previous page?

i want to redirect to a php page for example example.php and make possible not to turn back to the first page,if try to turn i must alert the user.i dont want do delete the page from hisotry.
Any of that insrtuction:
window.open('example.php','_self') or
location.href:example.php or
header("location: example.php) or
or location.replace('example.php;)
cant do what i want.
is there any javascript method or php function to make that?
Thanks in advance!
In Javascript you can use location.replace() to assign a new url - it forgets the previous location ( removes from history )
location.replace( 'example.php' );
For reference, try MDN

Changing PHP Session variables on click event

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.

Server side validation results appear in the same browser window

Simple HTML form:
<form action="test.php" method="post" name="my_form" id="my_form" >
<input name="c_name" type="text" id="c_name" maxlength="90"/>
</form>
test.php with the following code:
<?php
$name=$_POST['c_name'];
if ((!isset($name)) || ($name==''))
{
echo 'Fill in the name';
}
else
{
echo $name;
}
?>
Now, I want the following upon form submission:
1) Not to leave the current browser window as PHP to display the corresponding message.I want the form window to stay there.
2)I do not want just a simple response message. For example, I want instead of "Fill in the name" a more graphical way to interact like making a hidden div to appear; in other words I want to write Jquery inside PHP and make it execute. Is it possible and how?
I know I have to use AJAX, however I do not know what to do exactly! Could you give me some code as to realise the above two things?
I believe that if you're new to all of this, as you seem to are, you are best of using jQuery indeed. An ajax example can be found here: http://api.jquery.com/jquery.ajax/
Code in this case is q bit extensive. Cause you would not only want to check if it is in fact empty, but you also want to preg_match it. Which means you want to make sure that what your users send you actually want (and not break or hack or inject your server, which is important).
So seeing this I would recommend to read a tutorial, something like this (fast google search): http://code.tutsplus.com/tutorials/5-ways-to-make-ajax-calls-with-jquery--net-6289
And familiarize yourself with some basics before getting into the next steps.
To get the effect you are wanting. I like to use the jQuery Form Plugin, it makes it easy to create an AJAX form out of any existing form. Then simply echo your results in the .php file you submit to (Hint: you can echo div's with styling too.) The below example, assuming you have the jQuery Form Plugin included on your page, will submit the form using AJAX and return the echoed results to a div with id=status.
$(document).ready(function(){
var status = $('#status');
$('#my_form').ajaxForm({
beforeSend: function() {
status.empty();
},
complete: function(xhr) {
status.html(xhr.responseText);
}
});
});

Redirect to a hidden div

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

Categories

Resources