How to add php code in JS file - php

Im using yii framework to do web development , text are call from specific php file in order to help me exchange the language. Some how i have to add some message from JS file, but i still need the yii php to call the text . How to do that? bellow if my coding
I would like to change "English: 26 character","Chinese: 16 character" and "Other: 16 character".
Because in yii calling text from other php is something like this:
In the labels.php is like this:
(eg: 'englishchar'=>'English: 26 character')
So to call the text is like this:
eg:<?php echo Yii::t('labels','englishchar');?>
$(function () {
$('#select_gametitle').change(function () {
var k = $(this).val();
if (k == "E") {
$("#txtgametitle").attr("placeholder", "English: 26 characters").placeholder();
$("#txtgametitle").attr('maxlength', '26');
}
else if (k == "C") {
$("#txtgametitle").attr("placeholder", "Chinese: 16 characters").placeholder();
$("#txtgametitle").attr('maxlength', '26');
}
else if (k == "O") {
$("#txtgametitle").attr("placeholder", "Other: 16 characters").placeholder();
$("#txtgametitle").attr('maxlength', '26');
}
});
$('input[placeholder], textarea[placeholder]').placeholder();
});
PHP code:
<div class="inputWrapper">
<div class="usetitle">* <?php echo Yii::t('labels', 'gametitle'); ?> :</div>
<select id="select_gametitle" name="select_gametitle" class="selectInput" style="width:369px;">
<option value=""><?php echo Yii::t('labels', 'select_gametitle'); ?></option>
<option value="E"><?php echo Yii::t('labels', 'english'); ?></option>
<option value="C"><?php echo Yii::t('labels', 'chinese'); ?></option>
<option value="O"><?php echo Yii::t('labels', 'other'); ?></option>
</select>
<div id="err_select_gametitle" class="error"></div>
<input id="txtgametitle" name="txtgametitle" type="text" class="textInput" style="width:352px;" placeholder="" />
</div>

You can add it as a global var inside tags in your main layout file.
/view/layouts/main.php:
<?php
Yii::app()->getClientScript()->registerCoreScript('jquery.ui');
Yii::app()->clientScript->registerCssFile(Yii::app()->clientScript->getCoreScriptUrl().'/jui/css/base/jquery-ui.css');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/styles.css" />
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
<?php Yii::app()->bootstrap->register(); ?>
<link rel="stylesheet" href="<?php echo Yii::app()->baseUrl; ?>/css/font-awesome-4.0.3/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->baseUrl; ?>/css/style.css" />
<script>
eg:<?php echo Yii::t('labels','englishchar');?>
</script>
</head>
<body>

You could add the script before the div, from PHP, for example:
<!--just add your script code-->
<script>
$(function () {
//your js code
<?php echo "with text printed by php"); ?>
});
</script>
<!--and now your html code, also with embedded PHP-->
<div class="inputWrapper">
<div class="usetitle">* <?php echo Yii::t('labels', 'gametitle'); ?> :</div>
<!--the rest of your div-->
</div>

You could echo out the translate into a JSON.
Yii::app()->clientScript->registerScript('test',
'var lang =' . json_encode( array(
'message1'=>Yii::t('app','Message 1'),
'message2'=>Yii::t('app','Message 2'),
)) . '; console.log(lang.word1)');
$this->render('json');
Extending from this, you could use AJAX and JS template

Related

How to display Select all option at last using bootstrap multiselect?

