The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Checkbox Check Uncheck Problem
Discuss Checkbox Check Uncheck Problem in the PHP Development forum on Dev Shed. Checkbox Check Uncheck Problem PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 19th, 2012, 01:08 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 27
Time spent in forums: 3 h 38 m 44 sec
Reputation Power: 0
|
|
|
Checkbox Check Uncheck Problem
Hi
I have a bit of a dilemma.
I'm using onclick and onmouseup and onchange to submit values in a form. I usually let the user first choose all the values then i use a radio button to submit the changes, but i'm try to do it more on the fly so that every time they change a value the form submits and updates. Which i'm already doing. the but question is below the code
here is the php
PHP Code:
<?php
if (isset($_POST['ordernow'])) {
$sql="SELECT `quantity` from `mytable` where `filenr` = '$filenr'";
$result=mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
$quantity = $row['quantity'];
if (empty($quantity)){
$quantity = "250";
$exVat = 262.28;
$Vat = 262.28*0.14;
$Total = 262.28*1.14; // R299 total
}
foreach($_POST['ordernow'] as $ordernow){ $filenr = $ordernow . ' ';}
$pieces = explode("_", $filenr);
$filenr = $pieces[0]; // piece1
$ordernow = $pieces[1]; // piece2
$sql="UPDATE `mytable` set `ordernow` = '$ordernow' where `filenr` = '$filenr'";
//die ($sql);
mysql_query($sql, $link);
header ('Location: page.php'); die ('3'); exit (0);
}
if (isset($_POST['quantity'])) {
$quantity = safe($_POST['quantity']);
$pieces = explode("_", $quantity);
$filenr = $pieces[0]; // piece1
$quantity = $pieces[1]; // piece2
if ($quantity == "250"){
$exVat = 262.28;
$Vat = 262.28*0.14;
$Total = 262.28*1.14; // R299 total
}
if ($quantity == "500"){
$exVat = 350;
$Vat = 350*0.14;
$Total = 350*1.14; //R399 total
}
if ($quantity == "1000"){
$exVat = 437.72;
$Vat = 437.72*0.14;
$Total = 437.72*1.14; //R399 total
}
$sql="UPDATE `mytable` set `quantity` = '$quantity' ,`exVat` = '$exVat' ,`Vat` = '$Vat', `Total` = '$Total' where `filenr` = '$filenr'";
//die ($sql);
mysql_query($sql, $link);
header ('Location: page.php'); die ('3'); exit (0);
}
?>
and the html with php to make the top php work
PHP Code:
<form action="" method="post" name="form">
<table width="105" border="0" cellspacing="0" cellpadding="0">
<tr align="center" class="history">
<th width="71">Quantity</th>
<th width="34">Select</th>
</tr>
<?php
$sql="select * from `mytable` where `username` = '".$username."'";
$result=mysql_query($sql, $link);
$row = mysql_fetch_assoc($result)
$ordernow = $row['ordernow'];
$quantity = $row['quantity'];
?>
<tr>
<th>
<select name="quantity" style="WIDTH: 60px" onchange="this.form.submit();">
<option value="<?php echo $filenr."_250" ?>" <?php if ($quantity == "250"){ echo " selected='selected'";}else{}?>>250</option>
<option value="<?php echo $filenr."_500" ?>" <?php if ($quantity == "500"){ echo " selected='selected'";}else{}?>>500</option>
<option value="<?php echo $filenr."_1000" ?>" <?php if ($quantity == "1000"){ echo " selected='selected'";}else{}?>>1000</option>
</select>
</th>
<th>
<?php if ($ordernow == "1"){ echo "<input name=\"ordernow[]\" id=\"ordernow\" type=\"checkbox\" value=\"".$filenr."_0\" checked=\"checked\" onmouseup=\"this.form.submit();\" />";}else { echo "<input name=\"ordernow[]\" id=\"ordernow\" type=\"checkbox\" value=\"".$filenr."_1\" onchange=\"this.form.submit();\" />";} ?>
</th>
</tr>
</table>
</form >
So what it does is when the user checks the record it gets checked.
And uncheck unchecks the record,
But the problem comes in when the quantity gets changed, Then it obviously posts the checkbox also and unchecks it and quantity stays unchange. (not good)
Only the second time round when the checkbox is empty it actualy changes the quantity.
This is undesired.
I would like to have each input method only be effected when that specific dropdown or checkbox was selected.
so that when quantity gets updated it does not affect the state of the checkbox.
Does anyone have a workaround for this please
|

