Update hidden field - jquery - php

OK, going to try and explain this properly as I think thats why an answer is escaping me and some users are giving me negative replies as they think I have not tried with research to solve this problem.
I have a page where a user is presented with images within a slide viewer. Each item in the slide viewer is inside a LI element.
When a user chooses a slide, and the form is then submitted, I need the script to update a hidden field so I can take which slide they have chosen into a form. Either by updating onclick or when the form is submitted, it matters not when. The hidden field contains the URL of the image shown at that time.
So, the code I currently have is:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="/js/jquery.easing.1.3.js" type="text/javascript"></script>
<!-- this is the slideviewer code -->
<script src="/js/jquery.slideviewer.1.2.js" type="text/javascript"></script>
<!-- this is what I have been trying to update the hidden field -->
<script type="text/javascript">
$(document).ready(function(){
$( "#wl_add" ).submit( function ( event ) { /* form name and id = wl_add */
$('input[name="item_pic_url"]').val($('#mygalthree img').attr('src'));
} );
});
</script>
<!-- the div containing the slide viewer -->
<div id="mygalthree" class="svw"><ul>
<?PHP
foreach($html->find('img') as $e){ // from simple_html_dom
$image = $e->src;
echo '<li><img src="'.$image.'" width=300 alt="" /></li>';
}
// the hidden field to be updated - i am presuming needs to be after the loop which feeds the LI elements
echo '<input type="hidden" name="item_pic_url" value="'.$image.'" />';
?>
</ul></div>
Or, would it be easier to update the hidden field each time a slide is chosen? below you can see the code for the slideviewer - I would have thought I might be able to add
$('input[name="item_pic_url"]').val($('#mygalthree img').attr('src'));
when the user clicks? But I cannot find where to add this either and I have tried nearly every line! Slideviewer code:
jQuery(function(){
jQuery("div.svw").prepend("<img src='/images/spinner.gif' class='ldrgif' alt='loading...'/ >");
});
var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
settings = jQuery.extend({
easeFunc: "easeInOutExpo",
easeTime: 750,
uiBefore: false,
toolTip: false,
ttOpacity: 0.9
}, settings);
return this.each(function(){
var container = jQuery(this);
container.find("img.ldrgif").remove();
container.removeClass("svw").addClass("stripViewer");
var pictWidth = container.find("img").width();
var pictHeight = container.find("img").height();
var pictEls = container.find("li").size();
var stripViewerWidth = pictWidth*pictEls;
container.find("ul").css("width" , stripViewerWidth);
container.css("width" , pictWidth);
container.css("height" , pictHeight);
container.each(function(i) {
(!settings.uiBefore) ? jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>") : jQuery(this).before("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
jQuery(this).find("li").each(function(n) {
jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");
});
jQuery("div#stripTransmitter" + j + " a").each(function(z) {
jQuery(this).bind("click", function(){
jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
var cnt = -(pictWidth*z);
container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
return false;
});
});
container.bind("click", function(e){
var ui = (!settings.uiBefore) ? jQuery(this).next().find("a.current") : jQuery(this).prev().find("a.current");
var bTotal = parseFloat(jQuery(this).css('borderLeftWidth').replace("px", "")) + parseFloat(jQuery(this).css('borderRightWidth').replace("px", ""));
var dOs = jQuery(this).offset();
var zeroLeft = (bTotal/2 + pictWidth) - (e.pageX - dOs.left);
if(zeroLeft >= pictWidth/2) {
var uiprev = ui.parent().prev().find("a");
(jQuery(uiprev).length != 0)? uiprev.trigger("click") : ui.parent().parent().find("a:last").trigger("click");
}
else {
var uinext = ui.parent().next().find("a");
(jQuery(uinext).length != 0)? uinext.trigger("click") : ui.parent().parent().find("a:first").trigger("click");
}
});
jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
jQuery("div#stripTransmitter" + j + " a:first").addClass("current");
jQuery('body').append('<div class="tooltip" style="display:none;"><\/div>');
if(settings.toolTip){
var aref = jQuery("div#stripTransmitter" + j + " a");
aref.live('mousemove', function(e) {
var att = jQuery(this).attr('title');
posX=e.pageX+10;
posY=e.pageY+10;
jQuery('.tooltip').html(att).css({'position': 'absolute', 'top': posY+'px', 'left': posX+'px', 'display': 'block', 'opacity': settings.ttOpacity});
});
aref.live('mouseout', function() {
jQuery('.tooltip').hide();
});
}
});
j++;
});
};

