My project has an index.php file, and a utils.php file. I am trying to call a function in the utils.php file called dropdown. This function is going to generate a dropdown menu (I'm using Bootstrap to do that).
So, here is my index.php file:
<div class='container panel panel-default'>
<div class='panel-body'>
<!-- This is where I call the function to generate a dropdown -->
<?php require_once('utils.php'); dropdown(); ?>
</div>
</div>
And here is the utils.php file, containing the dropdown() function:
<?php
function dropdown () {
$str = "<form>
<div class='form-group'>
<label for='sel1'><h3>Pick a Year below:</h3></label>
<select class='form-control' id='sel1'>
<option>2016</option>
<option>2014</option>
<option>2013</option>
<option>2012</option>
</select>
</div>
</form>"
echo $str;
}?>
I am doing it this way, because in the future I'll have the dropdown options to be dynamic, using a database. Thanks everyone! ;)
Related
So i'm trying to make it so when I select something from the first select box will create a PHP variable.
Say in the select box I select option 1, it will create a php variable like $number = 1;. or if I select option 5 it'll create a variable like $number = 5;.
I know this require's ajax, but I have minimal to no experience with this at all.
Code:
<form class="form-horizontal row" id="select-service">
<div class="form-group">
<label class="col-sm-4 control-label">Select Social Media</label>
<div class="col-sm-4">
<select class="form-control" name="category" id="category" onchange="func(this.value)">
<?php
$stmt = $pdo->prepare('SELECT * FROM categories');
$stmt->execute();
if($stmt->rowCount() > 0) {
echo'<option selected="true" style="display:none;">Select a social media</option>';
} else {
echo '<option selected="true" style="display:none;">No social medias are available</option>';
}
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Select Service</label>
<div class="col-sm-4">
<select class="form-control" id="service" onchange="quantity(this.value)">
<option selected="true" style="display:none;">Please select a category.</option>
</select>
</div>
</div>
<ul class="pager wizard">
<div class="pull-right">
<li class="next">
<button href="#" class="btn btn-info"><i>Next</i></button>
</li>
</div>
</ul>
You cannot alter PHP variables on runtime (at least not from the same document you are executing the AJAX script). This is because PHP and jQuery are executed on diferent levels:
PHP is server side processing and jQuery happens on the client side.
Yes. AJAX can be used to make PHP requests but it does not exactly can alter variables in real time.
AJAX is a client side solution for calling external php files. in these files you could do basically anything you can do with a php request:
Read data from server
Insert data to server
Update parameters
Etcetera.
What you could do, depending on what you need is desing your site so what you need could be done using an external php file.
You call myfile.php?number=1 using AJAX
And then in myfile.php
<?php
$number = $_GET["number"]; // this sets the $number var to 1
//process or do whatever you need with the number
echo $number;
?>
This echo call will not be displayed on sceen as it normally is but instead you fetch it to Javascript or jQuery and process whatever you need it for.
I recomend you read the W3 Schools guide on AJAX for JS because it contains examples and it is very well explained on what you can do and how tho acomplish it.
I have a page where I have kept a dropdown where user can switch over languages. I am using theme so I have kept this dropdown on login.ctp page and posting form values to controller's method using Jquery. I have two form elements within login.ctp I don't know but my dropdown form element is not reaching to particular controller instead it is redirecting to login controller.
login.ctp
<form id="locale_form" method="post" action="changeLanguage">
<div id="language" class="language" align="right">
Please Select Language
<select id="languageselect" name="languageselect">
<option name="select" value="lang">Select A Language</option>
<option name="en_EN" value="en_EN">English</option>
<option name="de_DE" value="de_DE">German</option>
</select>
</div>
</form>
<div id="login-page">
<div class="container">
<!-- <form class="form-login"> -->
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create('',['class'=> 'form-login','id'=>'login_form']) ?>
-----------------Login Form Code----------------
Using Jquery onchange I am making switch over languages.
<script type="text/javascript">
$("select").change(function(){
$("#locale_form").submit();
});
</script>
My Userscontroller withing that method:
use Cake\I18n\I18n;
class UsersController extends AppController
{
public function changeLanguage()
{
/*I18n::locale('de_DE');
echo "reached here";*/
print_r($this->request->data());die();
if ($this->request->is('post'))
{
$lang = $this->request->data('languageselect');
$this->request->session()->write('locale', $lang);
I18n::locale($lang);
return $this->redirect(['action' => 'login']);
}
}
I don't know why But my form element with id="locale_form" is not reaching to its action element. I am using Cakephp3 and I don't know where I have messed up.
Add this method changeLanguage to allow like below.
$this->Auth->allow('changeLanguage');
I want to include/use bootstrap in all my controllers/views without having to load each css/js in each view file, so I have done this:
Installed Composer Bootstrap
composer require twbs/bootstrap
My Index Controller:
public function index() {
// Composer Autoloader
require VENDORPATH.'autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';
echo '<div class="section jumbotron text-center">Yu in index son.</div>';
}
VENDORPATH = myhomefolder../vendor/
vendor/autoload.php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit9d54f40b1177ed0ebd8d1d378ec06d06::getLoader();
/composer.json
{
"require": {
"twbs/bootstrap": "^3.3"
}
}
I don't know what to do now, I have searched all the web but it only says things about other packages or something not related and Im stuck in this right now, if someone in advance can help, I would appreciate, thank you.
First
create header and footer which consists of bootstrap css and js
and put them into a templates folder in your views folder
In controller use them like this to every method:
public function index() {
$data['title'] = 'home';
$this->load->view('templates/bootstrap_header',$data);
$this->load->view('index',$data);
$this->load->view('templates/bootstrap_footer');
}
What I do, is that I use a "main" template, which includes the css, js, head, body, etc... Then, inside my body tag, I do the $this->load->view('folder/function', $data); which processes the internal view... Do I make sense ???
Main View:
<html>
<head>
<?php $this->load->view('html/head', $view_data); ?>
</head>
<body lang="es">
<div class="container">
<?php $this->load->view('html/header')?>
<div class="contenido">
<?php if (isset($which_view)) $this->load->view($which_view, $view_data)?>
</div>
<?php $this->load->view('html/footer');?>
</div>
</body>
</html>
And finally, I create a small "inner-view" in a folder named after my controller, named after my function , so everything stays in sync...
<div class="formatoLogin modulo">
<?php echo form_open('login/doLogin', array('id'=>'formLogin', 'class'=>'form-signin'))?>
<?php $this->load->view('mod/notificacion')?>
<h1>Acceso Restringido</h1>
<div class="form-group">
<label for="Usuario" class="control-label">Usuario:</label>
<input type="text" name="Usuario" id="Usuario" class="form-control" />
</div>
<div class="form-group">
<label for="Contrasenia" class="control-label">ContraseƱa:</label>
<input type="password" name="Contrasenia" id="Contrasenia" class="form-control">
</div>
<div class="form-group text-center">
<button class="btn btn-info">Acceder</button>
</div>
<?php if($this->uri->segment(3)=='redirect'){?>
<input type="hidden" name="redirect" value="<?php echo $this->uri->segment(4).'/' . $this->uri->segment(5).'/' . $this->uri->segment(6) ?>">
<?php } ?>
<?php echo form_close()?>
</div>
Do I make sense? I hope it works cause it's saved me tons of includes...
We are toying about with the Open Source Flamer app. We noticed that they are able to call a function in a separate class located in a different PHP file via a form POST request. Here is an example of how they do it:
<div id="upldate_location_service" class="div_service">
<form action="process.php/upDetails" method="post">
<h3>Update location :</h3>
<div class="form_row">
<div class="form_label">Session Token *: </div>
<div class="form_field"><input type="text" name="ent_sess_token" />name= "ent_sess_token"</div>
</div>
<div class="form_row">
<div class="form_label"> </div>
<div class="form_field"><input type="submit" name="ent_submit" value="Submit" /></div>
</div>
</form>
</div>
The function they are trying to access is UpdateLocation in the file Process.php. We have tried to recreate this process but it wont work. We can echo outside of our class, but for some reason we cant access or run any content in our process.php function.
Here is a copy of our process document:
class LegitAPI Extends API{
public function upDetails($args)
{
echo "Hello World";
}
}
Theoretically, when submitting the form on the index page, it should load process.php and then the method upDetails in the LegitAPI Class and display hello world. However, a blank page is instead being displayed.
Any ideas?
I am a rookie PHP developer.
I have a PHP web project with an HTML page that contains an Add button. The name of the page is Awards.html. Elsewhere I have created a PHP class, Awards.php which contains a function.
The source code of my files is given as follows:
Awards.html
<div class="divparent">
<div class="modal-header">
<div class="btn-group">
<button class="btn" data-bind="click: closeModal">Exit</button>
</div>
</div>
<div class="modal-title">
<h1 id="headerid">Awards</h1>
</div>
<div class="modal-body">
<input id="hdnValueCurrentAwardSoid" type="hidden" value="null" />
<div class="divleft">
<input id="txtName" maxlength="80" type="text" class="water newpost1" placeholder="Award Name" tabindex="1" />
<section class="ThumbnailContainer">
<img id="imgThumbnail" class="imgThumbnail" />
<img src="http://localhost/rockontechnologies/Images/GreenRibbon.png" id="pictureribbon" class="pictureribbon" />
<input type="text" contenteditable="false" readonly id="transformtext" />
</section>
<textarea id="txtdescription" placeholder="Description" class="water newpost1" rows="4" tabindex="2"></textarea>
<div class="ui-widget">
<input id="txtIssueOrg" maxlength="50" type="text" placeholder="Issue Organization" />
</div>
</div>
</div>
<div class = "divbottom">
<div id="divAddAward">
<button class="btn" onclick="">Add</button>
</div>
</div>
</div>
Awards.php
<?php
class Awards
{
function clickFunction()
{
//Function that needs to be executed!
}
}
The problem here is that on the click event of the Add button, I need to call the clickFunction() of the Awards.php file. Can anyone please tell me how to do this?
Replies at the earliest will be highly appreciated. Thank you.
You should do something like that
<button class="btn" onclick="onrequest();">Add</button>
function onrequest() {
$.post(
'example.php'
).success(function(resp){
json = $.parseJSON(resp);
alert(json);
});
}
and in example.php call your function
$class = new Awards();
$method = $class->clickFunction();
echo json_encode($method);
and in your clickFunction();
clickFunction(){
$array = array(
'status' => '1'
);
return $array;
}
first of you have to a instance of class which is like that
$class = new Awards();
then if you want to onclick event you should make a javascript code but how to get the function you can do like this
<?php echo $class->clickFunction(); ?>
I think you can't call a class directly from HTML in PHP you have to call it through HTTP by loading a page.
Hence, use a AJAX call to call Awards.php. Eg. with JQuery it could look like this $.get("Awards.php", function() {
alert( "success" );
})
In your php-file you should also call your class something like this <?php echo $class->clickFunction(); ?>