How to create a pop up window using php via echo? - php

Is it possible to make a pop up window in my existing script?
session_start();
$_SESSION['success'] = ($result) ? TRUE : FALSE;
header('location: inv_fc.php');
session_start();
if ($_SESSION['success'] == TRUE) {
// CREATE POP UP WINDOW SUCCESS
} else {
// CREATE POP UP WINDOW FAILURE
}

You can do it with Javascript. For nicer results, use jQuery UI.
if ($_SESSION['success'] == TRUE) {
echo "<script>alert('Success!');</script>";
} else {
echo "<script>alert('Failure.');</script>";
}

You could open a pop-up using javascript or target a's attribute, but it's impossible from PHP, which is executed at server side.
Edit: ok, as I saw the <script> things: it's not PHP, it's Javascript, from PHP it's not possible.

<?php if ($_SESSION['success'] == TRUE)?>
<script>window.open(...);alert('Your Awesome!');</script>
<?php else ?>
<script>window.open(...);alert('You Fail!!');</script>
<?php endif; ?>

Related

PHP session and hide table with jQuery

Is it possible to hide the table with jQuery if their is session present?
this is my session code
<?php
$username = $this->session->userdata('username');
if($username == TRUE){
echo "WELCOME ".$username;
}else{
echo "<div class='msg'>Please Log In</div>";
}
?>
and in my jQuery I dont know what to put in IF statement so I put it like this
if(".msg:visible"){
$('table').hide();
}
If possible I want to hide the table using jQuery
You can do using jQuery try below code
if ($(".msg").length > 0) {
$('table').hide();
}
if($(".msg").is(":visible"))
$('table').hide();
If you need to hide the table when session exists (when msg is not visible), you can try this with jQuery:
if (!$('.msg').is(':visible')) {
$('table').hide();
}
However, if you page reloads when your users log in, you could do it directly on the table without jQuery:
<table <?php echo $username == TRUE ? 'style="display: none"' : ''; ?>>
<!-- contents -->
</table>

PHP redirect if statement

I am using this if statement to redirect a user if the values in a .txt document is 0 but if it is 1 I want nothing to happen however I'm having some issues with my code.
This is my code currently:
$setup = require('setup.txt');
if ($setup === "0") {
echo '<script type="text/javascript"> window.location = "setup.php" </script>';
}
The setup.txt document currently contains the value 0.
I'd look here as to the proper usage of the require function.
if (file_get_contents('setup.txt') == "0") {
header('Location: /setup.php');
}
Use the header function if you have not already sent output to the browser.
$setup = file_get_contents('setup.txt');
if ($setup == "0") {
header('Location: /setup.php');
}
Since all PHP is executed before the output the site. Use this option first.
You can not use include() / require() to transfer as a variable, like you have. Use file_get_contents() to achieve the results.
try this:
<?php
$setup = file_get_contents('setup.txt');
if (trim($setup) == "0") {
echo '<script type="text/javascript"> window.location = "setup.php" </script>';
}
?>

Need help making an if statement in php that creates a html href link when passed correctly

