Why I can't update the record in codeigniter? - php

I am getting this error,
A PHP Error was encountered
Severity: Notice
Message: Trying to get property 'post_title' of non-object.
I cannot get the form inputs to print on the page and cannot update. Please help!
So, I have tried to update each record and only artist users are able to do that. But I cannot figure out how to show the individual post on the form page. The records saves correctly and deletes but cannot update them.
Controller
function editpost($post_id)//Edit post page
{
if (!$this->session->Role =='member,artist')
{
redirect(base_url() . 'login');
}
$data['success'] = 0;
if( !empty($this->input->post('post_title')) ) {
$data['post_title'] = $this->input->post('post_title');
$data['post'] = $this->input->post('post');
$data['active'] = 1;
/* >> file attach */
View
<form action="<?= base_url()?>starter/editpost/" method="post" class="justify-
content-center" enctype='multipart/form-data'>
<div class="form-group">
<label class="sr-only">Title</label>
<input type="text" class="form-control" placeholder="Title"
name="post_title" value="<?php echo $post->post_title; ?>">
</div>
<div class="form-group">
<label class="sr-only">Description</label>
<textarea class="form-control" placeholder="Describe it" name="post" ><?
php echo $post->post; ?></textarea>
</div>
<div class="row py-4">
<div class="col-lg-6 mx-auto">
<!-- Upload image input-->
<!-- File Button -->
<div class="col-md-5">
<input name="main_img[]" type="file" accept="image/x-png,image/gif,image/jpeg">
</div>
<br>
<br>
<p style="padding-top: 15px"><span> </span><input class="submit" type="submit" name="update"
value="Publish" /></p>
</form>

use direct the variable because you are passing just values which you are posting from form.
//like :
//for post_title
<input type="text" class="form-control" placeholder="Title"
name="post_title" value="<?php echo $post_title; ?>">
// for description
textarea class="form-control" placeholder="Describe it" name="post" ><?
php echo $post; ?></textarea>

You don't need to pass the post data in controller you can get post data anywhere
<input type="text" class="form-control" placeholder="Title" name="post_title" value="<?php echo ($this->input->post('post_title')!='')?$this->input->post('post_title'):''; ?>">

Related

Form not being posted when a file is attached (xamp localhost)

I have a strange issue where a form is posted correctly when the file input is empty, but nothing is posted when I add a file.
My html:
<form method="post" action="create_product.php" enctype="multipart/form-data">
<?php echo display_error(); ?>
<div class="input-groep">
Homepage
<label>Product naam</label>
<input type="text" name="product_naam" value="<?php echo $product_naam; ?>" autocomplete="off">
</div>
<div class="input-groep">
<label>Beschrijving</label>
<input type="text" name="beschrijving" value="<?php echo $beschrijving; ?>" autocomplete="off">
</div>
<div class="input-groep">
<label>Prijs</label>
<input type="text" name="prijs" value="<?php echo $prijs; ?>" autocomplete="off">
</div>
<div class="input-groep">
<label>Afbeelding</label>
<input type="file" name="afbeelding" accept=".jpg,.jpeg,.png" autocomplete="off">
</div>
<div class="input-groep">
<button type="submit" class="homeknopdonker" name="register_product"> + Maak product aan</button>
Terug naar producten
</div>
</form>
When I submit the form without any values, I can see in my network tab the following:
product_naam:
beschrijving:
prijs:
afbeelding: (binary)
Above data is inside Form Data
However if I attach a file (small image) to the afbeelding input (file) the Form Data dissapears entirely from my network tab when I post it. The paramaters are nowhere to be seen and I get no errors when I turn on error_reporting.
What could be causing this?

How can I make sure a user either inserted a description or file before form submission?

i have a foreach row that displays the "message icon" (i dont think it has anything to do with the form since the form is outside the loop:
enter image description here
which displays a chat form that you can either type a chat or send an attachment:
enter image description here
How can i make one of them required before submission? I tried through multiple JQuery ways but because they are in a for loop and each of them do not have a special id, its not working. any help please? I just need it to show an alert
Here is my code for the form:
<form method="post"action="<?php echo base_url();?>form_support_chat" enctype="multipart/form-data" class="ticket-reply-form">
<div class="row">
<div class="col-xs-12">
<div class="chat-left">
<input type="text" name="message" placeholder="Type Message ..." class="form-control" autocomplete="off">
<input type="hidden" name="cst_id" value="<?php echo $cst_id; ?>">
<input type="hidden" name="arch_ticket" value="<?php echo $arch_ticket; ?>">
<input type="hidden" name="ticket_number" value="<?php echo $ticket_number?>">
</div>
<div class="chat-right">
<label>
<img src="<?php echo base_url();?>/assest/icon-img/paperclip.png" class="ic_img" >
<input type="file" name="file" class="form-control" style="display:none;" id="hidden_upload_file_chatting">
</label>
<button type="submit" class="btn btn-flat" name="reply" value="reply" >Reply</button>
<button type="button" class="btn btn-flat" data-dismiss="modal" style="margin-left: 10px !important;">Close</button>
<span id="_showName"></span>
</div>
</div>
</div>
</form>
Simply use html5's required value
<input name="cst_id" value="<?php echo $cst_id; ?>" required>
or
<input name="cst_id" value="<?php echo $cst_id; ?>" required="true">
update
Using jQuery, add id value to each input
$(document).ready(function() {
if( $('#input1').valid() || $('#input2').valid(); ):
console.log("Success");
endif;
});

Unable to submit form using jquery submit method

What i am trying to do is:
post a form when user is logged in.
but if he is not logged in then pop up login is shown to user.
and in that popup redirection URL is added to hidden field.
when popup opens and i login it redirect me to that form.
But when i try to submit form it not being submitted.
// submit button in form
$('#submitcompanyEnquiry').on('click',function(e){
e.preventDefault();
//get data attr to check if user is login
if($('#companyEnquiry').data('login')){
//companyEnquiry =>form id
//here i try to submit form
console.log('testing'); --->it is working
jQuery('#companyEnquiry').submit(); ---> //the problem is here this piece of code is executing
}else{
if($('#companyEnquiry').attr('action')!=''){
//here i added the current url to hidden field latter to used for redirection
$('#loginForm #redirectUrl').val($('#companyEnquiry').data('seotitle'));
}
//here the login popup is trigger.
jQuery("#login").trigger('click');
}
});
Things that I confirmed:
ensure that there is unique id with the
name provided.
console some value in the if block which was
running but the line of code i have mention.
PHP part is working fine i have removed the e.preventDefault();
it is works fine but doesn't achieve the require functionality.
HTML Code
<form action="<?=Route::url('default',array('controller'=>'contact','action'=>'user_contact'))?>" data-login="<?php echo $data; ?>" data-seotitle="<?=Route::url('company', array('controller'=>'listing','seotitle'=>$company_seotitle))?>" id="companyEnquiry" method="post">
<input type="hidden" name="company_to" value="<?php echo $id; ?>">
<?php if (!$auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="Name" name="name" required aria-describedby="basic-addon1">
</div>
<?php }else { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="Name" required value="<?php echo $auth->get_user()->company_name; ?>" name="name" aria-describedby="basic-addon1">
</div>
<?php } ?>
<?php if (!$auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="email" class="form-control search" placeholder="email" required name="company_from" aria-describedby="basic-addon1">
</div>
<?php }else { ?>
<div class="input-group searchbox">
<input type="email" class="form-control search" placeholder="email" required value="<?php echo $auth->get_user()->companyemail; ?>" name="company_from" aria-describedby="basic-addon1">
</div>
<?php } ?>
<?php if ($auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="phone number" required name="phone" value="<?php echo $auth->get_user()->company_phone_1; ?>" aria-describedby="basic-addon1">
</div>
<?php } else { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="phone number" required name="phone" aria-describedby="basic-addon1">
</div>
<?php } ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="subject" required name="subject" aria-describedby="basic-addon1">
</div>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="message" required name="message" aria-describedby="basic-addon1">
</div>
<input data-login="<?php echo $data; ?>" id="submitcompanyEnquiry" type="submit" name="submit" value="SEND" class="form-control blue-btn send-btn">
</form>
The problem can only exist in the way you are adding the login variable to the form with the id : companyEnquiry
Check if you have add it as correct parameter because jquerys data function will only read values which have a "data-" tag infront of it.
So your php code should look like this :
echo '<form id="companyEnquiry" ' . ($login ? 'data-login="1"' : '' . '>

Keep saved data in form after revisiting the page

I've created a form with three input fields. I managed to find out how to keep the data after saving the form, but when I leave the page and come back to this form, the fields are empty again.
Here is my code:
<?php
if(isset($_POST['save_home_details'])) {
$home_title = escape_string($_POST['home_title']);
$home_desc = escape_string($_POST['home_desc']);
$home_keywords = escape_string($_POST['home_keywords']);
$query = "INSERT INTO settings(home_title, home_desc, home_keywords) ";
$query .= "VALUES('{$home_title}', '{$home_desc}', '{$home_keywords}') ";
$home_details_query = mysqli_query($conn, $query);
confirm($home_details_query);
echo "<div class='alert alert-success'>Settings saved successfully!</div>";
}
?>
<form action="" method="post">
<div class="form-group">
<label for="home_title">Home title</label>
<input type="text" class="form-control" name="home_title" value="<?php if(isset($_POST['home_title'])) { echo htmlentities ($_POST['home_title']); }?>">
</div>
<div class="form-group">
<label for="home_title">Home description</label>
<input type="text" class="form-control" name="home_desc" value="<?php if(isset($_POST['home_desc'])) { echo htmlentities ($_POST['home_desc']); }?>">
</div>
<div class="form-group">
<label for="home_title">Home keywords</label>
<input type="text" class="form-control" name="home_keywords" value="<?php if(isset($_POST['home_keywords'])) { echo htmlentities ($_POST['home_keywords']); }?>">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="save_home_details" value="Save settings">
</div>
</form>
Basically what I want here is:
to be able to add value to these fields
save the values
keep the values in the fields whenever I come back to this page
if I click the save settings button, update the values and not add a new record to the database.
I know I haven't added the update query yet, but can I do that on the same page where I insert the values into the database?
Thanks
If you need to store temporarily you can use $_SESSION['anyData'] = $anyData.
When you do not need you can unset($_SESSION['anyData'])

Sending user data over part 2

I am seeking some more advice on the way to handle this.
I have got one page with links to each admin member which when clicked takes their display name over. On the second page which is a form it takes that display names and populates the subject field with their display name. I need to grab the email address that is associated to that user too but use that as the email address the form on the second page gets sent to as currently my script can only send it to an email address I hard code into it.
So page one is:
<?php
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
foreach ($committee as $user) {
echo '
<a href="../contact-form?displayname=' . $user->display_name . '"><b style="font-size:18px;">
<tr>
<td style="padding: 10px;">' .$user->job_title .' - </td>
<td style="padding: 10px;">' .$user->display_name .'</td>
</tr></b></a><br><br>';
}
?>
Page two is:
<?php $displayname = $_GET['displayname'];?>
<form role="form" method="post" action="../mailuser.php">
<div class="form-group">
<input type="hidden" name="displayname" value="<?php echo $displayname ?>">
<input type="text" name="hp" class="hp" value="" alt="if you fill this field out then your email will not be sent">
</div>
<div class="form-group">
<label for="InputName">Your name</label>
<input type="name" class="form-control" id="InputName" placeholder="Enter your name" name="username">
</div>
<div class="form-group">
<label for="InputEmail">Email address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="you#example.com" name="emailFrom">
</div>
<div class="form-group">
<label for="InputMsg">Message</label>
<textarea class="form-control" rows="8" id="InputMsg" placeholder="Please begin typing your message..." name="emailMessage"></textarea>
</div>
<button type="submit" class="btn btn-primary pull-right">Send</button>
</form>
And my send script has my email hard coded in as:
$mail->From = 'myemail#dummy.com';
So I need that be variable depending on which person you clicked on in the first place. It needs to be sent to both my hard coded email and also the persons email that is associated to them in the Wordpress user database.
Based on our comment discussion, you should be able to do something along the lines of the following on page two. Be sure to correct my email_address, I'm not sure if that's how get_users returns the email address or not.
<?php
$displayname = $_GET['displayname'];
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
$matchingEmail = false;
foreach ($committee as $user) {
if ( !$matchingEmail && $user->display_name == $displayname ) {
// great, we found our match
$matchingEmail = $user->email_address; // I don't know if email_address is right, double check this and modify if necessary
}
}
if ( $matchingEmail ) {
// only proceed if a matching email address is found
?>
<form role="form" method="post" action="../mailuser.php">
<div class="form-group">
<input type="hidden" name="displayname" value="<?php echo $displayname; ?>">
<input type="hidden" name="matchingEmail" value="<?php echo $matchingEmail; ?>">
<input type="text" name="hp" class="hp" value="" alt="if you fill this field out then your email will not be sent">
</div>
<div class="form-group">
<label for="InputName">Your name</label>
<input type="name" class="form-control" id="InputName" placeholder="Enter your name" name="username">
</div>
<div class="form-group">
<label for="InputEmail">Email address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="you#example.com" name="emailFrom">
</div>
<div class="form-group">
<label for="InputMsg">Message</label>
<textarea class="form-control" rows="8" id="InputMsg" placeholder="Please begin typing your message..." name="emailMessage"></textarea>
</div>
<button type="submit" class="btn btn-primary pull-right">Send</button>
</form>
<?php
} else {
?>
<p>Something is wrong, I can't find your email address! Please try again.</p>
<?php
}
?>
Finally on page three, where you send the email, you can do something like:
<?php $mail->addAddress(stripslashes( $_POST["matchingEmail"] ) ); ?>

Categories