I suggest the same as Josh V.K. but without that ludicrous positioning hack.
<label>
<img src="imagelocation.jpg" />
<input type="radio" name="coolName" value="image_1" style="display: none;" />
</label>

OK, this has been driving me insane, but it is now solved... In the actual slideviewer code, when a user chooses a number of an image, I added a simple onclick to write the value of the hidden field.
jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#' onClick=\"document.wl_add.item_pic_url.value='" + jQuery(this).find("img").attr("src") + "'\" >"+(n+1)+"<\/a><\/li>");
Thanks for suggestions.

What I would do, rather than updating a hidden field, is assign each image as a label for a radio button. Then when you submit the form it will submit the value of that radio button. You will need to position the radio button left:-9999 or something so that the actual button doesn't show up.
<label>
<img src="imagelocation.jpg" />
<input type="radio" name="coolName" value="image_1" style="margin-left:-99999px;>
</label>
When the click on the image, it will select that radio button and that value will be submitted with the form.

Related

Get drop down list options based on selected checkbox value

I have the following codes that generate checkboxes from database
< table class="table">
< thead>
< /thead>
< th>
<?php
$oaNamesQuery = "SELECT DISTINCT oaName FROM oaDetails";
$oaNamesQueryExecute = mysqli_query($conn, $oaNamesQuery);
while($oaNamesQueryRow = mysqli_fetch_array($oaNamesQueryExecute)){
$oaName = $oaNamesQueryRow['oaName'];
echo '<div class = "checkbox-group" required style="float:left; margin-right: 25px;"> <input class="checkBoxes" type="checkbox" id="checkBoxArray" name="checkBoxArray[]" value="'.$oaName.'"> '.$oaName.'</div>';
}
?>
< /th>
< /table>
There is another input box as below whereby input type is number
<div class="col-sm">
<label for="numberOfShiftPerDay">Number</label>
<input type="number" class="form-control" id="numberOfShiftPerDay" name="numberOfShiftPerDay" placeholder="No: " onchange="disablingRoutine()" min="1" max="4">
</div>
Example UI as below
When I enter some number, there will be a drop down menu appear according to the number I entered. Eg: If keyed in 1, there will be one drop down list will appear as below using jQuery 'OnChange'. The UI will be as below
What I Need
I need the drop down menu options to be based on user selected checkboxes. Eg: If the user selected checkboxes X, Y and XX, then these drop down list should show X, Y and XX. Can someone help me how to do this?
Edit 1
Added Javascript function on the change routine as suggested by stackoverflow member. But now having duplicate issues. Below the code that I changed
if ($("#numberOfShiftPerDay").val() == 1) {
$.each($("input[name='checkBoxArray[]']:checked"), function() {
cval = $(this).val();
$('#oaInShift1').append('<option>' + cval + '</option>')
});
} else if ($("#numberOfShiftPerDay").val() == 2) {
$.each($("input[name='checkBoxArray[]']:checked"), function() {
cval = $(this).val();
$('#oaInShift2').append('<option>' + cval + '</option>')
});
} else if ($("#numberOfShiftPerDay").val() == 3) {
$.each($("input[name='checkBoxArray[]']:checked"), function() {
cval = $(this).val();
$('#oaInShift3').append('<option>' + cval + '</option>')
});
} else {
$.each($("input[name='checkBoxArray[]']:checked"), function() {
cval = $(this).val();
$('#oaInShift4').append('<option>' + cval + '</option>')
});
}
You can get the selected checkbox values and append to your dropdown list in your onchange function.
Try this:
function disablingRoutine()
{
$.each($("input[name='checkBoxArray[]']:checked"), function(){
cval = $(this).val();
$('#dropdownID').append('<option>'+cval+'</option>')
});
}
Edit 2
function disablingRoutine()
{
cdata = '';
$.each($("input[name='checkBoxArray[]']:checked"), function(){
cval = $(this).val();
cdata += '<option>'+cval+'</option>';
});
$('#dropdownID').html(cdata)
}