In multi select dropdown Select all option should be displayed at last after displaying all other options...
Ex : Select All
Black
Red
What i want is
Ex : Black
Red
Select All
i am referring below link
https://github.com/davidstutz/bootstrap-multiselect
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<meta name='viewport' content='width=device-width'>
<link href="<?php echo base_url();?>assets/css/fonts.css" rel="stylesheet" type="text/css" />
<script src="<?php echo base_url();?>assets/js/anglr/library/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="<?php echo base_url();?>assets/css/bootstrap.min.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="<?php echo base_url();?>assets/css/bootstrap-multiselect.css"
rel="stylesheet" type="text/css" />
<script src="<?php echo base_url();?>assets/js/anglr/controller/bootstrap_multiselect.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#lstFruits').multiselect({
includeSelectAllOption: true,
selectedBottom:true
});
});
</script>
<script type="text/javascript">
var web_link2="<?php echo base_url();?>";
$(function () {
$('#btnSelected2').click(function () {
var selected = $("#lstFruits option:selected");
var message = "";
selected.each(function () {
message += $(this).val() + "\n";
});
var array_split=message.split(',');
var document_array = document.getElementById('selectedtoken').value;
window.location.href = web_link2+"campaign/confirm_unsubscribe/"+document_array+'-'+message;
});
});
</script>
</head>
<body>
<div class="subscribe-Container">
<header class="subscribe-Header">
<a class="link"><img src="<?php echo base_url();?>assets/images/opend-logo.png" class="logo-img" alt="Opend" title="Opend"></a>
</header>
<div class="subscribe-Content">
<h1>admin#opend.com</h1>
<?php if(!isset($msg)||$msg=='')
{
?>
<p>Please Confirm your unsubscribe request Opend by Clicking on "Unsubscribe" button below to remove your email address</p>
<select id="lstFruits" multiple>
<option value="v"><?php echo $vertical_name?></option>
<option value="c">Contact List</option>
</select>
<input type="hidden" id="selectedtoken" value="<?php echo $mail_token_var?>"/>
<!--<button class="" id="btnSelected2" value="Unsubscribe"></button>-->
<a class="unsub_button" id="btnSelected2" >Unsubscribe</a>
<?php } else echo $msg; ?>
</div>
</div>
</body>
</html>
I tried using the below mentioned code
use this script for select all functionality for your multiselect dropdown
html
<select id="lstFruits" multiple>
<option value="v"><?php echo $vertical_name?></option>
<option value="c">Contact List</option>
<option id="select_all">Select All<option>
</select>
JS
$('#select_all').click(function() {
$('#lstFruits option').prop('selected', true);
});

how to make php variables show in loaded content

tried to ask this question earlier but made a total mess of it. so thought i'd try it again but clearer this time.
how can you get php variables to display in loaded content using JQuery?
index.php:
<!doctype html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
$(document).ready(function(){
$('#clickMe').click(function(){
$('#parent').load('loaded.php #child', {},function(){
});
});
});
</script>
</head>
<?php
session_start();
$test = "this should display php string";
$_SESSION['another'] = "Session variable String";
echo ' tests to see if they work below <br>';
echo $test."<br>";
echo $_SESSION['another']."<br><br>";
?>
<button name="clickMe" id="clickMe" class="clickMe">Click me</button>
<div class="parent" name="parent" id="parent" style="background-color:yellow; height:200px; width:200px;">
</div>
<body>
</body>
</html>
loaded.php:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div name="child" id="child" class="child">
<p1> html loads fine..</p1><br>
<?php echo $test ?><br>
<?php echo $_SESSION['another'] ?>
</div>
</body>
</html>
As stated by #Fred -ii- in the comments, you only have to fetch the session in your index.php file to do this.
If you want to get a part of another web page inside index.php :
Your index.php should contain this call :
$(document).ready(function(){
$('#clickMe').click(function(){
$('#parent').load('loaded.php'); // No need for additional parameters
});
});
You don't need to select a part of the HTML, return just what you need :
loaded.php :
<?php session_start() ?>
<p>Example text</p>
<?php echo $_SESSION['another'] ?>

How do I run my PHP code on the click of a button?

