Heya folks,
this code below is currently called to edit an existing record, using a url with;
option=com_chronocontact&chronoformname=edit_car&car_id=X
Though its nearly there a couple of things are not quite playing nicely, so any comments appreciated!
1) CSS
I think I have everything wrapped correctly in one <div class="form_item">, but I don't think all of the formatting is coming through, things like label positions are off compared to other forms that were started with the form builder. This has been hand coded, but I have attempted to use all of the same classes. Any thoughts ?
2) A Better method of 'sanity checking' the logged in user
I check that the logged in user, is the one that uploaded the record set in question to make sure nobody formulate there own url to edit someone elses record. i.e. 'WHERE `cf_id`= "." ".$car_id." AND `cf_user_id` = "." ".$userid." "; '
This does work, but is clunky - no x parameter in the url give an sql error, the query is false gives a blank page. Normally I woudl make a series of if/else statements to decide if to load the form or not, and if not, error. However how can distinguish between the query failing (no x parameter) and cf_id for the car_id in question not equating to the $userid ?
Is there a generic 'iferror' I can use as the first ifelse condition ?
3) Defaulting the drop down list to the value in the database
Consider;
This is always displaying 'For Sale' even if the value in the field is 'sold' 'archived' or 'null'. In never infact displays 'forsale' which although not desirable, is what I think shoudl be happening.
Its easy for a text box, However how should I be setting the default value of the dropdown, based on the value in $status ?
thanks as always,
/Miz
this code below is currently called to edit an existing record, using a url with;
option=com_chronocontact&chronoformname=edit_car&car_id=X
<?php
//Get the currently logged in Joomla User, set to $userid
$user =& JFactory::getUser();
$userid = $user->get('id');
//Set the value of car_id to a $var in the url used to load this page
$car_id = JRequest::getString('car_id', '', 'get');
//Select anything where the car_id = and url AND the user_id is the logged in user
$query = "SELECT * FROM `jos_chronoforms_cars` WHERE `cf_id`= "." ".$car_id." AND `cf_user_id` = "." ".$userid." ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$reg = $row[vrm];
$desc = $row[description];
$cost = $row[cost];
$status = $row[status];
?>
<!-- Start the form below -->
<div class="form_item">
<div class="form_element cf_heading">
<h1 class="cf_text">Test - Simple Edit existing details form</h1><br />
</div>
<br />
<div class="cfclear"> </div>
<div class="form_element cf_text">
<span class="cf_text">
This is a simple edit test. It will only load if the logged in user id, matches in the record set for the car_id in the usr above. We need some form of error checking, as although this works, it either loads or throws an sql error!<br /><br />
</span>
</div>
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">Registration</label>
<input class="cf_inputbox required" maxlength="150" size="30" title="" id="vrm" name="vrm" type="text" disabled="disabled" value="<?php echo $reg; ?>" />
</div>
<div class="cfclear"> </div>
<div class="form_element cf_textarea">
<label for="description" class="cf_label" style="width: 150px;">Description</label>
<textarea class="cf_inputbox required" rows="5" id="description" tabindex="1" title="" cols="30" name="description">
<?php echo $desc; ?>
</textarea>
</div>
<div class="cfclear"> </div>
<div class="form_element cf_textbox">
<label for="cost" class="cf_label" style="width: 150px;">Cost</label>
<input class="cf_inputbox validate-number" maxlength="150" size="30" title="" id="cost" tabindex="2" name="cost" type="text" value="<?php echo $cost;?>" />
</div>
<div class="cfclear"> </div>
<div class="form_element cf_dropdown">
<label for="status" class="cf_label" style="width: 150px;">Status</label>
<select class="cf_inputbox validate-selection" id="status" size="1" tabindex="3" name="status">
<option value="<?php echo $status; ?>" selected="selected><?php echo $status; ?></option>
<option value="forsale">For Sale</option>
<option value="sold">Sold</option>
<option value="archive">Delete</option>
</select>
</div>
<div class="cfclear"> </div>
<div class="form_element cf_button">
<input value="Submit" name="submit" id="submit" tabindex="4" type="submit" />
</div>
</div>
Though its nearly there a couple of things are not quite playing nicely, so any comments appreciated!
1) CSS
I think I have everything wrapped correctly in one <div class="form_item">, but I don't think all of the formatting is coming through, things like label positions are off compared to other forms that were started with the form builder. This has been hand coded, but I have attempted to use all of the same classes. Any thoughts ?
2) A Better method of 'sanity checking' the logged in user
I check that the logged in user, is the one that uploaded the record set in question to make sure nobody formulate there own url to edit someone elses record. i.e. 'WHERE `cf_id`= "." ".$car_id." AND `cf_user_id` = "." ".$userid." "; '
This does work, but is clunky - no x parameter in the url give an sql error, the query is false gives a blank page. Normally I woudl make a series of if/else statements to decide if to load the form or not, and if not, error. However how can distinguish between the query failing (no x parameter) and cf_id for the car_id in question not equating to the $userid ?
Is there a generic 'iferror' I can use as the first ifelse condition ?
3) Defaulting the drop down list to the value in the database
Consider;
<div class="form_element cf_dropdown">
<label for="status" class="cf_label" style="width: 150px;">Status</label>
<select class="cf_inputbox validate-selection" id="status" size="1" tabindex="3" name="status">
<option value="<?php echo $status; ?>" selected="selected><?php echo $status; ?></option>
<option value="forsale">For Sale</option>
<option value="sold">Sold</option>
<option value="archived">Delete</option>
</select>
</div>
This is always displaying 'For Sale' even if the value in the field is 'sold' 'archived' or 'null'. In never infact displays 'forsale' which although not desirable, is what I think shoudl be happening.
Its easy for a text box, However how should I be setting the default value of the dropdown, based on the value in $status ?
thanks as always,
/Miz
Hi Mizpah,
Here's an updated version. I made quite a few changes:[list]Switched to Joomla db code Added a check that the key data is there Assuming your ids are integers I switched the validation a little I re-wrote the option array to set the selected value correctly. . . . [/list]
Bob
Here's an updated version. I made quite a few changes:[list]
<?php
//Get the currently logged in Joomla User, set to $userid
$user =& JFactory::getUser();
//Set the value of car_id to a $var in the url used to load this page
$car_id = JRequest::getInt('car_id', '', 'get');
//Select anything where the car_id = and url AND the user_id is the logged in user
if ( !$user || !$car_id ) {
global $mainframe;
$mainframe->enqueuemessage('Missing vital data');
return;
}
$db =& JFactory::getDBO();
$query = "
SELECT *
FROM `#__chronoforms_cars`
WHERE `cf_id` = $car_id
AND `cf_user_id` = ".$user->id." ;
";
$db->setQuery($query);
$row =& $db->loadArray();
?>
<!-- Start the form below -->
<div class="form_item">
<div class="form_element cf_heading">
<h1 class="cf_text">Test - Simple Edit existing details form</h1><br />
</div>
<br />
<div class="cfclear"> </div>
<div class="form_element cf_text">
<span class="cf_text">
This is a simple edit test. It will only load if the logged in user id, matches in the record set for the car_id in the usr above. We need some form of error checking, as although this works, it either loads or throws an sql error!<br /><br />
</span>
</div>
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">Registration</label>
<input class="cf_inputbox required" maxlength="150" size="30" title="" id="vrm" name="vrm" type="text" disabled="disabled" value="<?php echo $row->vrm; ?>" />
</div>
<div class="cfclear"> </div>
<div class="form_element cf_textarea">
<label for="description" class="cf_label" style="width: 150px;">Description</label>
<textarea class="cf_inputbox required" rows="5" id="description" tabindex="1" title="" cols="30" name="description" ><?php echo $row->description; ?></textarea>
</div>
<div class="cfclear"> </div>
<div class="form_element cf_textbox">
<label for="cost" class="cf_label" style="width: 150px;">Cost</label>
<input class="cf_inputbox validate-number" maxlength="150" size="30" title="" id="cost" tabindex="2" name="cost" type="text" value="<?php echo $row->cost;?>" />
</div>
<div class="cfclear"> </div>
<div class="form_element cf_dropdown">
<label for="status" class="cf_label" style="width: 150px;">Status</label>
<select class="cf_inputbox validate-selection" id="status" size="1" tabindex="3" name="status">
<option value='' >--?--</option>
<?php
$options = array('forsale' => 'For Sale', 'sold' => 'Sold', 'archive' => 'Delete');
foreach ( $options as $k => $v ) {
if ( $row_.status == $k ) {
$selected = "selected='selected'";
} else {
$selected = "";
}
echo "<option value='$k' $selected >$v</option>";
}
?>
</select>
</div>
<div class="cfclear"> </div>
<div class="form_element cf_button">
<input value="Submit" name="submit" id="submit" tabindex="4" type="submit" />
</div>
</div>
My guess is that you haven't got the CSS set to load in the Form General tab as the layout looks OK to me.Bob
Heya Bob,
Firstly apologies for the delay, I have just been dragged away again for week - secondly - wow! thanks greatly for the improved version, I appreciate the extensive time and effort 😀
/Miz
Firstly apologies for the delay, I have just been dragged away again for week - secondly - wow! thanks greatly for the improved version, I appreciate the extensive time and effort 😀
/Miz
Heya Bob,
Doubtless I will find somethign I have done sson enough - am just starting troubleshooting, however providing the correct values in the url gives:
Fatal error: Call to undefined method JDatabaseMySQL::loadArray()
Doubtless I will find somethign I have done sson enough - am just starting troubleshooting, however providing the correct values in the url gives:
Fatal error: Call to undefined method JDatabaseMySQL::loadArray()
$db =& JFactory::getDBO();
$query = "
SELECT *
FROM `jos_chronoforms_cars`
WHERE `cf_id` = $car_id
AND `cf_user_id` = ".$user->id." ;
";
$db->setQuery($query);
$row =& $db->loadArray();
ok first issue fixed (I think!)
I have changed:
to
As I am only expecting a single record here (as we include a unique identifier &car_id), I have gone for 'loadObject' as we can return results with $row->columnname. I think that loadAssoc, loadObject and loadRow would all have worked, I am sure theres some logic for the correct one to use in each case somewhere!?
Info has a come from here http://docs.joomla.org/How_to_use_the_database_classes_in_your_script (a usefull page, must use Joomla Database Clas in future!)
I have changed:
$db->setQuery($query);
$row =& $db->loadArray();
to
$db->setQuery($query);
$result =& $db->loadObject();
As I am only expecting a single record here (as we include a unique identifier &car_id), I have gone for 'loadObject' as we can return results with $row->columnname. I think that loadAssoc, loadObject and loadRow would all have worked, I am sure theres some logic for the correct one to use in each case somewhere!?
Info has a come from here http://docs.joomla.org/How_to_use_the_database_classes_in_your_script (a usefull page, must use Joomla Database Clas in future!)
ok,
Nearly there🙂
Using Joomla DB Code = Working
Checking for missing vars = Working
CSS = Working (yep! it was css files turned off!)
Defaulting Drop down list = not quite!
Here is the code as it stands now:
Oh and thanks for the enqueuemessage - I didnt know that one either!
Nearly there🙂
Using Joomla DB Code = Working
Checking for missing vars = Working
CSS = Working (yep! it was css files turned off!)
Defaulting Drop down list = not quite!
Here is the code as it stands now:
<?php
//Get the currently logged in Joomla User, set to $userid
$user =& JFactory::getUser();
//Set the value of car_id to a $var in the url used to load this page
$car_id = JRequest::getInt('car_id', '', 'get');
//Select anything where the car_id = and url AND the user_id is the logged in user
if ( !$user || !$car_id ) {
global $mainframe;
$mainframe->enqueuemessage('Missing vital data from the URL',''error');
return;
}
$db =& JFactory::getDBO();
$query = "
SELECT *
FROM `jos_chronoforms_cars`
WHERE `cf_id` = $car_id
AND `cf_user_id` = ".$user->id." ;
";
$db->setQuery($query);
$result =& $db->loadObject();
?>
<!-- debug box will remove once done -->
<span class="alert">
<?php print_r($result); ?>
</span>
<span class="information">
<?php echo $result->vrm; ?>
<?php echo $result->cost; ?>
<?php echo $result->status; ?>
</span>
<!-- Start the form below -->
<div class="form_item">
<div class="form_element cf_heading">
<h1 class="cf_text">Test - Simple Edit existing details form</h1><br />
</div>
<br />
<div class="cfclear"> </div>
<div class="form_element cf_text">
<span class="cf_text">
This is a simple edit test. It will only load if the logged in user id, matches in the record set for the car_id in the usr above. We need some form of error checking, as although this works, it either loads or throws an sql error!<br /><br />
</span>
</div>
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">Registration</label>
<input class="cf_inputbox required" maxlength="150" size="30" title="" id="vrm" name="vrm" type="text" disabled="disabled" value="<?php echo $result->vrm; ?>" />
</div>
<div class="cfclear"> </div>
<div class="form_element cf_textarea">
<label for="description" class="cf_label" style="width: 150px;">Description</label>
<textarea class="cf_inputbox required" rows="5" id="description" tabindex="1" title="" cols="30" name="description" ><?php echo $result->description; ?></textarea>
</div>
<div class="cfclear"> </div>
<div class="form_element cf_textbox">
<label for="cost" class="cf_label" style="width: 150px;">Cost</label>
<input class="cf_inputbox validate-number" maxlength="150" size="30" title="" id="cost" tabindex="2" name="cost" type="text" value="<?php echo $result->cost;?>" />
</div>
<div class="cfclear"> </div>
<div class="form_element cf_dropdown">
<label for="status" class="cf_label" style="width: 150px;">Status</label>
<select class="cf_inputbox validate-selection" id="status" size="1" tabindex="3" name="status">
<option value='' >--?--</option>
<?php
$options = array('forsale' => 'For Sale', 'sold' => 'Sold', 'archive' => 'Delete');
foreach ( $options as $k => $v ) {
if ( $result_.status == $k ) {
$selected = "selected='selected'";
} else {
$selected = "";
}
echo "<option value='$k' $selected >$v</option>";
}
?>
</select>
</div>
<div class="cfclear"> </div>
<div class="form_element cf_button">
<input value="Submit" name="submit" id="submit" tabindex="4" type="submit" />
</div>
</div>
Oh and thanks for the enqueuemessage - I didnt know that one either!
Heya Bob,
All of the form functions now work - however
It sees now I am testing further, that I am indeed populating the form correctly using the values pulled form the DB - however when I submit its simply creating a new record - rather than editing the existing one! 😶
On the new record, all fields not explicily populatedin the form remain null.
I had assumed that the default would be to return data to the record we selected - but I am guessing I have neer actually told it to do this anywhere! Off to search the forums🙂
All of the form functions now work - however
It sees now I am testing further, that I am indeed populating the form correctly using the values pulled form the DB - however when I submit its simply creating a new record - rather than editing the existing one! 😶
On the new record, all fields not explicily populatedin the form remain null.
I had assumed that the default would be to return data to the record we selected - but I am guessing I have neer actually told it to do this anywhere! Off to search the forums🙂
Hi Mizpah,
Looks good, there's a typo in this line
Sorry about loadArray() :-( I wrote most of that page in the Joomla docs because I can never remember which is which.
Bob
Looks good, there's a typo in this line
if ( $result_.status == $k ) {
should beif ( $result->status == $k ) {
That's one I often make with the shift key.Sorry about loadArray() :-( I wrote most of that page in the Joomla docs because I can never remember which is which.
Bob
Thankd Bob,
Yup, I caught the _.🙂 - and I actually managed to learn somthing in the process with jdocs!
Now the form works in terms of what is displayed - I just need it to now edit the selected record, not submit a new one!!
Yup, I caught the _.🙂 - and I actually managed to learn somthing in the process with jdocs!
Now the form works in terms of what is displayed - I just need it to now edit the selected record, not submit a new one!!
Ok,
Form searching around the forums, it seems that to edit the record, rather than update it, I need to pass a value that is a unique key in the db for the recordset.
This value, when hardcoded into the form as a hidden input works perfectly, and the record is edited correctly. As per
However when I use:
it seems as if cf_id is not being passed correctly. Enabling debug shows me:
$_POST Array: Array ( [cf_id] => [description] => Another new description [cost] => 100 [status] => forsale [submit] => Submit [386407bb500d4dd4f760f333328be7b3] => 1 [1cf1] => adb184613c84c029a7d6da0b60d2035e [chronoformname] => edit_car )
Yet:
Placed within the form works as expected!!
Is there an additional piece of voodoo for passing an INT value called from loadObject into a hidden field?
Form searching around the forums, it seems that to edit the record, rather than update it, I need to pass a value that is a unique key in the db for the recordset.
This value, when hardcoded into the form as a hidden input works perfectly, and the record is edited correctly. As per
<input type="hidden" name="cf_id" value="6" />
However when I use:
<input type="hidden" name="cf_id" value="<?php echo $result->cf_id; ?>" />
it seems as if cf_id is not being passed correctly. Enabling debug shows me:
$_POST Array: Array ( [cf_id] => [description] => Another new description [cost] => 100 [status] => forsale [submit] => Submit [386407bb500d4dd4f760f333328be7b3] => 1 [1cf1] => adb184613c84c029a7d6da0b60d2035e [chronoformname] => edit_car )
Yet:
<?php echo $result->cf_id; ?>
Placed within the form works as expected!!
Is there an additional piece of voodoo for passing an INT value called from loadObject into a hidden field?
well we have a solution that works - only problem is we dont know why!
adding:
gets everything working - I would love to know why <input type="hidden" name="cf_id" value="<?php echo $result->cf_id; ?> />" doesnt work however!
adding:
<?php $cfid = $result->cf_id; ?>
<input type="hidden" name="cf_id" value="<?php echo $cfid;?>" />
gets everything working - I would love to know why <input type="hidden" name="cf_id" value="<?php echo $result->cf_id; ?> />" doesnt work however!
This topic is locked and no more replies can be posted.