Words trimmed tooltip jquery

Basically I want my tooltip to display company names once company_id is being hovered. However instead of displaying "Company A" for example, it just displays "Company". I realized that it's just printing everything before a space.
Here's the script:
<script type="text/javascript">
$(document).ready(function() {
// Tooltip only Text
$('.masterTooltip').hover(function(){
// Hover over code
var title = $(this).attr('title');
$(this).data('tipText', title).removeAttr('title');
$('<p class="tooltip"></p>')
.text(title)
.appendTo('body')
.fadeIn('slow');
}, function() {
// Hover out code
$(this).attr('title', $(this).data('tipText'));
$('.tooltip').remove();
}).mousemove(function(e) {
var mousex = e.pageX + 20; //Get X coordinates
var mousey = e.pageY + 10; //Get Y coordinates
$('.tooltip')
.css({ top: mousey, left: mousex })
});
});
</script>
and here's my codes
<?php
$companyCtrl = new company_controller();
$companyInfoArr = $companyCtrl->retrieveAllCompanyInfo();
foreach($companyInfoArr as $info) {
$company_id = $info->getCompanyID();
$company_name = $info->getCompanyName();
echo "<a href='#' title=".$company_name." class='masterTooltip'>".$company_id."</a> <br>";
}
?>
There's not problem when i manually enter the text like this
Your Text
In this line:
echo "<a href='#' title=".$company_name." class='masterTooltip'>".$company_id."</a> <br>";
You need to add quotes around the title, so the HTML you're generating will be properly formed. Like this:
echo "".$company_id." <br>";

Adding html form fields from user input

I have a form that asks a user to give a device type a name and say how many attributes they would like to assign to that device type. That form calls the below php files which uses a loop to create the desired number of attributes.
I have used the name="attribute".$i in order to be able to identify each attribute on the next php page to send the information to a database.
<?php echo $_POST['device-name']; ?>
<?php
$num=$_POST['number-of-attributes'];
$num=intval($num);
for($i=0; $i<$num; $i++) {
newAttribute($i);
}
function newAttribute($i) {
echo ("<div id=\"new-attribute\">");
echo ("<h3>New Attribute</h3>");
echo ("<label for=\"attribute".$i."\">Name</label>");
echo ("<input id=\"attribute\" type=\"text\" name=\"attribute".$i."\">");
echo ("</div>");
}
?>
However I also want the user to be able to click for example:
<div id="small-button">New Attribute</div>
and create another set of fields to define an attribute.
How would I do this?
Thanks in advance
Using javascript/jquery on the client-side:
var template = "<div class=\"new-attribute\">"
+ "<h3>New Attribute</h3>"
+ "<label for=\"attribute\">Name</label>"
+ "<input id=\"attribute\" type=\"text\" name=\"attribute\">"
+ "</div>";
$(document).ready(function() {
// The DOM has loaded
$("#small-button").on('click', function() {
// The user clicked your 'New Attribute' button
// Append a new attribute to the <body> with `template` we defined above:
$(body).append(template);
});
});
Note: I changed id="new-attribute" to class="new-attribute" since there will be more than 1.
You would need JavaScript for said job. In your HTML document, at the end of the body element add the following script element:
<script type="text/javascript">
var numberOfAttributes = 1;
function newAttributeFields(e) {
// Creates the div
var div = document.createElement('div');
div.id = 'new-attribute';
// Appends the fields
div.innerHtml = '<h3>New attribute</h3><label for="attribute' + numberOfAttributes + '">Name</label><input id="attribute" type="text" name="attribute' + numberOfAttributes + '">';
// Appends it to the body element
document.getElementsByTagName('body')[0].appendChild(div);
// Increments the number of attributes by one
numberOfAttributes = numberOfAttributes + 1;
}
var smallButton = document.getElementById('small-button');
// When the 'small button' is clicked, calls the newAttributeFields function
smallButton.addEventListener('click', newAttributeFields, false);
</script>

