calling javascript function from PHP after form submit - javascript

i'm trying to call a javascript function after form submit and having any error then i want this script to run but this code is not working.I'm new in PHP Please help
if(!empty($varAccountType))
{
<?php
if($varAccountType=='Free Account')
{
echo "inside $varAccountType"; //this line is working
echo '<script type="text/javascript">OnSelectionAcctType(Free Account);</script>';
//only this is not calling//
}
?>}
please help..
Thanks in Advance

can you try by passing like below
echo '<script type="text/javascript">OnSelectionAcctType("Free Account");</script>';

Related

alert and redirect in codeigniter php using javascript not working

i have a function my controller, when the user clicks a button, the function runs and should show the alret box and then redirect the user to the same page, so i did the following code:
public function addtowishlist()
{
if($this->session->userdata('id'))
{
$id =$this->uri->segment(3);
$this->product->addtowishlist($id);
echo '<script type="text/javascript">alert("' . $pname . '")</script>';
redirect($_SERVER['HTTP_REFERER']);
}
}
however the issue is the alert is coming fine but the redirect to same page is not happening, only i get a blank page, can anyone please help me with this, thanks in advance
Your redirect will execute before alert since PHP is serverside and Javascript is client side, so what you need is redirect with javascript like this
public function addtowishlist() {
if($this->session->userdata('id')) {
$id =$this->uri->segment(3);
$this->product->addtowishlist($id);
echo '<script type="text/javascript">
alert("' . $pname . '");
window.location.href = "'.$_SERVER['HTTP_REFERER'].'"; ;
</script>';
}
}

Alert in PHP not working inside if

I want to put an alert inside an if (in a php document) with other actions. The other actions works fine but not the alert. Can anybody help me?. This is where the alert is:
if(empty($response)){
//SOME ACTIONS
echo '<script type='text/javascript'> alert("Message´s 1st line\nMessage´s 2nd line");</script>';
//SOME ACTIONS
header('Location: SOMEWEBSITE');
}else{
//ANOTHER ACTION
}
All actions works fine except for the alert. I tried on different browsers but the alert doesnt appear.
Sorry for my english
Thank you all.
It would be better if you use below mentioned code.
if(empty($response)){
//SOME ACTIONS
?>
<script type="text/javascript"> alert("Message´s 1st line\nMessage´s 2nd line");</script>
<?php
//SOME ACTIONS
header('Location: SOMEWEBSITE');
}else{
//ANOTHER ACTION
}
First thing is to fix quotes.
Second thing is remove header() call
http://php.net/manual/en/function.header.php
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
And don't use location header for 200 response
https://en.wikipedia.org/wiki/HTTP_location
Yes fix your quote.
The bellow code should work which you tried if you don't use redirection.
echo '<script language="javascript">alert("message");</script>';
You can't expect alert where you redirecting to another page from php. If you use javascript redirection, then it can be work. Replace redirection line with something like
echo '<script language="javascript">window.location = "http://SOMEWEBSITE.COM";</script>';
Try This echo '<script type="text/javascript">alert("Message´s 1st line\nMessage´s 2nd line");</script>'
echo "<script type='text/javascript'> alert('Message´s 1st line\nMessage´s 2nd line');</script>";
The code that you wrote the echo was terminating after
type='

how to dynamically change bootstrap alert test using php?

HTML:
<div id="signUpError"></div>
JS:
<script type="text/javascript">
function showSignUpError(message) {
document.getElementById('signUpError').innerHTML="<div class='alert alert-danger'>"+message+"</div>";
}
</script>
PHP:
$error="this is error"
How to call showSignUpError($error)
Try this code:
showSignUpError(<?php echo $error; ?>);
Try that first, but it might be necessary to quote the text:
showSignUpError("<?php echo $error; ?>");
Note that this will only run once, on page load.
If you wish to call-out to the PHP code while interacting with user, then you should check out AJAX:
AJAX request callback using jQuery

How would I call a java script function using php if statement with $_SESSION