I'm trying to build a website for school project. I want the schoolResponse() function to happen after clicking the Submit button. If I remove the jQuery function it works but then it shows a default value when I load the page.
Here's my code:
<!DOCTYPE html>
<html>
<head>
<title>NHG</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link type="text/css" rel="stylesheet" href="css/normalize.css"/>
<link type="text/css" rel="stylesheet" href="css/style.css"/>
<link type="text/css" rel="stylesheet" href="css/resposive.css"/>
</head>
<body>
<header>
<div id="main-head">
<h2 id="main-heading">sKoolBook</h2>
</div>
</header>
<section>
<div id="container">
<div id="wrapper">
<form method="POST">
<select name="school">
<option value="none" name="none">Please Select a School...</option>
<option value="NHG">New Horizon Gurukul</option>
</select>
<input type="submit" class="button"/>
<?php
error_reporting(0);
$school = $_POST['school'];
function schoolResponse() {
if ($_POST['school'] == 'none'){
echo 'nothing';
} else {
echo 'something';
}
}
?>
<script>
$('.button').click(
function(
<?php schoolResponse(); ?>
);
);
</script>
</form>
</div>
</div>
</section>
<footer>
</footer>
</body>
</html>
You should use jQuery/AJAX to do this.
<script>
$('.button').click(
$.ajax({
url: 'yoururl.php',
data: $("form").serialize(), //Better to put an ID to the form or something
success: function(data){
//DO something with your data
}
})
);
</script>
There are other functions you should check to properly use ajax requests.
In yoururl.php you should do something like
$school = $_POST['school'];
function schoolResponse() {
if ($_POST['school'] == 'none'){
echo 'nothing';
} else {
echo 'something';
}
}
You can't achieve it like this. Here is one solution:
<?php
error_reporting(0);
$school = $_POST['school'];
function schoolResponse() {
if ($_POST['school'] == 'none'){
echo 'nothing';
} else {
echo 'something';
}
}
if($_POST['school']) schoolResponse();
?>
It would be better if you replace error_reporting(0); with error_reporting(E_ALL); for example, because otherwise php errors will be disabled.
And remove this part:
<script>
$('.button').click(
function(
<?php schoolResponse(); ?>
);
);
</script>
But it is very very basic example.

Autocomplete combobox in PHP

I have a combobox with filled info (filled by mysql_query) i want to add autocomplete.
$urun_bul="";
$sorgug = mysql_query("SELECT x FROM y order by z asc");
while( $yyy = mysql_fetch_array($sorgug))
{
$urun_bul.='<option value="'.$yyy["urun_id"].'">'.$yyy["urun_kodu"].' - '.$yyy["urun_adi"].'</option>';
}
Then prints $urun_bul
just try
click here
you will bind your option tag with mysql using this example...
OR
Example:
index.php file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="chosen.css">
<style type="text/css" media="all">
/* fix rtl for demo */
.chosen-rtl .chosen-drop { left: -9000px; }
</style>
</head>
<body>
<form>
<div id="container">
<div id="content">
<div class="side-by-side clearfix">
<?php mysql_connect("hostname","username","password"); mysql_select_db("database_name"); ?>
<div>
<em>Select option with DB using autocomplete</em>
<select data-placeholder="Choose a Country..." class="chosen-select" style="width:350px;" tabindex="2">
<option value=""></option>
<?php $sorgug = mysql_query("SELECT x FROM y order by z asc");
while( $yyy = mysql_fetch_array($sorgug)){
?>
<option value="<?php echo $yyy["urun_id"]; ?>"><?php echo $yyy["urun_kodu"].' - '.$yyy["urun_adi"]; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="chosen.jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var config = {
'.chosen-select' : {},
'.chosen-select-deselect' : {allow_single_deselect:true},
'.chosen-select-no-single' : {disable_search_threshold:10},
'.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
'.chosen-select-width' : {width:"95%"}
}
for (var selector in config) {
$(selector).chosen(config[selector]);
}
</script>
</form>
</body>
</html>
please copy chosen.css and chosen.jquery.js files from downloaded code from above link and past into your folder where your index.php file is locate.

