I got a loop of checkboxes. I put a hidden field inside with different value and different id:
<form>
<?php
for($y=0;$y<$len;$y++)
{
echo "<div class='proc'> <pre>";
echo "<h6 >Process: ".$proc[$y]." " ;
echo "People required: ".$num[$y].". ";
echo "<span class='assigned' name='assigned[]' >People Assigned: 0. </span> <input id='unchecked' type='hidden' name='link[]' value='0'/><input id='checked' type='checkbox' name='link[]' value='1'><i title='Activate Process' class='fi-link'></i>Link Process</input></h6></pre>";
?>
<div class="procLeader">
<label>Leader:</label>
<ol>
<li class="placeholder"><div class="adding">Drop Here</div></li>
</ol>
</div>
<?php
echo "</div>";
}
?>
<input type = "submit" style="margin-left:300px; width:150px;" id="savebutton" name ="submit" class="button" value = "Create Project" onclick="userSubmitted = true;"/>
</fieldset>
</form>
What I want is to submit empty checkboxes also. and if user doesnt tick it set process column to 0 in database, and if ticks to 1. The problem is form sending both variables 1 and 0 if i tick the box. I tried to disable the one boxes which are ticked in javascript:
$(document).ready(function () {
if(document.getElementById("checked").checked) {
document.getElementById('unchecked').disabled = true;
}
});
But I still got both values. Is there a way to disable hidden inputs if checkboxes are checked?
Related
my problem is to get a dynamic total for each checkbox group coming from a PHP loop
unique array-items comes from mysql database loop
<?php
$price_list = array(1, 7, 8, 10, 12, 15);
foreach($price_list AS $price){ ?>
<div>
<span>Price-Group <?php echo $price;?></span>
<form method="post" action="">
<input type="text" value="20.50" id="price<?php echo $price;?>"><br>
Option 1<input type="checkbox" value="1.50" data-id="<?php echo $price;?>"><br>
Option 2<input type="checkbox" value="2.50" data-id="<?php echo $price;?>"><br>
Option 3<input type="checkbox" value="3.50" data-id="<?php echo $price;?>"><br>
<input type="text" id="total<?php echo $price;?>" value="20.50">
</form>
</div>
<hr>
<?php } ?>
and here is my problem jquery-code (i need a dynamic total)
<script>
$('input:checkbox').change(function(){
var id = $(this).attr('data-id');
var total = parseFloat($('#price'+id).val());
$('input:checkbox:checked').each(function(){
total += isNaN(parseFloat($(this).val())) ? 0 : parseFloat($(this).val());
});
$('#total'+id).val(total);
});
</script>
OK, I spotted a problem in your Javascript. In the middle you do:
$('input:checkbox:checked').each(function () {....});
and this function is therefore applied to all checked checkboxes. You only want to apply the function to checked checkboxes in the pricegroup, to which the checkbox that changed belongs. Something like this:
$('#form' + id + ' input:checkbox:checked').each(function () {....});
Now you don't have a form id, so I had to add that. See: code example.
Note that you don't have to give each checkbox an individual data-id, because from them you can access the form id. For instance, when a checkbox changed:
var id = $(this).parent().attr('id');
would give you the whole form id.
Sorry for my noobness here I'm trying to create a project with no PHP knowledge at all.
I have a database set up with a list of users and I am able to display users based on specific information through a search. Each search query has a checkbox. What I am trying to do now is limit the amount of selected checkboxes (up to 3 selections only) and save the selected queries to a different page. Can someone point me in the right direction? I'm sure my code is all over the place and probably wrong in many ways so I apologise in advance. I appreciate it.
Search Results Page:
<?php
include 'core/init.php';
protect_page();
include 'includes/overall/overallheader.php';
?>
<h1>New Staff Request</h1>
<p>Search for potential staff using the form below</p>
<form method="POST">
<input type="TEXT" name="search" />
<input type="SUBMIT" name="submit" value="Search" />
</form>
<br/>
<p><b> Select only up to 3 results</b></p>
<form method="post" action="StaffingRequest.php">
<?php
if(isset($_POST['submit'])){
$mysqli = NEW mysqli('localhost','root','','lr');
$search = $mysqli->real_escape_string($_POST['search']);
$resultSet = $mysqli->query("SELECT * FROM users WHERE jobcat LIKE '%$search%' ");
if($resultSet->num_rows > 0) {
while($rows = $resultSet->fetch_assoc()) {
$first_name = $rows['first_name'];
$last_name = $rows['last_name'];
$education = $rows['education'];
$salary = $rows['salary'];
$jobcat = $rows['jobcat'];
echo "<br /><input type='checkbox' name='query[]' value=''> First Name: $first_name<br />Last Name: $last_name<br />Job Category: $jobcat<br />Education: $education<br />Salary: $salary<br /><br />";
}
} else {
echo "No Results";
}
}
?>
<input type="submit" name="submit" Value="Save">
</form>
<?php include 'includes/overall/overallfooter.php'; ?>
Here is the page I want the results to display on:
<?php
include 'core/init.php';
protect_page();
include 'includes/overall/overallheader.php';
?>
<h1>My Staff Requests:</h1>
<p></p>
<?php
$chkbox = $_POST['query'];
$i = 0;
While($i < sizeof($chkbox)) {
echo "CheckBox Selected Values = " . $chkbox[$i] . '</br>';
$i++;
}
?>
<?php include 'includes/overall/overallfooter.php'; ?>
You are going to want to use JS/jQuery to accomplish this.
What you are going to do is add some client side functionality that checks how many check boxes of the same class have a check in them.
I added the code for you. This is what it does.
Add the jQuery library.
Add a classname (class="checkboxes") to the input tag to be referenced later.
Detect when there has been a change to an element with the class name "checkboxes".
Count to see how many elements with the class name "checkboxes" have a checked checkbox.
If there are three checked check boxes, remove the check from the last(4th) selected check box.
Display an error to the user that only three check boxes can be selected.
Like so:
<?php
include 'core/init.php';
protect_page();
include 'includes/overall/overallheader.php';
?>
<h1>New Staff Request</h1>
<p>Search for potential staff using the form below</p>
<form method="POST" action="">
<input type="TEXT" name="search" />
<input type="SUBMIT" name="submit" value="Search" />
</form>
<br/>
<p><b> Select only up to 3 results</b></p>
<form method="POST" action="StaffingRequest.php">
<?php
if(isset($_POST['submit'])){
$mysqli = NEW mysqli('localhost','root','','lr');
$search = $mysqli->real_escape_string($_POST['search']);
$resultSet = $mysqli->query("SELECT * FROM users WHERE jobcat LIKE '%$search%' ");
if($resultSet->num_rows > 0) {
while($rows = $resultSet->fetch_assoc()) {
$first_name = $rows['first_name'];
$last_name = $rows['last_name'];
$education = $rows['education'];
$salary = $rows['salary'];
$jobcat = $rows['jobcat'];
echo '<br /><input type="checkbox" name="query[]" class="checkboxes" value=""> First Name: ' . $first_name . '<br />Last Name: ' . $last_name . '<br />Job Category: ' . $jobcat . '<br />Education: ' . $education . '<br />Salary: ' . $salary . '<br /><br />';
}
} else {
echo "No Results";
}
}
?>
<input type="submit" name="submit" Value="Save">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".checkboxes").change(function (){
if($('.checkboxes:checkbox:checked').length > 3){
$(this).prop('checked', false);
alert("You can only select 3 checkboxes.");
}
});
});
</script>
<?php include 'includes/overall/overallfooter.php'; ?>
A couple other things.
I try real hard not to have more than one form on a page. For the most part you should only need one. I would say that if you have two forms it's a special case. Your first form that you use for the search could easily be replaced with an AJAX routine.
When echoing HTML though PHP, I recommend using single quotes. This will prevent alot of problems dealing with the quotes for the elements attributes. Here is an example of how I do it:
echo '<input id="myId">Test</input>';
This will save you some headaches down the road.
Hope this works for you..
I have searched stackoverflow for the past hour and have found similar questions but attempting the solution has given me only errors - it may be as I am trying to load it on the same page.
I have tried saving the value to a cookie in JS then loading it in php, also attempted through ajax, etc.
From what I've read it can't just be done within as PHP is server side and Javascript is browser side which I get.
I currently have a .html page with a form that sends values through POST:
form.html
<form id="calc" form name="calculator" method="post" action="/calculate.php">
The solution is not with the form, I can't have hidden fields as I need to user input. I could have a radio box in the form yes and then get the value that way, but that's not my question. My question is how I can get the user's input and turn it into php on the page the form redirects to calculate.php:
calculate.php
<?php
if(isset($_POST['submit'])){
$x= $_POST['value'];
echo "your something:";
echo $x;
?>
Click button below for option one to do this, or click two for that, or three for something else...
<button id="one"> one </button>
<script>
$(document).ready(
function() {
$("#one").click(function() {
make php value 1
});
</script>
use php value from click
So the user will already have clicked submit. Then be redirected onto the .php page with their inputs, they can then choose button 1, 2 or 3 for example through javascript on calculate.php NOT the form.
My problem is I need to know on the same page - calculate.php, which button has been clicked in php. So that I can use their input and do different things with it depending on which button.
Is it possible to get a value to be set or create one from a button click on calculate.php itself with the javascript button producing a php value on the same page? Does it need to be loaded from an external.php page through ajax? Is there any other way?
PHP Option:
<?php
if(isset($_POST['submit'])){
$x= (isset($_POST['one'])) ? $_POST['one'] : '';
$y = (isset($_POST['two'])) ? $_POST['two'] : '';
$z = (isset($_POST['three'])) ? $_POST['three'] : '';
echo "your something:";
echo $x;
echo $y;
echo $z;
?>
<form name="calculator" method="post">
<INPUT TYPE="RADIO" NAME="one" VALUE="1"> ONE
<INPUT TYPE="RADIO" NAME="two" VALUE="2"> TWO
<INPUT TYPE="RADIO" NAME="three" VALUE="3"> THREE
<input type="submit" value="Submit">
</form>
JS mode:
if(isset($_POST)){
$value= (isset($_POST['value'])) ? $_POST['value'] : '';
$previous = (isset($_POST['previous'])) ? $_POST['previous'] : '';
//play with value and previous one
$x = //your algorithm;
echo "your something:";
echo $x;
?>
<form name="calculator" method="post">
<button type="button" value='1'>ONE</button>
<button type="button" value='2'>TWO</button>
<button type="button" value='3'>THREE</button>
<input name='value' type='hidden' value=''>
<input name='previous' type='hidden' value='<?= (isset($x)) ? $x : ''?>'>
</form>
<script>
$(document).ready(
function() {
$("button").click(function() {
$('input[name=value]').val($(this).val());
$('form').submit();
});
</script>
use php value from click
I have 4 groups of data that are tied to a click-bind event. I'm trying to add the value from a hidden field contained within each group to a paragraph later in the page, so that when the checkbox is clicked from one or more groups of data, the value from the hidden field displays within the paragraph area.
I'd also like to get it so that when the checkbox is unclicked, it removes the value from the paragraph, but for now I'm just trying to get the value to display when the checkbox is clicked.
Here's what I've tried:
This is the hidden field within the group of data that stores the value:
<input id="servicename_<?php echo $i;?>" name="servicename_<?php echo $i;?>"
type="hidden" value="<?php echo $service->PRODUCT;?>"></input>
This is the click-bind event:
$('input[id^=ckNow_]').each(function(){
$(this).bind('click',function(){
if($(this).prop('checked'))
{
var servicename = '#servicename_'+$(this).attr('value').split("_");
ServiceName += servicename;
$('#lblServiceName').append($(ServiceName));
EDIT to add the checkbox that is within each group:
<input type="checkbox" id="ckNow_<?php echo $i;?>" name="ckNow[<?php echo $i;?>]">
</input>
And then the paragraph text:
<p id="lblServiceName">Service Name
<input type="hidden" id="servicename" value="0"></input>
</p>
What am I doing wrong and how can I fix it? All responses are very appreciated and I'm completely open to any and all suggestions. Thank you.
Try
<p id="lblServiceName">Service Name
<span></span>
<input type="hidden" id="servicename" value="0"></input>
</p>
then
jQuery(function ($) {
var $checks = $('.group-data input[name^="ckNow["]').change(function () {
var vals = $checks.filter(':checked').map(function () {
return $(this).closest('.group-data').find('input[id^=servicename_]').val()
}).get();
$('#lblServiceName span').text(vals.join(', '))
})
})
Demo: Fiddle
This is my continuation of my previous question: Modifying $i inside a form
So now I created the code like this:
<form method="post" action="actionhere">
<div id='clone_me'>
<?php
for($i=1; $i<=1;$i++) {
?>
<span id="title">Line <?php echo $i;?></span>
<input type='checkbox' name='ck[]'/>
<input type='text' name='tx[]'/>
<?php } ?>
</div>
<input type='submit' name='submit' value='Submit'/>
<input type="button" value="Add row" class="addrow" />
</form>
<script>
$(document).ready(function() {
$('.addrow').click(function(){
var n= $("#clone_me").length;
var new_n=n+1;
var new_line= $('#clone_me div:first').clone().append();
$("span#title", new_line).text("Line bet "+new_n+" ");
new_line.appendTo('#clone_me');
alert(new_n);
});
});
</script>
So I can add new row when I click the button, but I want to change the title inside span element, but I can't make it work. The 1st cloned, yes, the title is changed correctly (from 1 to 2), but the 2nd cloned still display "2", not "3".
What is the correct way?
EDIT
I only put $i=1 for testing purposes. In actual application I need to change it to 10 (so will loop 10 rows)