Reputation: 167
I have a condition in javascript that says if the text box is blank it would alert and return false, but it doesn't work instead it submits here is my view
frm_otherequips2.php
<form action="edit_delete_others2" method="POST" id='frm_otherequips2' name="frm_otherequips2">
<fieldset id="edit_other">
<legend>Inventory Details</legend>
<table>
<tr>
<?php $otherequips = $other->Equipment;
$otherdesc = $other->ItemDescription;
$otherID = $other->ID;
$otherserialnum = $other->SerialNumber;
?>
<td>Equipment</td>
<td><input type="text" id="txt_editotherequip" name="txt_editotherequip" value="<?php echo $otherequips ?>"/></td>
<td>Item Description</td>
<td><input type="text" id="txt_editotherdesc" name="txt_editotherdesc" value="<?php echo $otherdesc ?>"/></td>
<td style="visibility: hidden;"><input type="text" id="txt_editotherID" name="txt_editotherID" value="<?php echo $otherID ?>"/></td>
</tr>
<tr>
<td>Serial Number</td>
<td><input type="text" id="txt_editotherserialnum" name="txt_editotherserialnum" value="<?php echo $otherserialnum ?>"/></td>
</tr>
<tr>
<td><button class="btn_editother" id="btn_editother" name="btn_editother" type="submit" value="Edit" onclick="validate_edit_otherequip();">Edit</button></td>
</tr>
</table>
</fieldset>
</form>
and here is my javascript:
<script>
function validate_edit_otherequip(){
if(document.getElementById('txt_editotherequip').value === ""){
alert('Please Input Equipment!');
return false;
}
else if(document.getElementById('txt_editotherdesc').value === ""){
alert('Please Input Item Description');
return false;
}
else if(document.getElementById('txt_editotherserialnum').value === ""){
alert('Please Input Serial Number');
return false;
}
else{
var y;
if(confirm("Are you sure you want to save updated data?") === true){
var y = document.forms['frm_otherequips2'].submit();
}
else if(confirm("Are you sure you want to save updated data?") === false){
return false;
}
}
}
</script>
What I did is i put return false in controller, here is my controller:
public function edit_delete_others2($id){
$data_other['other'] = $this->inventory_model->other_search($id);
$this->load->view('homeview');
$this->load->view('frm_otherequips2', $data_other);
$this->load->view('footer_view');
if(!empty($_POST['btn_editother'])){
if($_POST['txt_editotherequip'] == "") {
return false;
}
else if($_POST['txt_editotherdesc'] == "") {
return false;
}
else if($_POST['txt_editotherserialnum'] == ""){
return false;
}
else{
$this->inventory_model->update_others($this->input->post());
redirect('inventorysys_controller/view_others', 'refresh');
}
}
}
But everytime i run it and I leave a blank textbox for example 'txt_editotherequip' is blank it posts the alert and then this error occur:
**A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/frm_otherequips2.php
Line Number: 10
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/frm_otherequips2.php
Line Number: 11
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/frm_otherequips2.php
Line Number: 12
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/frm_otherequips2.php
Line Number: 13**
Please do help me on this one, I've been stuck with this for days. Thank you in advance for your help!
Upvotes: 0
Views: 185
Reputation: 164
Make sure that the onclick event occurs, since you said that the form submits whereas in your code it should have popped up a confirm box or an alert on any case.
function validate_edit_otherequip(){
alert('works!');
....
}
Check for javascript errors.
Upvotes: 0
Reputation: 1
Try using the flag variable
and trim function
in the controller.
$flag=1;
if(!empty($_POST['btn_editother'])){
if(trim($_POST['txt_editotherequip']) == "") {
$flag=0;
}
if(trim($_POST['txt_editotherdesc']) == "") {
$flag=0;
}
if(trim($_POST['txt_editotherserialnum']) == ""){
$flag=0;
}
if($flag == 0) return false;
else{
$this->inventory_model->update_others($this->input->post());
redirect('inventorysys_controller/view_others', 'refresh');
}
}
The error Trying to get property of non-object
occurs when the model is either unable to fetch data or the data fetched is not an object at all.
Upvotes: 0