codeigniter require no such file or directory [duplicate]

This question already has an answer here:
Codeigniter include not working?
(1 answer)
Closed 9 years ago.
I just got a very frustating error. I always use require 'file_name' when i tried to load a view (ex : header).
Im using CI now and i tried to do something like this : <?php require 'header_v.php';?> in my home_v.php but it always give me the No such file or directory error.
This is header_v.php :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Backend-Vihara Dharma Bhakti</title>
<link href="../../../style/style_backend.css" rel="stylesheet" type="text/css">
<link href="<?php echo base_url(); ?>style/style_backend.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="<?php echo base_url(); ?>jquery/jquery-ui-1.10.2.custom/css/ui-lightness/jquery-ui-1.10.2.custom.min.css" />
<script language="javascript" type="text/javascript" src="<?php echo base_url(); ?>jquery/jquery-ui-1.10.2.custom/js/jquery-1.9.1.js"></script>
<script language="javascript" type="text/javascript" src="<?php echo base_url(); ?>jquery/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
$(function() {
$( "#tabs" ).tabs();
});
</script>
<div id="header"><h1>HEADER</h1></div>
<div id="tabs">
<ul>
<li>Daftar Umat</li>
<li>Daftar Pengurus</li>
<li>Absensi</li>
</ul>
</div>
</head>
And this is home_v.php (the one that require header_v.php) :
<body>
<link rel="stylesheet" href="<?php echo base_url(); ?>jquery/jquery-ui-1.10.2.custom/css/ui-lightness/jquery-ui-1.10.2.custom.min.css" />
<script language="javascript" type="text/javascript" src="<?php echo base_url(); ?>jquery/jquery-ui-1.10.2.custom/js/jquery-1.9.1.js"></script>
<script language="javascript" type="text/javascript" src="<?php echo base_url(); ?>jquery/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<?php require 'header_v.php';?>
<div id="form_search">
<?php echo form_open('backend/index') ?>
<p>
<?php echo form_dropdown('ddl_search', $data_search, 'id="ddl_search"');?>
</p>
<p>
Kelas :
<?php echo form_dropdown('ddl_kelas1', $list_kelas, 'id="ddl_kelas1"');?> -
<?php echo form_dropdown('ddl_kelas2', $list_kelas, 'id="ddl_kelas2"');?>
</p>
<p>
Nama : <?php echo form_input('txt_nama');?>
Alamat : <?php echo form_input('txt_alamat');?>
Tanggal Lahir : <input type="text" id="datepicker" />
</p>
<?php echo form_submit('btn_search', 'Search');?>
<?php echo form_close(); ?>
</div>
<div>
<?php echo $table ?>
<?php echo $pagination ?>
</div>
</body>
</html>
What am i missing?Does it has something to do with config.php, routes.php or other CI's settings?Because i have use this way so many time and its always working except now.
Thanks for your help :D
NOTE : The file name is right, header_v and home_v is in the same folder(views).
The correct way to load a view file is this:
$this->load->view('file-in-views-folder')
To use the require function in CI, you have to type the full path of that file.
Like:
<?php require '/var/www/main/application/views/header_v.php';?>
But, the correct way using CI, is to load the view in your controller.
$this->load->view("header_v");
and then
$this->load->view("home_v"); //remove the require function from your code.
Why don't you load in your controller file?
$this->load->view('header_v');
$this->load->view('home_v');
if you want to output somenthing use
echo $this->load->file('/path/to/myfile');
if it's a view
echo $this->load->view('myviewname');
if it's a php library or an helper (so you haven't to output somenthing but just require or include classes and methods):
$this->load->helper();
$this->load->library();
Remember
views has to stay under /application/views folder
libraries under /application/libraries
helpers under /application/helpers
general files types can stay where you want since you use to load them with the enteire path to them /path/to/my/file

Categories