I am trying to print alert when user is redirected to login-redirect, however the alert isn't working as I want it to, instead it brings the user straight to the login-redirect.
Here's my code:
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<?php
session_start();
if( !isset($_SESSION["loginSuccess"]) ){
echo "<script type='text/javascript'>alert('Login failed!');</script>";
header('Location:' . $base_url . 'login-redirect');
}
?>
That's just how it works. You're doing a redirect, but it doesn't run the js before redirecting. If you want to run the js then you'll need to do the redirect different using a meta tag or JavaScript, rather than a http header.
The alert is working.
I think your session variable is not set.
Related
I am using the Scripts n Styles plugin with this code to auto-fill a logged in user's email into form on WordPress:
<script type="text/javascript">
window.onload = function () {
document.getElementsByName("Element-Name")[0].value = "<?php $current_user->user_email ?>";
}
</script>
However, when I refresh the page I get the following text (the quoted value in the code above) instead of the actual value.
<?php $current_user->user_email ?>
Any idea what I am missing here? Is this an issue with the plugin?
The first thing I see is that you don't output anything.
Try
<?php echo $current_user->user_email; ?>
But in your example it should have been just an empty value. There is something fishy with the PHP parser not detecting the PHP part.
I want to redirect to view with script in codeigniter. Script first and then page redirection.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function login()
{
$this->load->view('login');
}
}
I tried this code but it's not working because page is redirected before script completes it's executions. So, How to show notification in alert.
echo "<script>";
echo "alert('User not Found');";
echo "</script>";
redirect('home/login');
I tried with this also,
echo "<script>";
echo "alert('User not Found');";
echo "</script>";
echo "<script>setTimeout(\"location.href = 'http://localhost/dealsnow/index.php/home/login';\",300);</script>";
Second way is working for me but i think it's not good way. So is there any other option available for redirection to view after script completes it's execution.
Example : If i insert Form Data and then I want to show user that data is inserted properly in script and then i want the page to redirect.
Script first and then page redirection.
Solve my question,
If we want to execute script first and then after some timeout.
Ex. : if we want to show user that data is inserted properly in script and then i want the page to redirect.
echo "<script>";
echo "alert('Data Insereted Properly..!');";
echo "</script>";
$url = base_url().'/index.php/home/login';
header("refresh:3;url=$url");
use flash data in your code.
// Set flash data
$this->session->set_flashdata('error', 'User not Found');
// After that you need to used redirect function instead of load view such as
redirect("home/login");
// Get Flash data on view
$this->session->flashdata('error');
Hope it will help.
I have this code which uses jquery and ajax to send a request to the other page i.e.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<input type="button" id="butt" value="button11111111111111" >
</html>
<script>
$("#butt").on('click',function(e)
{
$.ajax(
{
type:'POST',
url:'testmysql.php',
data:
{
product_type:"cake";
}
});
});
</script>
When i click the above button it should send the request and data to the other file testmysql.php
<?php
session_start();
$_SESSION['type']=$_POST['product_type'];
echo $_SESSION['type'];
?>
However , when i refresh the other page after clicking the button i do not see any kind of echo and it gives me a notice stating
Undefined index: product_type
Since, i am new to ajax and jquery is there anything i am missing ?if yes,then what should i do to make this work ?
Thanks!
Note: Both of them are in the same directory.
First of all, remove the semicolon(;) from this statement,
product_type:"cake";
^
otherwise it will give you syntax error. And now comes to your issue.
when i refresh the other page after clicking the button i do not see any kind of echo and it gives me a notice stating Undefined index: product_type
That because when you refresh testmysql.php page, the $_POST array would be empty, and there would be no index named product_type in $_POST array. You can verify it using var_dump($_POST);.
On testmysql.php page you can check whether $_POST['product_type'] is set or not like this:
<?php
session_start();
if(isset($_POST['product_type'])){
$_SESSION['type']=$_POST['product_type'];
echo $_SESSION['type'];
}
?>
you added the ; at the end of the data object. remove it and try.
and you are sending a ajax request to the testmysql.php page, the $_POST['product_type'] will available when you are send the request. when you refresh the page the you are not sending any post request to testmysql.php page.
I want to check if the user is not logged in then he cannot access to the patient-dashboard page and redirect to index.php thats working fine but when i add an alert of javascript in patient-dashboard page where i redirecting the user to index.php if not logged in then it will first show alert box then it will go to index page thats not working... kindly help..
patient-dashboard.php
if(!isset($_SESSION['username'])) {
echo "<script type='text/javascript'>";
echo "alert('Login First');";
echo "</script>";
header('Location: index.php');
}
if(!isset($_SESSION['username'])) {
echo '<script type="text/javascript">;alert("Login First");window.location = "index.php";</script>';
}
You must send headers before anything else. The script is preventing the header from working. Redirect to an informational page instead of using a script, and use meta refresh to take them back to the index.
The informational page is just an ordinary HTML page that says, "You have to log in." You get them back to the index page like this:
<meta http-equiv="refresh" content="5;URL=http://www.exmaple.com/index/.html">
The "5" is five seconds. You can probably just put index.html in the URL; I haven't tested that.
Try
// patient-dashboard.php
if (!isset($_SESSION['username'])) {
$_SESSION['login_failed'] = 1;
header('Location: ./index.php');
exit; // eliminate errors and prevent the user from going any further
}
// index.php
if (isset($_SESSION['login_failed'])) {
unset($_SESSION['login_failed'];
die("Login First");
}
Or
// patient-dashboard.php
if (!isset($_SESSION['username'])) {
die("<script type='text/javascript'>alert('Login First'); location.href = './index.php';</script>");
}
Try this, you don't need to have the different parts of the alert in different echos, and the header redirect is now before the alert.
**EDIT**This refresh way works as a redirect and gives the alert
<?php
$var1 = 1;
$var2 = 2;
if($var1 < $var2) {
header( "Refresh:1; url=index.php");
echo "<script type='text/javascript'>alert('Login First')</script>";
}else{
echo "<script type='text/javascript'>alert('Logged In')</script>";
}
?>
I have a HTML form and PHP code. In my form, I have a textbox and a textarea. Within both, I have included the "disabled" option. I want both textboxes to remain disabled until the user decides to click the "Edit" button, in which case both textboxes should be enabled so changes can be made and the output once again saved. According to the research I have done, the only way to do this is to use javascript, so I have included the following code within my PHP;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "var oldnotes = document.getElementById('oldnotes');";
echo "oldnotes.disabled = false;";
echo "var record = document.getElementById('record');";
echo "record.disabled = false;";
echo "</script>";
}
I have also tried;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "$('#oldnotes').removeAttr('disabled')";
echo "$('#record').removeAttr('disabled')";
echo "</script>";
}
But no luck :(
I am not receiving any errors, the textboxes just remain disabled after I click the Edit button. Could anyone help me with this? Thanks in advance.
This is a better approach for these kind of problems :
if (isset($_POST['edit'])){
?>
<script type="text/javascript">
var oldnotes = document.getElementById('oldnotes');
oldnotes.disabled = '';
var record = document.getElementById('record');
record.disabled = '';
</script>
<?php
}
<?
<script language='javascript'>
(javascript code here)
</script>
?>
Try use onclick on your Button
<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">
I hope it helps.
The second approach seems correct, but you're missing ; there. Also, you haven't put any newline characters.
Here's what I suggest:
<?php
if (isset($_POST['edit'])) {
?>
<script type="text/javascript">
alert("Check entry"); // Use this for checking if your script is reaching here or not
$('#oldnotes, #record').removeAttr('disabled');
</script>
<?php
}
?>
You don't need to echo the whole thing. You can simply put it out of the PHP and keep it inside a if block.
Also, I've kept the alert to check if your code structure is proper. If you're getting the alert on clicking the edit button, means you're on the right path. You just need to workout on the JS.
If not, something wrong with your edit button and the form POST
Use single quotes for script type
echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";
use this jquery code:
<script>
$('#oldnotes,#record').attr('disabled','')
</script>
If you want to use JS with PHP you can read here:
how to write javascript code inside php
However: it seems you want the JS to be called on it own accord upon evaluation in the php logic as per the implementation provided.
Technically speaking your JS simply states it should re-enable the controls. Rather add the JS functionality to the OnClick of the submitting control and it should work fine. This is a quicker way of testing the functionality as well.
<a href="javascript:void(0);" onclick="document.getElementById('oldnotes').disabled=false;" >
Once it is clicked it will hide the appropriate control instead of trying to write new JS on the fly that is not executed. This is tried and tested and should work fine for you.
Hope it helps!
you can always make a cookie
use document.cookie()
set the onclick=“” to changing the cookie
and make a script that runs and gets the cookie when you open the site (use window.onload = function(){/*code goes here*/})