Hi I'd like some help please. I have created a form in CodeIgniter in which I want to save the name of a movie and the actors who play in. My code looks like this:
<?php echo form_open().PHP_EOL; ?>
<div class="form-group">
<label for="title">Title *</label>
<?php echo form_input('title', set_value('title', html_escape($movie->title)), 'class="form-control"'); ?>
<?php echo form_error('title'); ?>
</div>
<h4>Cast</h4>
<button type="button" id="add_actor" class="btn btn-primary btn-sm">Add Actor</button>
<hr>
<ul class="list-unstyled" id="cast"></ul>
<div class="form-group">
<?php echo form_submit('save', 'Save', 'class="btn btn-primary"').PHP_EOL; ?>
</div>
<?php echo form_close().PHP_EOL; ?>
<script>
$(document).ready(function() {
var output = '<li style="margin-bottom:5px;">\
<div class="row">\
<div class="col-md-6">\
<?php echo form_dropdown("actors", $actors, null, "class=\"form-control\""); ?>\
</div>\
<div class="col-md-6">\
<input type="text" name="character[]" class="form-control"\ placeholder="Character" />\
</div>\
</div>\
</li>';
$('#add_actor').click(function() {
$('#cast').append(output);
});
});
On firebug I get this as output:
<script>
$(document).ready(function() {
var output = '<li style="margin-bottom:5px;">;\
<div class="row">;\
<div class="col-md-6">;\
<select name="actors" class="form-control">
<option value="5">Keira Knightley</option>
<option value="7">Matthew Goode</option>
<option value="6">Benedict Cumberbatch</option>
<option value="2">Christian Bale</option>
<option value="10">Jude Law</option>
<option value="4">Daniel Craig</option>
<option value="3">Brad Pitt</option>
<option value="1">Johnny Depp</option>
<option value="9">Sean Bean</option>
<option value="8">Pierce Brosnan</option>
<option value="11">Ed Harris</option>
<option value="12">Judi Dench</option>
</select>\
</div>\
<div class="col-md-6">\
<input type="text" name="character[]" class="form-control"\ placeholder="Character" />\
</div>\
</div>\
</li>';
$('#add_actor').click(function() {
$('#cast').append(output);
});
});
</script>
I probably guess that the error is caused because the form_dropdown() doesn't have the \ at the end of the line.
So what I'm trying to do basicly is when I click the "Add Actor" button the jquery will append a row in my form where it should have a dropdown menu for choosing the actor, and next to this dropdown an input field for typing the actor's character in the movie. When I click the "Add Actor" button again I will to the same process again to enter the next actor in the movie, etc etc.
However the code in the script tag doesn't work as I expected, In fact jquery complains for syntax error. I 'm checking my code and try some changes but no effect.
How can I make this work properly?
You should see an error like Uncaught SyntaxError: Unexpected token ILLEGAL in your console as you have ill formatted string literal.
Add \ at the end of each line for the output
var output = '<li style="margin-bottom:5px;">\
<div class="row">\
<div class="col-md-6">\
<?php echo form_dropdown("actors", $actors); ?>\
</div>\
<div class="col-md-6">\
<input type="text" name="character[]" class="form-control"\ placeholder="Character" />\
</div>\
</div>\
</li>';
You can't begin a new line at the middle of a string literal without telling the compiler that the string is continued in the next line, so in your case you can use the \ operator as given above to indicate that.
Demo: Fiddle
Related
I am using a stats app that will attach players (obtained from data within a database) to particular positions. I want players to disappear from being selected in forthcoming positions if they have already been selected so that the same player cannot be put in two separate positions. Can anyone suggest what to do here
This is the code, which i have summarised
<div class="row">
<div class="col-sm-4">
<label>1. Goalkeeper</label>
</div>
<div class="col-sm-4">
<select class="form-control">
<option value="" disabled selected>Select player</option>
<?php
include('conn.php');
$selectplayer= "SELECT * from panel";
$playerresult=$conn->query($selectplayer);
if(!$playerresult){
echo $conn->error;
}
while ($select=$playerresult->fetch_assoc()){
$identify=$select['id'];
$name=$select['name'];
echo "<option>$identify. $name</option>";
}
?>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label>2. Right corner back</label>
</div>
<div class="col-sm-4">
<select class="form-control">
<option value="" disabled selected>Select player</option>
<?php
include('conn.php');
$selectplayer= "SELECT * from panel";
$playerresult=$conn->query($selectplayer);
if(!$playerresult){
echo $conn->error;
}
while ($select=$playerresult->fetch_assoc()){
$identify=$select['id'];
$name=$select['name'];
echo "<option>$identify. $name</option>";
}
?>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label>3. Full back</label>
</div>
<div class="col-sm-4">
<select class="form-control">
<option value="" disabled selected>Select player</option>
<?php
include('conn.php');
$selectplayer= "SELECT * from panel";
$playerresult=$conn->query($selectplayer);
if(!$playerresult){
echo $conn->error;
}
while ($select=$playerresult->fetch_assoc()){
$identify=$select['id'];
$name=$select['name'];
echo "<option>$identify. $name</option>";
}
?>
</select>
</div>
</div>
Create a datalist with your players, and JQuery to remove from it when selected one using onchange() on the input:
function resetPlayerList(pos) {
//Get player name
var player = $('input#' + pos).val();
//If player exists in datalist
if($('option[value="' + player + '"]')) {
//Remove player from datalist meaning other inputs cannot add the player
$('option[value="' + player + '"]').remove();
}
//If changing a player, you need to add the old player back into the datalist. This code does not do that for you.
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<datalist id="players">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
<option value="5">
<!-- etc. -->
</datalist>
<input id="CF" onchange="resetPlayerList('CF')" list="players"> <!-- Center Forward -->
<br/><br/>
<input id="LW" onchange="resetPlayerList('LW')" list="players"> <!-- Left Wing -->
This will remove the player from the list using JavaScript/JQuery.
If you want to change a player after adding them in, you will need to do more work, to first re-add the old set player back to the datalist, before removing the new player from it... does that make sense?
UPDATE
<datalist id="players">
<?php
$players = array(
array(
'name' => 'Player 1'
),
array(
'name' => 'Player 2'
)
);
//$players should be your array/object of players from SQL.
for($players as $player) {
echo '<option value="' . $player['name'] . '" />'; //or $player->name if object
}
?>
</datalist>
I have 3 PHP Variable values with me which i want to display depending on the HTML option selected from the drop down menu without refreshing the page.
<p>
<label for="package" class="formlbl">I want to Enroll In:</label>
<select name="package" id="package">
<option selected="selected" value=""></option>
<option value="full_package">Complete Package</option>
<option value="training">Only Training</option>
<option value="counselling">Only Counselling</option>
</select>
</p>
What i want is when he selects full package it should echo variable no 1, when he selects training it should echo variable no 2 and so on without refreshing the page.
Please tell me what i should do. The values and variable are working in the code.
Since PHP is only a server-side language once the response is sent to the client, php can no longer change anything on the page without a refresh.
You will need to use javascript in order to have content that changes without reloading a new page (also called dynamic content). You can do this really easily with a framework called jquery.
Add jquery to your webpage with this html line:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
and then use this html template for your dropdown:
<!-- make each item in your dropdown an <a> with an ID -->
<ul class="dropdown">
<li onclick="toggleResponse('comp')">Complete </li>
<li onclick="toggleResponse('pack')">Package Only </li>
<li onclick="toggleResponse('train')">Training Only </li>
<li onclick="toggleResponse('couns')">Counselling </li>
<ul>
<!-- give every response the same class, and individual IDs -->
<span class="response" id="comp"><?php echo var1 ?></span>
<span class="response" id="pack"><?php echo var2 ?></span>
<span class="response" id="train"><?php echo var3 ?></span>
<span class="response" id="couns"><?php echo var4 ?></span>
<style>
// let all the responses be hidden initially
.response {
display: none;
}
</style>
<script type="text/javascript">
function toggleResponse (id) {
// hide any responses that might be showing
$('.response').hide();
// show the response for this option
$('#' + id).show();
}
</script>
This is probably the quickest and easiest way to do it.
Fiddle example: https://jsfiddle.net/te36zy18/
HTML
<p>
<label for="package" class="formlbl">I want to Enroll In:</label>
<select name="package" id="package">
<option selected="selected" value=""></option>
<option value="full_package">Complete Package</option>
<option value="training">Only Training</option>
<option value="counselling">Only Counselling</option>
</select>
</p>
<div id="full_package" class="package"><?php echo full_package;?></div>
<div id="training" class="package"><?php echo training;?></div>
<div id="counselling" class="package"><?php echo counselling;?></div>
jQuery
$("#package").on("change", function(){
var package = $(this).val();
$(".package").hide()
$("#" + package).show();
});
CSS
.package {display: none'}
as I am not experienced with php, I need some help solving my issue.
I pasted the snippet of php I believe needs editing.
<div class="selectBox" id="products-variation-container">
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation">
<option value="0"><?php _e('Select variation', 'wp-woo-leasing'); ?></option>
</select>
</div>
<!-- <input type="submit" name="btn-submit" class="button alt" id="leasingAddItem" value="<?php _e('Add Item', 'wp-woo-leasing'); ?>"
data-value="<?php _e('Add Item', 'wp-woo-leasing'); ?>" />-->
</div>
</div>
<div class="leasing-single-container" >
<div class="leasing-col1" >
<div id="product-description" class="product-description"></div>
<div id="product-price-details" class="product-price-details"></div>
</div>
My issue is that I want my code to be like:
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation">
<option value="0"><?php _e('Select variation', 'wp-woo-leasing'); ?>
If option value = "0" then hide the following div, upon loading site, since default is value is 0.
<div id="product-description" class="product-description"></div>
I hope you understand my question, and I hope that you can guide me to approaching this issue, thanks.
You can use jQuery to show and hide div on basis of select value.
Wordpress have already included jquery file, so you no need to add it.
(https://developer.wordpress.org/reference/functions/wp_enqueue_script/).
Here is code you can use:
<div class="selectBox" id="products-variation-container">
<select class="product-variation-dropdown" name="product-variation" id="sel-product-variation" onchange="getval();">
<option value="0">Select Option</option>
<option value="1">Select Option1</option>
</select>
</div>
<div class="leasing-single-container" >
<div class="leasing-col1" >
<div id="product-description" class="product-description">dgsdgsdgsdgsdgsd</div>
<div id="product-price-details" class="product-price-details"></div>
</div>
</div>
<script>
jQuery(document).ready(function() {
var sel = jQuery('select[name=product-variation]').val();
if(sel == 0) {
jQuery('#product-description').attr('style', 'display:none');
}
jQuery('#sel-product-variation').on('change', function() {
var sel = jQuery('select[name=product-variation]').val();
if(sel == 0) {
jQuery('#product-description').attr('style', 'display:none');
}
else {
jQuery('#product-description').attr('style', 'display:block');
}
});
});
</script>
Let me know incase of any concern for the same.
I have created a form with multiple fields like input type name, checkboxes and also a dropdown. My code for dropdown:
<div class="container">
<form action="selecttest.php" method="post">
<div class="dropdown">
<div class="dropdown-toggle somename1" data-toggle="dropdown"id="menu1"></div>
<ul class="dropdown-menu myclass numberom1" aria-labelledby="menu1" name="thenumbers">
<li value="one">One</li>
<li value="two">Two</li>
<li value="three">Three</li>
<li value="four">Four</li>
<li value="five">Five</li>
</ul>
</div>
<input type="submit" name ="submit"/>
I want to get the value of the selected <li> in the selecttest.php. The following code is not working:
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
How can I get the value of the dropdown in the php page? There are other elements in the form as mentioned above so i cannot use jquery onclick event. And this is a non ajax form.
Thanks.
A dropdown menu is not the same as a select input.
Within a form, the syntax for a select input (like yours, within Bootstrap) would be:
<label for="select_1">Select list:</label>
<select class="form-control" id="select_1" name="thenumbers">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>
So long as the select has an attribute name with value "thenumbers", and the select is within the form that is being submitted, you will be able to get the value of the selected element in PHP with:
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
To use the Bootstrap dropdown as a faux select, you can use JavaScript. Here is an example with jQuery:
HTML
<!-- Create dropdown and add specific class '.thenumbers' to ul element -->
<div class="dropdown">
<div class="dropdown-toggle somename1" data-toggle="dropdown"id="menu1"></div>
<ul class="dropdown-menu myclass numberom1 thenumbers" aria-labelledby="menu1" name="thenumbers">
<li value="one">One</li>
<li value="two">Two</li>
<li value="three">Three</li>
<li value="four">Four</li>
<li value="five">Five</li>
</ul>
</div>
<!-- Create a hidden input -->
<input type='hidden' name='thenumbers'>
jQuery
$(function(){
//Listen for a click on any of the dropdown items
$(".thenumbers li").click(function(){
//Get the value
var value = $(this).attr("value");
//Put the retrieved value into the hidden input
$("input[name='thenumbers']").val(value);
});
});
That way (as long as the hidden input is within the form), when the form is submitted, the "thenumbers" input value will be the last selected dropdown option.
Your form is not submitting because there is no submit button.Try this code.
HTML CODE
<form action="selecttest.php" method="post">
<div class="dropdown" >
<select class="form-control" name="thenumbers">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>
</div>
<input type="submit" name ="send"/>
</form>
PHP CODE
if(isset($_POST['send']))
{
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
}
When using Input group-bootstrap, you can get values in the PHP page by writing your code like this:
HTML/Bootstrap
<select name = "thenumbers" class="btn btn-outline-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<option value ="one" name ="one">One</option>
<option value ="two" name ="two">Two</option>
<option value ="three" name ="three">Three</option>
</select>
<input class="form-control border-secondary py-2" name="searchterm" type="text" >
<div class="input-group-append">
<button class="btn btn-primary" name="submit" type="submit" value ="su bmit">
<i class="fa fa-search"></i>
</button>
</div>
PHP code:
<?php
if(isset($_POST['submit']) && !empty($_POST['searchterm'])){
if(isset($_POST['thenumbers']){
$val = $_POST['thenumbers'];
echo $val;
}
}
You can remove the form element and use hyperlinks with url query string parameters. When a user clicks an option a GET request is made to the PHP file.
<ul class="dropdown-menu myclass numberom1" aria-labelledby="menu1">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
In your PHP script, use $_GET to retrieve the query string variable.
$val = $_GET["thenumbers"];
echo "the Value selected is ".$val;
I load my content from my DB with PHP, into my div.
I have a dropdown above it, where i should be able to sort out the content, meaning that when dropdown is chosen, the old content in the div is removed, and the new appears.
So how is it possible to "remove" the old content in the div, and get the new content based on the dropdown selection ?
This is what i got so far:
<div class="col-md-4">
<ul class="nav nav-tabs top_nav">
<li role="presentation" class="active">Edit</li>
<li role="presentation" class="inactive">
<!-- THE DROPDOWN THAT SHOULD SORT THE CONTENT BELOW -->
<!-- REMOVE OLD, AND GET NEW AFTER SELECTED -->
<select id="filter_type">
<option selected value="Alle">Alle</option>
<option value="Privat_Tale">Privat Tale</option>
<option value="Erhverv_Tale">Erhverv Tale</option>
<option value="Gamle">Gamle</option>
<option value="Privat_MBB">Privat MBB</option>
<option value="Erhverv_MBB">Erhverv MBB</option>
<option value="Familietele">Familietele</option>
<option value="Retention">Retention</option>
</select>
</li>
</ul>
<!-- LOAD THE CONTENT FROM DB, AND SHOW IT -->
<div class="abo_load" id="abo_load">
<?php
if (mysql_num_rows($sql_select_edit) > 0) {
while ($row = mysql_fetch_assoc($sql_select_edit)) {
?>
<div class="abo_box" id="abo_box">
<label class="abo_navn"><?php echo $row['abo_name']; ?></label>
<input type="hidden" value="<?php echo $row['category']; ?>" name="category" />
<form action="conn/delete.php" method="post">
<input type="hidden" value="<?php echo $row['id'];
?>" name="slet">
<input class="delete" value="Delete" type="submit" onclick="return confirm('Er du sikker??\nDet ville jo være ÆV med en Fejl 40!');">
</form>
<label class="edit">Edit</label>
<?php include("files/edit_list_content.php"); ?>
</div>
<?php
}
}
?>
</div>
</div>
EDIT:
I have already made it with JQuery, i load a php page, that gets the selected item, and then get the data from the DB. But the problem is, that when its loaded, i can't use any JQuery to manipulate with the new content. Like i have a edit label on the content with a JQuery hide function on. That function is working when i get the data though php from my DB, but not when i get the data though JQuery load. Thats why i want to load the data though php
You need to bind change event on your dropdown & based upon selection, fire an ajax request to get related content & then upon receiving update content inside DIV.
If you are using jQuery, Some useful functions would be:
Ajax: http://api.jquery.com/jquery.ajax/
Binding event: https://api.jquery.com/change/
A good example of implementation has been given here:
https://stackoverflow.com/a/4910803/2004910