Here's the html code:
<Div>
<Div #btn-group>
<button value=0><img />Foo</button>
<button value=1 class=active><img />Lor</button>
<button value=2 class=active><img />Bar</button>
<button value=3><img />Ips</button>
</div>
<select hidden multiple>
<option value=0>
<option value=1 selected>
<option value=2 selected>
<option value=3>
</select>
</div>
http://jsfiddle.net/n6ns4/5/
I dont want it to constantly be running. So I have been trying to call .on() and .click() and .trigger() on the buttons. Which would then if check for "active", then add a selected, else remove selected. But the buttons dont toggle to ".active" until after the click/on/trigger completes.
I dont care what you change, but the requirements:
button needs to contain dynamic image and text (which is why i'm not
using checkbox.)
and the result needs to be able to submit to a form.
eventually it's going to use php laravel for the content.
Basically, if a button is pressed on top, and has the class 'active'...
Then the corresponding select option should have the attribute 'selected'
Or... Simply a way to submit a group of separated, toggling buttons with a dynamic image on them, to a form.
oh I like that answer Kalley, this is what I got.
<script>
$(function() {
$("#btnGroup button").click(function(){
var $thisButton = $('#btnGrouSelect option[value'+$(this).attr('value')+']');
if($(this).hasClass('active')){
$(this).removeClass('active');
if($thisButton.is(':selected')){} else {$thisButton.prop("selected", false)}
} else {
$(this).addClass('active');
if($thisButton.is(':selected')){} else { $thisButton.prop("selected", true)}
}
//$thisValue = $(this).attr('value');
})
});
// BEGIN: jQuery functions on load
</script>
<Div>
<Div id="btnGroup">
<button value=0>[img0] 0</button>
<button value=1>[img1] 1</button>
<button value=2>[img2] 2</button>
<button value=3>[img3] 3</button>
</div>
<select multiple id="btnGrouSelect">
<option value=0>0</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<input type="submit">
</div>
So, something like this fiddle:
$(document).ready(function () {
$('div.partnerBtnGroup button').on('click', function (ev) {
var btn = $(this).toggleClass('active');
var uid = btn.prop("value");
$("select option[value='" + uid + "']").prop('selected', btn.hasClass('active'));
$('pre').html(JSON.stringify($('#partnersSelect').val()));
ev.stopPropagation();
});
});
I solved it using the method described here, trying to use select was a poor decision. answer source:
<button type="button"> used as form value on submit
jsfiddle:
http://jsfiddle.net/nQFxU/3/
code
HTML
<div id="btn-group">
<button type="button" data-name="uid0" class="btn" data-toggle="btn-input" data-target="#puBtn0" value="uid0">
<img src="http://placehold.it/50">
<br>Randy</button>
<input type="hidden" id="puBtn0" />
<button type="button" data-name="uid1" class="btn" data-toggle="btn-input" data-target="#puBtn1" value="uid1">
<img src="http://placehold.it/50">
<br>Dick</button>
<input type="hidden" id="puBtn1" />
<button type="button" data-name="uid2" class="btn" data-toggle="btn-input" data-target="#puBtn2" value="uid2">
<img src="http://placehold.it/50">
<br>Jane</button>
<input type="hidden" id="puBtn2" />
<button type="button" data-name="uid3" class="btn" data-toggle="btn-input" data-target="#puBtn3" value="uid3">
<img src="http://placehold.it/50">
<br>Alice</button>
<input type="hidden" id="puBtn3" />
<button type="button" data-name="uid4" class="btn" data-toggle="btn-input" data-target="#puBtn4" value="uid4">
<img src="http://placehold.it/50">
<br>John</button>
<input type="hidden" id="puBtn4" />
</div>
JS
$(document).ready(function () {
$('[data-toggle="btn-input"]').each(function () {
var $this = $(this);
var $input = $($this.data('target'));
var name = $this.data('name');
var active = false; // Maybe check button state instead
$this.on('click', function () {
active = !active;
if (active) $input.attr('name', name).val($this.val());
else $input.removeAttr('name').removeAttr('value');
$this.button('toggle');
});
});
});
conclusion
This works, it's a lot less hassle, and it's just as easy to put into effect. The downside to this is in my form submit result I have .html?ID1=ID1&ID2=ID2 when I was hoping for .html?users=ID1+ID2+ID3
Related
I need the sum the values from the selected radio button and add to the submit button.
Find the code below :
<?php foreach($configure['options']['option'] as $options)
{
?>
<label class="containerRadio">{{$options['name']}}
<input type="radio" class="calc" name="{{$name}}" value="
{{$configure['id']}}_{{$options['id']}}-{{$options['name']}}-
{{$options['pricing']['INR']['monthly']}}" checked="checked">
<span class="checkmark"></span>
</label>
<?php } ?> </div><br><br> <?php } ?><input type="text" name="sum"></button>
<button type="submit" name="sum"><i class="fa fa-rupee" aria-hidden="true">
</i> {{$key['monthly']}}
</button>
Fimd below the javascript :
<script type="text/javascript">
function calcscore(){
var score = 0;
$(".calc:checked").each(function(){
score+=parseInt($(this).val(),10);
});
$("input[name=sum]").val(score)
}
$().ready(function(){
$(".calc").change(function(){
calcscore()
});
});
</script>
Suggest a solution to add the value selected from the radio button to the default value given as "{{$key['monthly']}}".
JQUERY
When I clicked the both 2 buttons it only returns the value 1
$(document).ready(function() {
var getvalue = $(".view_btn").val();
$(".view_btn").click(function() {
alert(getvalue);
});
});
PHP
<?php foreach ($studentRankingViewGET as $studentRankingViewSHOW) {?>
<input type="button" value="<?php echo $studentRankingViewSHOW['id'];?>" class="view_btn">
<?php } ?>
This returned 2 values .i.e. 1 AND 2
$('.view_btn').click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="button" value="123" class="view_btn"/>
<input type="button" value="456" class="view_btn"/>
you should use $(this) as referring object.
Since you have two buttons with same class. You can get the clicked element with this.
$(".view_btn").click(function() {
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="view_btn" value="Button 1">Button 1<button>
<button class="view_btn" value="Button 2">Button 2<button>
This will support for dynamic content also
$(".view_btn").on("click", function() {
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="view_btn" value="Button 1">Button 1<button>
<button class="view_btn" value="Button 2">Button 2<button>
I have a module where a user can upload a profile picture. I want to do several things when a user uploads a picture to there profile, but I am trying to take it in steps, the first step being getting the picture saved and uploaded to the server. Right now, the user can select there picture just fine, after the user has chosen there picture, it gives a little notice that the picture is uploading. I don't know if something is supposed to show after uploading or if it is just supposed to go away (I joined a friend in getting this site done, the module was already here; I'm trying to get the functionality finished up and working).
This is what it looks like:
I have noticed though, that if you click save nothing seems to happen. Like the save button is not set up correctly or something.
I suspect that the the issue of the pictures not being uploaded is either because of the "Uploading..." text or the save button not working. I have tried to debug by echoing out if $_FILES is getting the picture that I am trying to upload, but the module does not reload to allow me to see if $_FILES is getting anything (it's not making it to the if loop of what happens after the save button has been clicked.
Here is the code for the module itself:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
</div>
<div class="modal-body">
<center>
<h3>Edit Profile Picture</h3>
<img src="images/default.png" name="aboutme" width="140" height="140" border="0" class="img-circle"></a></br></br></br></br></br></br></br>
</center>
<div class="modal-body">
<form id="cropimage" method="post" enctype="multipart/form-data" action="upload_pic/change_pic.php">
<strong>Upload Image:</strong> <br><br>
<input type="file" name="profile-pic" id="profile-pic" />
<input type="hidden" name="hdn-profile-id" id="hdn-profile-id" value="1" />
<input type="hidden" name="hdn-x1-axis" id="hdn-x1-axis" value="" />
<input type="hidden" name="hdn-y1-axis" id="hdn-y1-axis" value="" />
<input type="hidden" name="hdn-x2-axis" value="" id="hdn-x2-axis" />
<input type="hidden" name="hdn-y2-axis" value="" id="hdn-y2-axis" />
<input type="hidden" name="hdn-thumb-width" id="hdn-thumb-width" value="" />
<input type="hidden" name="hdn-thumb-height" id="hdn-thumb-height" value="" />
<input type="hidden" name="action" value="" id="action" />
<input type="hidden" name="image_name" value="" id="image_name" />
<div id='preview-profile-pic'></div>
<div id="thumbs" style="padding:5px; width:600p"></div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="save_crop" class="btn btn-primary">Save</button>
</div>
</div></br>
<div class="container">
<div id="profile_pic_modal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Upload Profile Picture</h3>
</div>
<div class="modal-body">
<form id="cropimage" method="post" enctype="multipart/form-data" action="upload_and_change.php">
<strong>Upload Image:</strong> <br><br>
<input type="file" name="profile-pic" id="profile-pic" />
<input type="hidden" name="hdn-profile-id" id="hdn-profile-id" value="1" />
<input type="hidden" name="hdn-x1-axis" id="hdn-x1-axis" value="" />
<input type="hidden" name="hdn-y1-axis" id="hdn-y1-axis" value="" />
<input type="hidden" name="hdn-x2-axis" value="" id="hdn-y2-axis" />
<input type="hidden" name="hdn-thumb-width" id="hdn-thumb-width" value="" />
<input type="hidden" name="hdn-thumb-height" id="hdn-thumb-height" value="" />
<input type="hidden" name="action" value="" id="action" />
<input type="hidden" name="image_name" value="" id="image_name" />
<div id='preview-profile-pic'></div>
<div id="thumbs" style="padding:5px; width:600p"></div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="save_crop" class="btn btn-primary" name="save">Save</button>
</div>
</div>
</div>
</div>
</div>
</br></br></br></br></br>
</div>
</div>
</div>
This is the function.js where I think the "Uploading..." is coming from:
jQuery(document).ready(function(){
/* When click on change profile pic */
jQuery('#change-profile-pic').on('click', function(e){
jQuery('#profile_pic_modal').modal({show:true});
});
jQuery('#profile-pic').on('change', function() {
jQuery("#preview-profile-pic").html('');
jQuery("#preview-profile-pic").html('Uploading....');
jQuery("#cropimage").ajaxForm(
{
target: '#preview-profile-pic',
success: function() {
jQuery('img#photo').imgAreaSelect({
aspectRatio: '1:1',
onSelectEnd: getSizes,
});
jQuery('#image_name').val(jQuery('#photo').attr('file-name'));
}
}).submit();
});
/* handle functionality when click crop button */
jQuery('#save_crop').on('click', function(e){
e.preventDefault();
params = {
targetUrl: '/upload_pic/change_pic.php?action=save',
action: 'save',
x_axis: jQuery('#hdn-x1-axis').val(),
y_axis : jQuery('#hdn-y1-axis').val(),
x2_axis: jQuery('#hdn-x2-axis').val(),
y2_axis : jQuery('#hdn-y2-axis').val(),
thumb_width : jQuery('#hdn-thumb-width').val(),
thumb_height:jQuery('#hdn-thumb-height').val()
};
saveCropImage(params);
});
/* Function to get images size */
function getSizes(img, obj){
var x_axis = obj.x1;
var x2_axis = obj.x2;
var y_axis = obj.y1;
var y2_axis = obj.y2;
var thumb_width = obj.width;
var thumb_height = obj.height;
if(thumb_width > 0) {
jQuery('#hdn-x1-axis').val(x_axis);
jQuery('#hdn-y1-axis').val(y_axis);
jQuery('#hdn-x2-axis').val(x2_axis);
jQuery('#hdn-y2-axis').val(y2_axis);
jQuery('#hdn-thumb-width').val(thumb_width);
jQuery('#hdn-thumb-height').val(thumb_height);
} else {
alert("Please select portion..!");
}
}
/* Function to save crop images */
function saveCropImage(params) {
jQuery.ajax({
url: params['targetUrl'],
cache: false,
dataType: "html",
data: {
action: params['action'],
id: jQuery('#hdn-profile-id').val(),
t: 'ajax',
w1:params['thumb_width'],
x1:params['x_axis'],
h1:params['thumb_height'],
y1:params['y_axis'],
x2:params['x2_axis'],
y2:params['y2_axis'],
image_name :jQuery('#image_name').val()
},
type: 'Post',
success: function (response) {
jQuery('#profile_pic_modal').modal('hide');
jQuery(".imgareaselect-border1,.imgareaselect-border2,.imgareaselect-border3,.imgareaselect-border4,.imgareaselect-border2,.imgareaselect-outer").css('display', 'none');
jQuery("#profile_picture").attr('src', response);
jQuery("#preview-profile-pic").html('');
jQuery("#profile-pic").val();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('status Code:' + xhr.status + 'Error Message :' + thrownError);
}
});
}
});
I have attempted to comment out the line jQuery("#preview-profile-pic").html('Uploading....'); but that does not seem to remove the "Uploading..." text and the picture still does not upload.
If someone could help me figure out why the module is not working correctly, I would greatly appreciate it. If there is an easier way to get the functionality I am looking for, please let me know.
From my analysis, I did ran the code and found lots of more errors but the following are the key issues to address first.
Your code uses an assigned header file, as seen here ,that carries all the snippets for CDN jquery files. if you would have opened your console while its 'Uploading...' you would have noticed an error like one in this pic
First impression is that no jQuery.form.js file was found which is responsible for this function. If you go further and confirm that it is correctly loaded, Use this function here to check its loading:
(function() {
var startingTime = new Date().getTime();
// Load the script
var script = document.createElement("SCRIPT");
script.src = 'dist_files/jquery.form.js';
script.type = 'text/javascript';
document.getElementsByTagName("head")[0].appendChild(script);
// Poll for jQuery to come into existance
var checkReady = function(callback) {
if (window.jQuery) {
callback(jQuery);
}
else {
window.setTimeout(function() { checkReady(callback); }, 20);
}
};
// Start polling...
checkReady(function($) {
$(function() {
var endingTime = new Date().getTime();
var tookTime = endingTime - startingTime;
window.alert("jQuery.form is loaded, after " + tookTime + " milliseconds!");
});
});
})();
if you would use this function to check if its correctly loaded, after then, if it loads, your next victim is your jQuery, this is because of compatibility issues, the jQuery.form.js version used requires a jQuery of 1.5.x or higher.
Secondly,you have to check that you don't have any conflict with your imports, if in your project you used a version like
Assuming you had used jQuery in your other functions before the profile thing came in, which in this case came with its imports.
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
then definitely you'll not have jQuery functioning, meaning, jQuery will no work, and also, the same guy needs to call jQuery.form.js, now you see you'll come back to the same error. so harmonize your imports and delete any duplicates in .js imports and also .css files(although there is no css conflict currently but just for your info)
Once you clean the imports(I advise you use CDN if you are developing with good network connection) clear your browser caches and redo the upload with 'inspect element option in your browser' Do this:
Right click an empty space on browser
select 'inspect element'
switch in the inspector to 'Console' and you'll be able to see the errors if any.
your end result should work and get something of this sort:
I need to send a Form with just the values append to the URL, like this:
http://thisistheurl.com/serv#search/VALUE1/VALUE2/VALUE3/VALUE4
I could send like this:
http://thisistheurl.com/serv?variable1=value&variable2=value&variable3=value&variable4=value#search/
The form is very simple.
<form id="consultatickets" name="consultatickets" role="form" method="get" class="tickets-form" action="http://thisistheurl.com/serv#search/" target="_blank">
<div class="row">
<div class="form-group col-md-12 col-sm-12">
<label for="ciudadorigen" class="tickets-imputs">Ciudad de Origen</label>
<select name="ciudadorigen" class="form-control ciudadorigen tickets-imputs" for="ciudadorigen">
<option selected disabled>1</option>
<option value="562">2</option>
<option value="582">3</option>
</select>
</div>
</div>
<!-- Here goes the rest of the form -->
<a type="submit" class="btn btn-warning waves-effect btn-block" href="javascript:{}" onclick="document.getElementById('consultatickets').submit();">Buscar <i class="fa fa-search" aria-hidden="true"></i></a>
</div>
I don't know how to extract the just the values from the variables and append to the URL.
If the order doesn't matter, you can achieve this by serializing the values of the form and appending it to the action attribute of the form to build the final url.
<form id="consultatickets" name="consultatickets" role="form" method="get" class="tickets-form" action="http://thisistheurl.com/serv#search" target="_blank">
<div class="row">
<div class="form-group col-md-12 col-sm-12">
<label for="ciudadorigen" class="tickets-imputs">Ciudad de Origen</label>
<select name="ciudadorigen" class="form-control ciudadorigen tickets-imputs" for="ciudadorigen">
<option selected disabled>1</option>
<option value="562">2</option>
<option value="582">3</option>
</select>
<label for="campo_adicional">Campo adicional</label>
<input id="campo_adicional" type="text" name="campo_adicional" />
</div>
</div>
<input type="submit" value="search"/>
</form>
$("#consultatickets").on('submit',
function(e) {
e.preventDefault();
var values = $(this).serializeArray();
var baseUrl = $(this).prop("action");
$.each(values, function(i, v) {
baseUrl += "/" + encodeURIComponent(v.value);
});
alert(baseUrl);
}
);
https://jsfiddle.net/kb3rvLjs/
Untested and I'm not sure if I got your question correctly but it seems you look for something like this:
// your form submit event handler
function formSubmit() {
var form = $('#consultatickets');
// build your url
var url = 'http://thisistheurl.com/serv#search/' +
getValues(form).join('/');
// redirect to your new location
location.href = url;
};
// function to get the values from the form elements
function getValues(form) {
// get all form fields in the form
var fields = $( "input, select", form);
var values = [];
// loop over the fields, add them to array
jQuery.each( fields, function( field ) {
values.push(encodeURI(field.val()));
});
return values;
}
In case you want to trigger the form submit with the a tag, simply change your onclick attribute to: onclick ="formSubmit();".
I'm working on a footer generator.
Which looks like this:
This "preview" button has 2 functions function 1 is posting the values that the user entered in the black box like this :
and the second function is to show me a button(which is hidden by default with css) called "button-form-control-generate" with jquery like this:
$("button.form-control").click(function(event){
$("button.form-control-generate").show();
});
Now here comes my problem:
If i click on preview it refreshes the page.. so if i click on preview it shows the hidden button for like 1 second then it refreshes the page and the button goes back to hidden. So i tried removing the type="submit" but if i do that it wont post the entered data like it did in image 2 it will show the hidden button though, but because the submit type is gone it wont post the entered data on the black box.
Here is my code:
<form class ="form" method="post">
<h3>Select your trademark</h3>
<select class="form-control" name="trademark" action="">
<option></option>
<option>©</option>
<option>™</option>
<option>®</option>
</select>
<h3>Your company name</h3>
<input class="form-control" type="text" name="companyName" placeholder="Your company name" />
<br/>
<br/>
<button class="form-control" type= "submit" name="submit">
Preview
</button>
<br/>
<button class="form-control-generate"name= "submit">
Generate
</button>
</form>
<!-- script for the preview image -->
<div id = "output">
<?php
function footerPreview ()
{
date_default_timezone_set('UTC');
$trademark = $_POST["trademark"];
$company = $_POST["companyName"];
$date = date("Y");
echo "<div id='footer_date'>$trademark $date $company </div>";
}
footerPreview();
?>
The jquery:
$("button.form-control").click(function(event){
$("button.form-control-generate").show();
});
Already tried prevent default but if i do this the users entered data doesnt show in the preview box. Looks like preventdefault stops this bit from working:
<!-- script for the preview image -->
<div id = "output">
<?php
function footerPreview ()
{
date_default_timezone_set('UTC');
$trademark = $_POST["trademark"];
$company = $_POST["companyName"];
$date = date("Y");
echo "<div id='footer_date'>$trademark $date $company </div>";
}
footerPreview();
?>
If you don't want to post the form you can use the preventDefault(); function.
$("button.form-control").click(function(event) {
event.preventDefault();
$("button.form-control-generate").show();
});
$("button.form-control").click(function(event) {
event.preventDefault();
setTimeout(function(){$("button.form-control-generate").show();},100);
});
Try It Once
Submit your form as
$('.form').on('submit', function(e){
$("button.form-control-generate").show();
e.preventDefault();
this.submit();
})
instead of using the click event. With button of type submit the submit() action is triggered automatically so you can show your button in the submit handler and then prevent the default action to refresh the page.
UPDATE:
Instead of using php you can do the same with jquery as well.
$('.form').on('submit', function(e){
e.preventDefault();
var date = new Date();
date = date.getYear() + 1900;
var data = $(this).serializeArray();
var str = data[0].value + date + data[1].value;
$('#output').text(str);
$('.form-control-generate').show();
})
.form-control-generate {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class ="form" method="post">
<h3>Select your trademark</h3>
<select class="form-control" name="trademark" action="">
<option></option>
<option>©</option>
<option>™</option>
<option>®</option>
</select>
<h3>Your company name</h3>
<input class="form-control" type="text" name="companyName" placeholder="Your company name" />
<br/>
<br/>
<button class="form-control" type= "submit" name="submit">
Preview
</button>
<br/>
<button class="form-control-generate" name= "submit">
Generate
</button>
</form>
<!-- script for the preview image -->
<div id = "output"></div>
#ShubhamKhatri almost had it right
The problem is, he thought this was an on submit event, I don't believe it should be. I believe you have the type="submit" on the wrong button. Surely preview wouldn't submit the form? You want to submit on generate, no?
so a slight change to the markup - added an id to preview to make life simple
<button class="form-control" id="preview">
Preview
</button>
<br/>
<button class="form-control-generate" type="submit" name="submit">
Generate
</button>
and a slight modification to the code in #ShubhamKhatri answer
$("#preview").click(function(e) {
e.preventDefault();
var date = new Date();
date = date.getYear() + 1900;
var data = $('.form').serializeArray();
var str = data[0].value + ' ' + date + ' ' + data[1].value;
$('#footer_date').text(str);
$('.form-control-generate').show();
});
et voilà