<?php
error_reporting(E_ALL);
// Include the database page
include ('../inc/dbconfig.php');
$styleID = $_GET['id'];
$query = "SELECT
fields.ID,
fields.fullName,
fields.enabled
FROM
fields
INNER JOIN styles
ON styles.ID = fields.styleID
WHERE
styles.ID = '" . $styleID . "'";
$result = mysqli_query ( $dbc, $query ); // Run The Query
?>
<script>
$(document).ready(function() {
$('div.message-error').hide();
$('div.message-success').hide();
$("input.submit").click(function() {
$('div.message-error').hide();
var dataString = 'submitBioFields=True';
$('#bioConfigForm .field').each(function() {
dataString += '&'+$(this).find('input:first').attr('name')+'=';
dataString += ($(this).find('input[value|=0]').is(':checked')) ? '0' : '1';
});
$.ajax({
type: "POST",
url: "processes/bioconfig.php",
data: dataString,
success: function() {
$('div.message-error').hide();
$("div.message-success").html("<h6>Operation successful</h6><p>Bio fields saved successfully.</p>");
$("div.message-success").show().delay(10000).hide("slow", function() {
$('#content').load('mods/bioconfiguration.php');
});
}
});
return false;
});
});
</script>
<!-- Title -->
<div id="title" class="b2">
<h2>Bio Configuration</h2>
<!-- TitleActions -->
<div id="titleActions">
<!-- ListSearch -->
<div class="listSearch actionBlock">
<div class="search">
<label for="search">Recherche</label>
<input type="text" name="search" id="search" class="text" />
</div>
<div class="submit">
<button type="submit" id="search-button" class="button"><strong><img src="img/icons/search_48.png" alt="comments" class="icon "/></strong></button>
</div>
</div>
<!-- /ListSearch -->
</div>
<!-- /TitleActions -->
</div>
<!-- Title -->
<!-- Inner Content -->
<div id="innerContent">
<!-- Form -->
<form action="#" id="bioConfigForm" >
<fieldset>
<legend>Bio Config</legend>
<?php
while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
?>
<div class="field">
<label for="<?php '' . $row['ID'] . '' ?>"><?php echo '' . $row['fullName'] . ''?></label>
<input type="radio" value="0" name="<?php echo $row['ID']; ?>" class="status"
<?php if($row['enabled'] == 0)
echo ' checked="checked"';
?>
/>Enabled
<input type="radio" value="1" name="<?php echo $row['ID']; ?>" class="status"
<?php if($row['enabled'] == 1)
echo ' checked="checked"';
?>
/>Disabled
</div>
<?php
}
?>
<input type="submit" class="submit" name="submitBioFields" id="SubmitBioFields" title="Submit Bio Fields" value="Submit Bio Fields"/>
</fieldset>
</form>
<!-- /Form -->
<!-- Messages -->
<div class="message message-error">
<h6>Required field missing</h6>
<p>Please fill in all required fields. </p>
</div>
<div class="message message-success">
<h6>Operation succesful</h6>
<p>Bio configuraton was eddited to the database.</p>
</div>
<!-- /Messages -->
Bio Fields
Page 1 of 17 Replies - 1425 Views - Last Post: 06 March 2011 - 04:55 PM
#1
Bio Fields
Posted 03 March 2011 - 09:29 AM
I believe I've developed my dataString incorrectly. What I would like to have in my dataString is the ID of the bioField(ID from database) and either a 0(enabled)/1(disabled) from the form.
Replies To: Bio Fields
#2
Re: Bio Fields
Posted 03 March 2011 - 02:36 PM
Hi,
Why don't you do something like :
Put a hidden input on the form with the bioFieldid and that should give you everything you need.
Why don't you do something like :
$("#bioConfigForm").submit(function(){
var data = $(this).serialize() + "&submitBioFields=True";
});
Put a hidden input on the form with the bioFieldid and that should give you everything you need.
This post has been edited by Jstall: 03 March 2011 - 02:37 PM
#3
Re: Bio Fields
Posted 03 March 2011 - 03:00 PM
That wouldn't make sense because there's like 15 bio fields.
Jstall, on 03 March 2011 - 02:36 PM, said:
Hi,
Why don't you do something like :
Put a hidden input on the form with the bioFieldid and that should give you everything you need.
Why don't you do something like :
$("#bioConfigForm").submit(function(){
var data = $(this).serialize() + "&submitBioFields=True";
});
Put a hidden input on the form with the bioFieldid and that should give you everything you need.
#4
Re: Bio Fields
Posted 03 March 2011 - 08:23 PM
Right and they are all being output on your form as radio buttons being set to off or on correct? .serialize() will create a query string based on the values of your form so it will work for all your radio buttons, you would get something like :
3=1&5=1&6=0&7=1
Where each of the keys in that query string are id's of biofields. Thast what you are trying to do correct?
Notice that I changed your handler from
to
as it's better to attach the handler to the form's submit event since that's what you are really waiting for, plus you can refer to the form as $(this) within your callback.
Hope this helps.
3=1&5=1&6=0&7=1
Where each of the keys in that query string are id's of biofields. Thast what you are trying to do correct?
Notice that I changed your handler from
$("input.submit").click
to
$("#bioConfigForm").submit
as it's better to attach the handler to the form's submit event since that's what you are really waiting for, plus you can refer to the form as $(this) within your callback.
Hope this helps.
#5
Re: Bio Fields
Posted 04 March 2011 - 08:43 AM
Okay so my issue is now with my php processsing which I have
<?php
// Include the database page
require ('../inc/dbconfig.php');
if (isset($_POST['submitBioFields'])) {
$biofieldID = $_POST['ID'];
$enabled = (int)$_POST['value'];
$query = "UPDATE `fields` SET `enabled` = '".$value."' WHERE `ID` = '".$biofieldID."'";
mysqli_query($dbc,$query);
$result = "good";
} else {
$result = "bad";
}
//Output the result
echo $result;
?>
Jstall, on 03 March 2011 - 08:23 PM, said:
Right and they are all being output on your form as radio buttons being set to off or on correct? .serialize() will create a query string based on the values of your form so it will work for all your radio buttons, you would get something like :
3=1&5=1&6=0&7=1
Where each of the keys in that query string are id's of biofields. Thast what you are trying to do correct?
Notice that I changed your handler from
to
as it's better to attach the handler to the form's submit event since that's what you are really waiting for, plus you can refer to the form as $(this) within your callback.
Hope this helps.
3=1&5=1&6=0&7=1
Where each of the keys in that query string are id's of biofields. Thast what you are trying to do correct?
Notice that I changed your handler from
$("input.submit").click
to
$("#bioConfigForm").submit
as it's better to attach the handler to the form's submit event since that's what you are really waiting for, plus you can refer to the form as $(this) within your callback.
Hope this helps.
#6
Re: Bio Fields
Posted 05 March 2011 - 07:50 PM
Hi,
Well the first thing is you are checking to see if the submit button was posted before you try your query. This will not work as .serialize() only serializes "successful" controls, of which the submit button is not. You can compensate for that by adding a hidden input or something and doing a check for that being posted.
Next you are checking for $_POST['ID'] and $_POST['value'] but they are not set anywhere in your form. The "name" attribute of an element is whats used to access an element in the $_POST array.
Your next problem is the way you have your form set up, you have the biofieldId as the name of your radio button and if it is enabled as the value. You are going to have to iterate through the post array as you can't know what id's are going to be submitted(or maybe you do but having to write out code to check each one individually would suck).
Because of the way your form is set up your $_POST array will look like this
So using a foreach you can grab those values
You might wanna do a check on the $id to ensure it's numeric, that way you can be reasonably certain that it's a biofieldid radio control and not some other element(the hidden input you may add, for instance). You should also do some sort of data scrubbing on the posted values before you use them in a query.
Hope this helps, good luck.
Well the first thing is you are checking to see if the submit button was posted before you try your query. This will not work as .serialize() only serializes "successful" controls, of which the submit button is not. You can compensate for that by adding a hidden input or something and doing a check for that being posted.
Next you are checking for $_POST['ID'] and $_POST['value'] but they are not set anywhere in your form. The "name" attribute of an element is whats used to access an element in the $_POST array.
Your next problem is the way you have your form set up, you have the biofieldId as the name of your radio button and if it is enabled as the value. You are going to have to iterate through the post array as you can't know what id's are going to be submitted(or maybe you do but having to write out code to check each one individually would suck).
Because of the way your form is set up your $_POST array will look like this
$_POST['bio_id']=>value
So using a foreach you can grab those values
foreach($_POST as $id=>$value)
{
//write your query here
}
You might wanna do a check on the $id to ensure it's numeric, that way you can be reasonably certain that it's a biofieldid radio control and not some other element(the hidden input you may add, for instance). You should also do some sort of data scrubbing on the posted values before you use them in a query.
Hope this helps, good luck.
#7
Re: Bio Fields
Posted 06 March 2011 - 10:33 AM
I echoed the query once and it came up as UPDATE `fields` SET `enabled` = 'True' WHERE `ID` = ''good Not sure why.
<?php
// Include the database page
include ('../inc/dbconfig.php');
$styleID = $_GET['id'];
$query = "SELECT
fields.ID,
fields.fullName,
fields.enabled
FROM
fields
INNER JOIN styles
ON styles.ID = fields.styleID
WHERE
styles.ID = '" . $styleID . "'";
$result = mysqli_query ( $dbc, $query ); // Run The Query
?>
<script>
$(document).ready(function() {
$('div.message-error').hide();
$('div.message-success').hide();
$("#bioConfigForm").submit(function() {
$('div.message-error').hide();
var data = $(this).serialize() + "&submitBioFields=True";
$.ajax({
type: "POST",
url: "processes/bioconfig.php",
data: data,
success: function() {
$('div.message-error').hide();
$("div.message-success").html("<h6>Operation successful</h6><p>Bio fields saved successfully.</p>");
$("div.message-success").show().delay(10000).hide("slow", function() {
$('#content').load('mods/bioconfiguration.php');
});
}
});
return false;
});
});
</script>
<!-- Title -->
<div id="title" class="b2">
<h2>Bio Configuration</h2>
<!-- TitleActions -->
<div id="titleActions">
<!-- ListSearch -->
<div class="listSearch actionBlock">
<div class="search">
<label for="search">Recherche</label>
<input type="text" name="search" id="search" class="text" />
</div>
<div class="submit">
<button type="submit" id="search-button" class="button"><strong><img src="img/icons/search_48.png" alt="comments" class="icon "/></strong></button>
</div>
</div>
<!-- /ListSearch -->
</div>
<!-- /TitleActions -->
</div>
<!-- Title -->
<!-- Inner Content -->
<div id="innerContent">
<!-- Form -->
<form action="#" id="bioConfigForm" >
<fieldset>
<legend>Bio Config</legend>
<?php
while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
?>
<div class="field">
<label for="<?php '' . $row['ID'] . '' ?>"><?php echo '' . $row['fullName'] . ''?></label>
<input type="radio" value="0" name="<?php echo $row['ID']; ?>" class="status"
<?php if($row['enabled'] == 0)
echo ' checked="checked"';
?>
/>Enabled
<input type="radio" value="1" name="<?php echo $row['ID']; ?>" class="status"
<?php if($row['enabled'] == 1)
echo ' checked="checked"';
?>
/>Disabled
</div>
<?php
}
?>
<input type="submit" class="submit" name="submitBioFields" id="SubmitBioFields" title="Submit Bio Fields" value="Submit Bio Fields"/>
</fieldset>
</form>
<!-- /Form -->
<?php
// Include the database page
require ('../inc/dbconfig.php');
if (isset($_POST['submitBioFields'])) {
foreach($_POST as $bioFieldID => $value) {
$query = "UPDATE `fields` SET `enabled` = '".$value."' WHERE `ID` = '".$biofieldID."'";
}
mysqli_query($dbc,$query);
$result = "good";
} else {
$result = "bad";
}
//Output the result
echo $result;
?>
#8
Re: Bio Fields
Posted 06 March 2011 - 04:55 PM
Hey,
If I had to guess the 'True' is coming from the
appended to your post the "good" is what happens at the end of your script when you echo $result.
As I mentioned you would have to do some sort of verification on the values before using them in your query.
I'm still pretty sure this can be accomplished with a foreach over the $_POST array. To get an idea of what is being posted either use a webdev browser tool like Firebug or do
To see whats in there.
If I had to guess the 'True' is coming from the
&submitBioFields=True
appended to your post the "good" is what happens at the end of your script when you echo $result.
As I mentioned you would have to do some sort of verification on the values before using them in your query.
I'm still pretty sure this can be accomplished with a foreach over the $_POST array. To get an idea of what is being posted either use a webdev browser tool like Firebug or do
print_r($_POST);
To see whats in there.
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|