I have
<button type="button" onclick=" showUser($_SESSION["id"] )">Change Content</button>
Is there a way to do something like
if(isset($_SESSION["id"] )) showUser($_SESSION["id"] ); ?
showUser is an AJAX script that prints something.
I want it to run without any button press.
if (isset($_SESSION['id']))
{
echo '<script>showUser('.$_SESSION['id'].');</script>';
}
try
<?php if(isset($_SESSION["id"] )) {?>
<script> showUser('<?php echo $_SESSION["id"];?>');</script>
<?php }?>
You are messing HTML, JS and PHP to one code. But yes, it's possible:
<script>
<?php
if (isset($_SESSION['id']) {
?>
showUser(<?php echo $_SESSION['id']; ?>); // do js function call to ajax function
<?php } ?>
/* rest of js */
</script>
Related
This is my jQuery code
<script>
$(document).ready(function() {
$('#countries').change(function(){
$('#countriesForm').submit();
});
});
</script>
and my php code looks like this
<form action="<?php echo $this->createUrl($this->id."/".$this->action->id); ?>" id="countriesForm" method='POST'>
<?php echo CHtml::dropDownList('countries', $select = array(),
CHtml::listData($countries, 'code', 'name'));
?>
<form>
<div class='description'></div>
Can someone pls help me how to get echo/print name or code of the selected country into the div .description.
And how to send the selected code to other php script.
It can be like
<div class='description'><?php
if( isset($_POST['countries']) ) {
echo CHtml::encode($_POST['countries']);
}
?></div>
<?php
$controller = Yii::app()->controller->id;
$action = Yii::app()->controller->action->id;
?>
Using this, you get the controller and action performed.
<li <?php
if ($controller=="site"&&$action=="index"){
echo 'class="active"'; } ?> >
<?php echo CHtml::link('Home',array('site/index')); ?>
</li>
I am using the following code to delete a record from a database. But I am facing a problem: when I click on "cancel" in the confirm box then it deletes the record. If I click cancel it returns false but how does it delete the record?
What am doing wrong?
Javascript code:
function ConfirmDialog() {
var x=confirm("Are you sure to delete record?")
if (x) {
return true;
} else {
return false;
}
}
PHP code in view:
<?php
echo anchor('user/deleteuser/'.$row->id, 'Delete', array('class'=>'delete', 'onclick'=>"return ConfirmDialog();"));
?>
Everything in one line
<?=anchor("user/deleteuser/".$row->id,"Delete",array('onclick' => "return confirm('Do you want delete this record')"))?>
Try This:
Delete
and use this in your script:
<script type="text/javascript">
var url="<?php echo base_url();?>";
function delete(id){
var r=confirm("Do you want to delete this?")
if (r==true)
window.location = url+"user/deleteuser/"+id;
else
return false;
}
</script>
Are you getting any Javascript errors in the console? I suspect that some other Javascript may be interfering. This simple test page works fine:
<?php echo anchor('user/deleteuser/'.$row->id, 'Delete', array('class'=>'delete', 'onclick'=>"return confirmDialog();")); ?>
<script>
function confirmDialog() {
return confirm("Are you sure you want to delete this record?")
}
</script>
In your View:
<?= anchor("admin/delete_article/{$article->id}", 'Test', ['name'=>'submit', 'value'=>'Delete', 'class'=>'btn btn-danger', 'onclick'=>'return confirm()']);
?>
OR
<?=
form_open('admin/delete_article'),
form_hidden('article_id', $article->id),
form_submit(['name'=>'submit', 'value'=>'Delete', 'class'=>'btn btn-danger', 'onclick'=>'return confirm()']),
form_close();
?>
JavaScript in your View:
<script>
function confirm(){
job=confirm("Are you sure to delete permanently?");
if(job!=true){
return false;
}
}
</script>
See webGautam answer Here
I use This in my code
<a href='<?php site_url('controler/function/$id');?>' onClick='javascript:return confirm(\"Are you sure to Delete?\")'>Delete</a>
Try this
Delete
Then your function will be like:
function isconfirm(url_val){
alert(url_val);
if(confirm('Are you sure you wanna delete this ?') == false)
{
return false;
}
else
{
location.href=url_val;
}
<?=
form_open('admin/delete_noti'),
form_hidden('noti_id',$notification->id),
form_submit('submit','Delete',array('onclick' => "return confirm('Do you want delete this record')",'class'=>'btn btn-danger float-center')),
form_close();
?>
This is the deletion code. first of all we open form admin is our controller name and delete_noti is the function inside admin controller. We are sending id as hidden form. Here $notification is the variable which is passed through method which is available in controller.
Finally, if you want javascript confirm: so when the user clicks, it asks to confirm and then goes to delete if the user clicks okay. You're asking confirmation when the row has already been deleted.
In View Page :-
<td>DELETE</td>
Controller Code:-
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class user extends CI_Controller {
public function deleteuser($userid)
{
$this->db->delete('user', array('user_id'=>$userid));
$this->session->set_flashdata('success','User deleted Successfully!!!');
redirect(base_url().'admin/user');
}
}
?>
Then Show Success Message on View:-
<div class="row col-md-12">
<?php
if($this->session->flashdata('success')){
?>
<div class="alert alert-success " style="display:none;">
<?php echo $this->session->flashdata('success'); ?>
<?php
} else if($this->session->flashdata('error')){
?>
<div class = "alert alert-danger" style="display:none;">
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php } ?>
</div></div>
I'm working on a project that uses jQuery modals that dynamically load their content on a button click.
I am having an issue using this loaded content like I would normally.
Here is an example of my problem
<script type="text/javascript>
click function{
load modal{
open: $('#modalID').load('phpfile.php?id=<?php echo $id ?>');
}
</script>
That all works fine, but when trying to use jQuery within the "phpfile.php" is where the problem lies
<?php
$id = $_GET['id'];
$db = USE ID TO GET DATABASE INFO; //works fine here
?>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
When I click the button, I get the alert but it just says test and I don't get the ID like I should.
Thanks in advance for your help and suggestions on this one!
You have pasted pseudo code so it is difficult to say what is wrong in your code.
The following should work, give it a try:
File 1.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?
$id = "10";
?>
<script>
$(document).ready(
function(){
$('#modalID').load('phpfile.php?id=<?php echo $id ?>');
});
</script>
<div id = 'modalID'></div>
And phpfile.php:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?php
$id = $_GET['id'];
?>
<script>
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
<input type="button" id = "buttonID" value="click me">
Please check on the do you have any errors in your browser's console. The above code should work. The possibilities are like if you don't get your $_GET['id'] or any script errors will prevent execution of your code.
I have created a sample php file which is working fine.
<?php
$id = rand();
?>
<button id="buttonID"> Click </button>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
The PHP script doesn't seem to call dis(); function..Here it is:
PHP:
if (!$_SESSION['user']) {
echo"<script type='text/javascript'>dis();</script>";
}
JS:
<script type="text/javascript">
function dis() {
$(document).ready(function() {
$("#main_text_area").attr("disabled", "disabled");
});
}
When I place just $("#main_text_area").attr("disabled", "disabled"); it disables correctly...but I need to do it on a function call...Thanks for comments.
I'd recommend, instead of disabling the textarea, outputting the content of the <textarea> as static text.
Maybe something like this:
<?php if( !$_SESSION['user'] ): ?>
<div class="text">
<?php echo $textareaContents; ?>
</div>
<?php else: ?>
<textarea id="main_text_area">
<?php echo $textareaContents; ?>
</textarea>
<?php endif; ?>
The Javascript approach you're currently taking is trivially easy to get around.
How can I add script inside a php code? suppose i want to give an alert for a button click.. how can i do that??
You can just echo all the HTML as normal:
<?php
echo '<input type="button" onclick="alert(\'Clicky!\')"/>';
?>
<?php
echo"<script language='javascript'>
</script>
";
?>
You mean JavaScript? Just output it like anything else in the page:
<script type="text/javascript">
<?php echo "alert('message');"; ?>
</script>
If want PHP to generate a custom message for the alert dialog, then basically you want to write your JavaScript as usual in the HTML, but insert PHP echo statements in the middle of your JavaScript where you want the messages, like:
<script type="text/javascript">
alert('<?php echo $custom_message; ?>');
</script>
Or you could even do something like this:
<script type="text/javascript">
var alertMsg = '<?php echo $custom_message; ?>';
alert(alertMsg);
</script>
Basically, think about where in your JavaScript you want PHP to generate dynamic output and just put an echo statement there.
To avoid escaping lot of characters:
echo <<<MYSCRIPT
... script here...
MYSCRIPT;
or just turn off php parsing for a while:
?>
...your script here
<?php
You could use PHP's file_get_contents();
<?php
$script = file_get_contents('javascriptFile.js');
echo "<script>".$script."</script>";
?>
For more information on the function:
http://php.net/manual/en/function.file-get-contents.php
You mean you want to show a javascript alert when a button is clicked on a PHP generated page?
echo('<button type="button" onclick="alert(\'Alrt Text!\');">My Button</button>');
Would do that
You can insert script to HTML like in any other (non-PHP) page, PHP processes it like any other code:
<button id="butt">
→ Click ME! ←
</button>
<script>
document.getElementById("butt").onclick = function () {
alert("Message");
}
</script>
You can use onSOMETHING attributes:
<button onclick="alert('Message')">Button</button>
To generate message in PHP, use json_encode function (it can convert to JavaScript everything that can be expressed in JSON — arrays, objects, strings, …):
<?php $message = "Your message variable"; ?>
<button onclick="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button>
If you generate code for <script> tags, do NOT use htmlspecialchars or similar function:
<?php $var = "Test string"; ?>
<button id="butt">Button</button>
<script>
document.getElementById("butt").onclick = function () {
alert(<?=json_encode($var)?>);
}
</script>
You can generate whole JavaScript files, not only JavaScript embedded into HTML. You still have to name them with .php extension (like script.php). Just send the correct header.
script.php – The JavaScript file
<?php header("Content-Type: application/javascript"); /* This meant the file can be used in script tag */ ?>
<?php $var = "Message"; ?>
document.getElementById("butt").onclick = function () {
alert(<?=json_encode($var)?>);
}
index.html – Example page that uses script.php
<!doctype html>
<html lang=en>
<head>
<meta charset="utf-8">
<title>Page title</title>
</head>
<body>
<button id="butt">
BUTTON
</button>
<script src="script.js"></script>
</body>
</html>
Exactly the same way you add HTML tags. Echo it
One way to avoid accidentally including the same script twice is to implement a script management module in your templating system. The typical way to include a script is to use the SCRIPT tag in your HTML page.
<script type="text/javascript" src="menu_1.0.17.js"></script>
An alternative in PHP would be to create a function called insertScript.
<?php insertScript("menu.js") ?>
To add javascript inside a PHP code you can do this
<?php
echo "<script>alert('message');</script>";
?>
Better this
<?php
echo "<script src='myScript.js'></script>";
?>
And this for WordPress function.php file
<?php
$script= get_template_directory_uri() . '/myScript.js';
echo "<script src=".$script."></script>";
?>
In your php file you can do something like this :
<?
//Your php code
?>
<script type="text/javascript">
//Your javascript code
</script>
<?php //Your php code