Hi I am creating a website with a login section this is working I am using HTML and PHP. What I am trying to do is one of my pages has a html button I want this to be disabled for certain users. at the moment this is what I have got.
this is the part that I use for the login details.
<?php
session_start();
$_SESSION["username"];
$_SESSION["password"];
$_SESSION["access"];
?>
I have got if statments that I am currently using which are
if($_SESSION["access"] == "Administrator"){
echo $Admin;
}
what I am trying to do is call a javascript function within a PHP if statement what i have got so far is
<?php
if($_SESSION["access"] == "Consumer")
{
echo '<script type="text/javascript">
Disable();
</script>';
}
if($_SESSION["access"] == "Administrator")
{
echo '<script type="text/javascript">
Enable();
</script>';
}
?>
the javascript functions that i am trying to call are
<script type="text/javascript">
function Enable() {
SubmitButton.disabled = false;
}
function Disable() {
SubmitButton.disabled = true;
}
</script>
I have also tryed
if($_SESSION["access"] == "Consumer")
{
echo "<script> Disable(); </script>";
}
Im just wondering if I have typed something in wrong or if I have forgotten to put something in.
any help would be much appreciated.
Looking at your code you have couple of issues:
Mixing your PHP logic and pure HTML is (usually) not a good idea.
Instead I would suggest you move your access checking logic fully on the server side and display the button accordingly (disabled or enabled) based on the user's access.
Example:
<?php if($_SESSION['access']): // Only show the button for users with access ?>
<button type="submit" value="Submit" <?php echo ($_SESSION['access'] != 'Administrator' ? 'disabled' : ''); // Button disabled for everyone but administrators ?> />
<?php endif; ?>
And let me point out the obvious (as mentioned by the other answers), that's not 100% bulletproof. The user can still manually submit the button even if he is not an administrator by editing the page's HTML on the fly. That's just a UI fix. The real check should be done on the server side once the button is submitted (e.g. is the user logged in, does he have a cookie on his computer that identifies him as an administrator, does he have a session cookie set, etc).
Calling JS in random places, e.g. in the header can have unexpected consequences.
You better wait for the page to be loaded fully before calling any JS functions. You can do that via jQuery easily, but make sure you include the jQuery library before that in your header like so.
Afterwards you can call any JS after the page is loaded by placing them within the following block:
$(function(){
// Place your JS calls here, e.g. call to Enable()
});
String concatenation in PHP is done with a dot . and strings can be multiline
This code which you used is just plain wrong.
echo '<script type="text/javascript">'
, 'Enable();'
, '</script>';
You should use something like:
echo '<script type="text/javascript">'
.'Enable();'
. '</script>';
or better:
echo '<script type="text/javascript">
Enable();
</script>';
PHP doesn't use , sign for joining. Use ..
But otherwise it should work, except that you should define SubmitButton in advance of using it.
<?php
echo "<script type='text/javascript'>";
// if the id of your element is "submitButton"
echo "var submitButton = document.getElementById('submitButton');";
echo " function disable(){ submitButton.disabled=true; }";
echo "</script>";
?>
After that you can use it as you did..
<script type='text/javascript'>
disable();
</script>
Just be advised that denying access to some elements/functionality on your webpage with JavaScript alone is not a good practice - JavaScript is executed locally on the user's computer and therefore the user can modify it to gain an advantage.
Well, the problem may be that you're trying to call the javascript function before the HTML is ready (or finally rendered), so the browser, when executes the function doesn't find the button.
You could solve this placing your javascript code at the end of your page, or using jQuery and doing:
$(document).ready(function() {
<%php if ($_SESSION['access'] == 'xxxxx') {%>
Enable();
<%php } else { %>
Disable();
<%php } %>
});
Anyway, ALWAYS check user permissions on the server side, because someone could enable the button using Firebug or something else...

How to run php code in javascript when callback is called?

I'm using easyModal to create modal windows in my web. Saw that they have callback onClose and I want use php code on close is it possible, if yes How to do that? I want to unset some values from sessions (don't have much experience in javascript, jquery or ajax)
This is my code:
<div id="ebox">
<?php
echo $_SESSION['unameE'];
echo $_SESSION['pwordE'];
echo $_SESSION['emailE'];
?>
</div>
$(function() {
$('#ebox').easyModal( {
autoOpen:<?php echo $error; ?>
onClose:???
});
});
Or maybe there is another solution that unsets values from session after displaying it once?
on your script you should have :
onClose: function(){
$("#ebox").html(' ') ;
$.post("remove-session.php");
}
and then on the page "remove-session.php" you can do all sort of stuff with your session:
<?php
unset($_SESSION['unameE']);
//...
//or even
session_destroy();
?>

Categories

Resources