JQuery Slider Value with PHP Generated Input

Below is my code I have everything working except dynamically linking the sliders to each input field. Here is my live page
<?php $dirname="panos/" ; $images=g lob($dirname. "*.jpg");
foreach($images as $image) {
$imageName=s ubstr($image, -14); echo '
<img src="resize.php?w=450&img='.$image. '" />
<input id="'.$imageName. '-slider"/>
<br />
<div style="width:450px" id="'.$imageName. '" class="slider"></div>
'; } ?>
<script>
$(function () {
$(".slider").each(function () {
$(this).slider({
value: 0,
min: 0,
max: 360,
step: 1,
stop: function (event, ui) {
var v = $(this).attr('id')
var n = $(this).slider('value')
$("#" + v + "-slider").val(n);
window.alert(v)
},
create: function (event, ui) {
var v = $(this).attr('name')
var n = $(this).slider('value')
$("#" + v + "-slider").val('0');
}
});
})
});
</script>
FIGURED IT OUT!!! The files that was getting pulled by the PHP had ".jpg" in the middle of it! And even tho it is a valid ID name it ended up messing up the jquery portion of it! I just did this
$imageNameLong = substr($image, -14);
$imageName = substr($imageNameLong,0 , -4);
and took off that ".jpg" and now it works flawlessly!!!

Increment href value by one (Javascript)

I have got a ordered list
<li id="prev">
<a href="#fragment-1>Next</a>
</li>
I want to increment the href value to
<a href="#fragment-1">...
<a href="#fragment-2">...
<a href="#fragment-3">...
<a href="#fragment-4">...
When the next is clicked it should stop from 4 and return to 1 allso is it possible with javascript at all
Thank you
Give your link an id first to make it easier to select.
Then,
document.getElementById('the_id_here').addEventListener('click', function(e) {
var n = e.target.href.split('-')[1] * 1 + 1;
if (n > 4)
n = 1;
e.target.href = e.target.href.split('-')[0] + '-' + n;
}, false);
Example: http://jsbin.com/ajegaf/4#fragment-1
Not exactly what you want but should be close enough to aim you in the right direction, check http://jsfiddle.net/pj28v/
<ul id="list"></ul>
Next​
$(function() {
var $list = $('#list'),
count = 4,
i = 0,
cur = 0;
for (; i < count; i++) {
$list.append('<li>frag' + i + '</li>');
}
$('a', $list).eq(0).css('color','red');
$('#next').click(function(ev) {
ev.preventDefault();
cur++;
if (cur >= count) cur = 0;
$('a', $list).css('color','blue').eq(cur).css('color','red');
});
});​
A jQuery solution:
$(document).ready(function(){
$('#prev a').click(function(){
var href = $(this).attr('href');
var num = parseInt(href.substr(href.indexOf('-') + 1));
if(num == 4)
{
num = 1;
}
else
{
num++;
}
$(this).attr('href', '#fragment-' + num)
});
});​
http://jsfiddle.net/UYDdB/
A solution similar to Delan's one but with jQuery and without the usage of a test. http://jsfiddle.net/GZ26j/
$('#next').on('click', function(e) {
e.preventDefault();
href = $(this).attr('href');
var n = href.split('-')[1] * 1 + 1;
n = n%5;
e.target.href = href.split('-')[0] + '-' + n;
});​
Edit:
This solution makes the counter start at 0 and not 1
use a hidden field to hold the value, and an onclick function to increase it and submit the form. SEE: How can I increase a number by activating a button to be able to access a function each time the button is increased?
<?
if(!isset($_GET['count'])) {
$count = 0;
} else {
$count = $_GET['count'];
}
?>
<script type='text/javascript'>
function submitForm(x) {
if(x == 'prev') {
document.getElementById('count').value--;
} else {
document.getElementById('count').value++;
}
document.forms["form"].submit();
}
</script>
<form action='hidfield.php' method='get' name='form'>
<input type='hidden' name='count' id='count' value='<?php echo $count; ?>'>
</form>
<input type='submit' name='prev' value='prev' onclick="submitForm('prev')">
<input type='submit' name='next' value='next' onclick="submitForm('next')">

Categories