November 20th, 2012, 10:10 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
|
|
|
I'm a bit lost with how you make it sound that every time the form is submitted, a checkbox alters to its opposite on its own. If I'm understanding the overall goal right, your intent is to do something similar to a shopping cart, where if you update quantity, it will update price to with your new values.
If you wish to do this in PHP the way it sounds, I would pretty much have 2 types of submits. I forgot how to do this, n am on my way out the work, so sorry for the lack of info, but 1 submit simply submits current info back to the page so it can update its items. Final submit is the forms true submit to toss the info into a database, or your intent.
|

November 20th, 2012, 11:53 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 27
Time spent in forums: 3 h 38 m 44 sec
Reputation Power: 0
|
|
|
My own shopping cart yes
I see yes
Like Submit the form but to Session variables.
I prefer the hard code way.
You see, the way the check box was updating and changing itself is cause its part of the form where the quantity is being updated.
I do it with onchange events for each drop down so every change the user makes gets inserted and updated into the database database.
I'm not getting the checkbox method right so ive changed the checkbox also into a dropdown.
The Session submit will be way to elaborate i think.
My code is working now but would like to figure the checkbox out.
|

November 20th, 2012, 01:14 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
|
|
|
Ah, I think I was a lil off on your issue. When you start, the checkbox may already be checked since it was defined as checked. You would like this to hold value and be able to change your quantity? If that is the case, I would just toss in a check to see if current quantity value in database matches that submitted by the form.
|

November 20th, 2012, 01:21 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 27
Time spent in forums: 3 h 38 m 44 sec
Reputation Power: 0
|
|
|
Yes I've tried that with the checkbox, didnt work.
It is exactly what i'm already doing with the dropdowns.
If posted value = db value then skip sql insert.
but the checkbox did not want to play ball so i tossed it for the dropdown which says yes or know.
But boy the checkbox looks so much better.
You can use my code to try it and see for yourself.
It's checked at first. then when i post the form it unchecked because of reversed checkbox values.
You see when its checked value is 0
When unchecked value is 1
This is because when you uncheck the box it has to post the opposite value all the time then you just go into the loop all the time
|

November 20th, 2012, 01:54 PM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
Quote: | Originally Posted by jpmul Does anyone have a workaround for this please |
Yes: Don't update like that; use a single submit button ("Update totals") and have it work out the changes then instead.
Besides, f the user selects the wrong value from the drop-down and then it updates the cart, they then have to re-select / submit the change. Not good or user-friendly.
__________________
The number for UK Emergencies is changing, the new number is 0118 999 881 999 119 7253
"For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
- George Orwell, 1984
|

November 21st, 2012, 02:59 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 27
Time spent in forums: 3 h 38 m 44 sec
Reputation Power: 0
|
|
|
The thing is i think the user experience will in this case will better rather than being diminished. Most people have broadband now a days.
There will mostly be more than one item and each item have about 6 option which can affect that items price outcome.
Good advice noted though
|

November 21st, 2012, 07:49 AM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
Quote: | Originally Posted by jpmul The thing is i think the user experience will in this case will better rather than being diminished. Most people have broadband now a days.
There will mostly be more than one item and each item have about 6 option which can affect that items price outcome.
Good advice noted though |
Well, although I have 60mb broadband it'd annoy the hell out of me if the whole page reloaded when I tried to make a change.
It'd have to be done via Ajax or a single 'update cart' option for me. Multiple selects and checkboxes are really quite easy, search this forum where I myself have made several posts about the subject - there'll be links to example pages which will help too.
Of course, it's up to you, you have my opinion.
Regards!
|

November 21st, 2012, 08:35 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
|
|
|
Yep. That's the way I'd go. If you want instant updates after tiny changes, just write it to be done client-side, and don't bother truely submitting anything til it's in it's final state and truely ready to be submitted.
|

November 21st, 2012, 04:07 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 27
Time spent in forums: 3 h 38 m 44 sec
Reputation Power: 0
|
|
|
Cool Thanks
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|