<?php if ($_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
i want the link to only be created if the IF statement is successful and to not create it if it is not. At the moment no matter what i have tried to change the link will always be created. thanks.
Have you tried
<?php
if ($_SESSION["isLoggedOn"] == True)
{
echo 'LOGOUT';
}
?>
Your this visible code seems to be corrected. May be you would have some error in storing the sessions. Try:
echo $_SESSION['isLoggedOn'];
OR try doing this:
<?php
if ($_SESSION['isLoggedOn']) {
echo "Logout";
}?>
<?php if (isset($_SESSION["isLoggedOn"]) && $_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
Your $_SESSION["isLoggedOn"] is always TRUE.
Also dont use == TRUE for True/False variables.
Also logging out at terms and conditions page is not very good i guess ;-)
Elaborate your code:
<?php
session_start();
if ( !isset($_SESSION) || !isset($_SESSION["isLoggedOn"]) {
echo "an error occured instantiating the session.";
exit 1;
}
if ( $_SESSION["isLoggedOn"] === true ) {
echo 'LOGOUT';
}
?>
You need to start a session as very first action in your PHP.
Check if you actually have a vale in $_SESSION
Then, check its state and output what you want.

Use PHP sessions with jquery

Well, my question is simple:
I wish to use my PHP sessions in jQuery, in an if statement, however I do not know how I can do this on an easy way. Am I supposed to somehow import the $_SESSION in jquery?
What I basically want to do, is open a dialog if the User_ID is the same as the $_SESSION User_ID. Also I wish to have it checked to see if someone is an administrator, shop-manager or a user, with the session. This information already exists in PHP and the User_ID only exists in jQuery.
if($admin == true || gebr_id == $_SESSION['id']){
Javascript code here?
}
I think that would be the easiest way to solve it, however that does not seem to work for me. Perhaps I am forgetting tags for the PHP to understand it's javascript?
I've figured out an easy way to come around the solution, thanks you you all!
Solution:
$('.do').click(function(){
<?php
if ($admin == true) {
echo "$('#dialog').dialog('open');\n";
echo "var cid = $(this).attr('id');\n";
echo "var datum = cid.substr(0, 10);\n";
echo "var naampos = cid.indexOf('|');\n";
echo "var gebridpos = cid.indexOf('||');\n";
echo "var naam = cid.substring(naampos+1,gebridpos);\n";
echo "var gebrid = cid.substring(gebridpos+2);\n";
echo "$.ajax({\n";
echo "type: \"POST\",\n";
echo "url: \"agenda.php\",\n";
echo "data: naam,\n";
echo "success: function(){\n";
echo "$('#gebruikerinput').html(\"<input type='text' READONLY='' size='35' value='\" + naam +\"'>\");\n";
echo "$('#gebridinput').html(\"<input type='hidden' name='gebridtextbox' value='\" + gebrid + \"'>\");\n";
echo "$('#datuminput').html(\"<input type='text' READONLY='' size='12' name='datum' value='\" + datum + \"'>\");\n";
echo "}\n";
echo "})\n";
echo "return false;\n";
}
?>
});
It seems to be working so far. The only thing left for me to do is to make so jQuery returns a User_ID variable which needs to be identical to the session ID.
if($admin == true || gebr_id == $_SESSION['id']){
$javascript = "javascript code here";
}
and then in your html
<body>
<?php echo $javascript ?>
</body>
You cannot import the $_SESSION, but you can store some of them (only those that you want) in the cookie and read it in javascript.
For example: store the user-id in the cookie and you can read the value from the cookie in javascript.
read cookie:
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
Sadly that you can mix server-side script (PHP) with client-side script (Javascript). However, you can use PHP to echo Javascript, like:
if ($admin == true || gebr_id == $_SESSION['id']){
echo '<script>
alert("Admin!");
$("selector").function(); //
</script>';
}
Php is a server side language. javascript runs on the browser (client side).You can do this:
if($admin == true || gebr_id == $_SESSION['id'])
echo "<script>";
echo "here your jquery function";
echo "</script>";
}
So that you're just rendering HTML
hope it helps
<script type="text/javascript">
<?php if($admin == true || gebr_id == $_SESSION['id']){ ?>
alert('<?php print $admin; ?>');
<?php } ?>
</script>
also works.
Bypassing the apparently obvious security flaws with what you seem to be suggesting, you want to output the PHP session ID to your web page. Of course, JQuery is a client-side language, and it's pretty trivial to circumvent any client-side security of that nature.
<script type='text/javascript'>
<?php
//put in your variables from PHP
echo 'var phpId="' . $_SESSION['id'] .'";' ."\n";
echo 'var admin=' .$admin.";\n";
?>
//Back to Javascript (client side) here
if (gebr_id == phpId || admin){
...
}
</script>
You say you only have the user ID in JQuery. What you should do is code it so you have the user id available in PHP instead... Submit a form with the user id or something like that, then you can store that in the PHP session and do your authentication server-side.
If you like to get access to PHP Sessions from javascript, you can write a simple wrapper.
Somethign like this:
<?php
session_start($_GET['sid']);
echo json_encode($_SESSION);
?>
Keep in mind that this is a possible security flaw, as everyone can get access to your PHP Session variables from outside (with a valid session id).
Now you can simply use jQuery to ask the server for the current session data

javascript redirecting to main window doesn't take into account http option (&)

i'm triying to use javascript redirect to main window after log in succeed in an iframe.
this is my code :
if ($_GET['redirect']!='') {
$redirect=$_GET['redirect'];
$smart->assign('redirect',$redirect);
}
$redirect=$_GET['redirect'];
echo $redirect;
if(isset ($_SESSION['user'])&&$_SESSION['user']!='') {
$user->email=$_SESSION['user'];
$user->addCorporate();
$user->signIn();
$user->loadSession();
echo("<script language=\"javascript\" type=\"text/javascript\">");
echo "document.write('redirecting...');";
if ($redirect!='') {
echo 'self.parent.location = "'.$redirect.'"';
} else
echo 'self.parent.location = "index.php"';
//echo $redirect;
// redirect($redirect);
echo "</script>";
}
the echo $redirect displays http://xxxxxxxx/play.php?action=play&id=d59541b89828da34e9a8345a1bdafe2b
but the redirection is made to http://xxxxxxxx/play.php? (without the php option)
This sounds pretty mysterious.
Here's how I'd proceed: Turn off JavaScript in your browser and examine your created JavaScript. Then, at least, you know whether maybe for some bizarre reason the wrong URL is print out after all, or whether the problem's in the redirection part.
If you change the line:
echo 'self.parent.location = "'.$redirect.'"';
to this:
echo 'alert("'.$redirect.'")';
What happens?

Categories