2 Forms, Same Method but different task - php

How can I make 2 forms which has the same method? One is responsible to open editaddresss.php with hidden addresse ID, and the second form (editaddress.php)should trigger controller and repository to save the data?
The problem is, as soon as I click "Edit Address" button on profile.php, triggers editAddress() method in controller. But I want to only happen when is clicked on editaddress.php
Profile.php
<form action="editaddress.php" method="POST" id="form1">
<tr>
<input type="hidden" name="addressid" value="<?php echo $perssonalAddress->getAddressid(); ?>">
<td><?php echo $perssonalAddress->getStreet(); ?></td>
<td><?php echo $perssonalAddress->getZip(); ?></td>
<td><?php echo $perssonalAddress->getCity(); ?></td>
<td><?php echo $perssonalAddress->getCountry(); ?></td>
<td><?php echo $perssonalAddress->getType(); ?></td>
<td><input type="submit" id="removeitem" value="Edit Address" form="form1"></td>
</form>
Editaddress.php
<form action="editaddress" method="POST" name="editadresse" id="form2">
<input type="hidden" name="update">
<input type="hidden" value="<?php echo $getPersonalAddress->getAddressid(); ?>" name="addressid">
<label for="street">Street</label>
<input type="text" value="<?php echo $getPersonalAddress->getStreet() ?>" id="street" name="street">
<label for="zip">ZIP</label>
<input type="text" value="<?php echo $getPersonalAddress->getZip() ?>" id="zip" name="zip">
<label for="city">City</label>
<input type="text" value="<?php echo $getPersonalAddress->getCity() ?>" id="city" name="city">
<label for="country">Country</label>
<input type="text" value="<?php echo $getPersonalAddress->getCountry() ?>" id="country" name="country">
<input type="submit" class="login" id="login" value="Update Address" name="editadresse" form="form2">
</form>
Controller
/**
* Get Customers specific Address
*/
public function editAddress(): void
{
//Error and Success Messages
$error = false;
$success = false;
//Error if there is no such an address
$message = false;
//Get the specific customer address
$getPersonalAddress = $this->profileService->getAddressById();
if ($getPersonalAddress === false) {
$message = $this->profileService->getMessages();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $this->addressForm->isValid($_POST)) {
$this->profileService->updateCustomerAddress();
$success = $this->profileService->getMessages();
}else {
$error = $this->addressForm->getFormErrorMessage();
}
//Render to View
$this->render('editaddress', ['getPersonalAddress' => $getPersonalAddress, 'message' => $message, 'error' => $error, 'success' => $success]);
}

I think you're approaching this incorrectly. You are trying to get one end-point (editaddress.php) to do two different things.
instead, you should have one end-point for generating a form with the details filled in.
and a different end-point for recording data saved from that form.

Related

Codeigniter Show Data from Database into Textbox

I created an accounts page wherein I can view all accounts from the database. Each of them has an action button called View More. If I press View More, additional details from the database will be displayed in textboxes. I want to get the ID for each of the row and pass it to the controller in order for it to be viewed. However, I can't seem to make them show in the boxes.
Expected: Image
Current: Image
Controller:
function viewmore()
{
$userid = $this->input->get('userid', TRUE);
$data['view'] = $this->model_accounts->viewmore_user($userid);
$data['main_content'] = 'view_adminviewmore';
$this->load->view('includes/admin_viewmore_template', $data);
}
Model:
public function viewmore_user($userid)
{
$query= $this->db->select('*')->from('accounts')->where('userid', $userid)->get();
return $query->result();
}
View for the Action Button:
<?php foreach($users as $row): ?>
<tr>
<td><?php echo $row->firstname; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->address; ?></td>
<td class="action-button mobile-important">
<button type="button" class="btn btn-custom-3">View More</button>
</td>
<?php endforeach; ?>
</tr>
View for displaying the data:
<?php foreach($view as $row): ?>
<br>
<p> First Name </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->firstname; ?>">
<br>
<p> Last Name </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->lastname; ?>">
<br>
<p> Username </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->username; ?>">
<br>
<p> Address </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->address; ?>">
<br>
<p> E-mail Address </p>
<input class="form-control" id="sel1" type="email" placeholder="" value="<?php echo $row->email; ?>">
<br>
<p> Contact Number </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->contactnum; ?>">
<br>
<?php endforeach; ?>
If you are using CodeIgniter, why not use the Form Helper?
You can use code like this:
<?php echo form_input('sel1', $row->firstname); ?>
instead of:
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->firstname; ?>">
Read some more about it here: https://www.codeigniter.com/user_guide/helpers/form_helper.html
your action button must be like this:
you can use <?="hello word!!!" ?> instead of <?php echo "hello word!!!" ?>
and base_url() return the root of your codeigniter folder that set
in application->config->config.php
<a href="<?=base_url()." YOUR_CONTROLLER_NAME/viewmore/".USER_ID ?>View More</a>
link above return <a href="localhost/YOUR_CONTROLLER_NAME/viewmore?id=USER_ID>View More</a>
and your function viewmore() must be change to function viewmore($id=0),this $id give USER_ID from your url
and you better to know url in codeigniter contain this:
Controller/Function/variable that you want pass to your function

How to Keep the data that user entered in textbox even after submit button is clicked and even if page is reloaded

I want to keep the data in a text box after submit and inserted into db.
even if i refresh the page data should not change or empty
here is my code
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_admin"><?php _e('Collaboration Admin', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_admin" name="approved_admin" type="email" value="<?php echo esc_attr($item['approved_admin'])?>"size="50" class="code" placeholder="<?php _e('Admin Email', 'custom_table_example')?>" required>
</td>
</tr>
if i do this data will be there if i reload empty text box will be shown.I need to make this readonly and onload same text should be there.
Form
<form id="form" method="POST">
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
<?php /* NOTICE: here we storing id to determine will be item added or updated */ ?>
<input type="hidden" name="sid" value="<?php echo $item['sid'] ?>"/>
<div class="metabox-holder" id="poststuff">
<div id="post-body">
<div id="post-body-content">
<?php /* And here we call our custom meta box */ ?>
<?php do_meta_boxes('Settings', 'normal', $item); ?>
<input type="submit" value="<?php _e('Save', 'custom_table_example')?>" id="submit" class="button-primary" name="submit" / >
<input type="submit" value="<?php _e('Edit', '')?>" id="edit" class="button-primary" name="edit" / >
</div>
</div>
</div>
</form>
I tried doing
<input type="email" name="approved_admin" value="<?php if (isset($_POST['approved_admin'])) { echo $_POST['approved_admin']; } ?>">
and also
<input type="text" name="country" value="Norway" <?php isset($approved_admin) ? echo 'readonly' : ''; ?> >
You can save data after page reload a lot of ways like... Session , cookie, get , post. After this value get by isset and echo .
In your case Post method used..
Try this working fine all thing
<input type="text" name="country" value=" <?php echo isset($_POST['country']) ? $_POST['country'] : ''; ?>" <?php echo isset($_POST['country']) ? 'readonly' : ''; ?> >
You just put field name in isset();
You can use this syntax. Have a try for this.
<input type="text" id="to_date" name="to_date" class="form-control" placeholder="Enter End Date" value="<?php echo isset($_POST['to_date']) ? $_POST['to_date'] : '' ?>" />
The name of the input type has to be mentioned in the isset() condition.

save fields values during form submit

I want that when the user submits the form, and some reason happen some error, I want that the fields that the user already wrote to be saved, so I want to be show the values the user already wrote.
Im doing this with code below, but I dont understand why, when I submit the form the fields stay empty.
Somebody there see something wrong?
<?php
if (isset($_POST['sendForm'])) {
$f['name'] = $_POST['name'];
$f['content'] = $_POST['content'];
$f['date'] = $_POST['date'];
} ?>
<form name="form" action="" method="post">
<label class="line">
<span class="data">Name:</span>
<input type="text" name="name" value="<?php if (isset($f['name'])) echo $f['name']; ?>"/>
</label>
<label class="line">
<span class="data">Content:</span>
<textarea name="content" rows="3" value="<?php if (isset($f['content'])) echo $f['content']; ?>"></textarea>
</label>
<label class="line">
<span class="data">Date:</span>
<input type="text" name="date" value="<?php if (isset($f['date'])) {
echo $f['date'];
} else {
echo date('d/m/Y H:i:s');
} ?>"/>
</label>
<input type="submit" value="Create" name="sendForm" class="btn"/>
</form>
In short you can set by this way,
<input type="text" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />
You do not need to use $f['name']. you can get value directly by $_POST['name'] method.

Save input for next input

Well, I have this code:
<form name="download" id="download" method="post" enctype="multipart/form-data">
<div>
<label for="beginning_date"><span class="required">*</span> Beginning Date: </label>
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo date("d.m.Y"); ?>" required="required" />
</div>
<input type="submit" value="Next" name="next"/>
</form>
and php..
if(array_key_exists('next', $_POST))
{
if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{4}$/', $date))
{
//the code continues...
}
else
{
echo "Wrong formation <br />";
}
}
This is a long form, and I want that if someone inputs a date like this "02.03.20322", and the error appears, they don't have to start over to fill the form... the form already appears with the "fills field"...
I've already tried like these:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if (isset($date)) { echo $date; } else { echo date("d.m.Y"); } ?>" required="required" />
but... no changes :(
Looking at your code, there simply is no $date so what you should check for is the existence of $_POST['beginning_date'] instead of $date like so:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if (isset($_POST['beginning_date'])) { echo $_POST['beginning_date']; } else { echo date("d.m.Y"); } ?>" required="required" />
When using method post the submitted values of the user go into the $_POST array. The name of the form element is used as key.
So to retrieve the date submitted by the user, you should use:
$_POST['beginning_date']
Just save it in $_SESSION,
at the top of page or anywhere in your action.php:
if (isset($_POST)){
$_SESSION['date'] = $_POST['beginning_date'];
}
and then:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo (isset($_SESSION['date'])) ? $_SESSION['date'] : date("d.m.Y");?>" required="required" />
Using session is useful in case that in the future you need to change your action page. but if you want to use post instead(for same page posting), just echo it:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo (isset($_POST['beginning_date'])) ? $_POST['beginning_date'] : date("d.m.Y"); ?>" required="required" />
php..
session_start();
if(array_key_exists('next', $_POST))
{
if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{4}$/', $date))
{
//the code continues...
}
else{
echo "Wrong formation <br />";
$_SESSION['date']=$_POST['beginning_date'];
}
}
html..
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if
(isset($_SESSION['date'])) { echo $_SESSION['date'] } else { echo date("d.m.Y"); } ?>"
required="required" />

How to get form fields refilled when data is incorrect

The script for handling the action of my form redirects to the form page if the values are not in proper format. I want to fill the textfields and textarea with the faulty data the user entered on redirect to the form page. I have written the following script which redirects the page on wrong value submission, but does not fill the fields thereafter.
script on form page:
<?php
if(session_id('stat')=="true")
{
$isbn=$_SESSION['var1'] ;
$name=$_SESSION['var2'] ;
$author=$_SESSION['var3'] ;
$publisher=$_SESSION['var4'];
$price=$_SESSION['var5'];
$descrip=$_SESSION['var6'];
$status=$_SESSION['stat'];
}
else
{
$isbn="";
$name="";
$author="";
$publisher="";
$price="";
$descrip="";
$status=false;
}
?>
The html part of the form:
<form action="scripts/addscript.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<label for="isbn">ISBN</label>
<input type="text" name="isbn" id="isbn" value="<?php $isbn ?>"/>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>"/>
</p>
<p>
<label for="author">Author</label>
<input type="text" name="author" id="author" value="<?php echo $author; ?>"/>
</p>
<p>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" id="publisher" value="<?php echo $publisher; ?>"/>
</p>
<p>
<label for="price">Price</label>
<input type="text" name="price" id="price" value="<?php echo $price;?>"/>
</p>
<p>
<label for="description">Description</label>
<textarea name="description" id="description" cols="45" rows="5"><?php echo $descrip; ?></textarea>
</p>
<p>
<label for="img">Select an image for the book:
<input type="file" name="img" id="img" />
</label>
<input type="submit" name="submit" id="submit" value="Submit"/>
</p>
</form>
The redirecting script on addscript.php to which the form values are submitted:
<?php
// Get values from form
$isbn=$_POST['isbn'];
$name=$_POST['name'];
$author=$_POST['author'];
$publisher=$_POST['publisher'];
$price=$_POST['price'];
$descrip=$_POST['description'];
$_SESSION['var1'] = $isbn;
$_SESSION['var2'] = $name;
$_SESSION['var3'] = $author;
$_SESSION['var4'] = $publisher;
$_SESSION['var5'] = $price;
$_SESSION['var6'] = $descrip;
if(strlen($isbn)==0||strlen($name)==0||strlen($author)==0||strlen($publisher)==0||strlen($price)==0)
{
$_SESSION['stat']="true";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Please telll me where is the problem and how can I solve the issue?
Thanks in advance.
session_start() must be called at the top of your PHP script to use the $_SESSION variable.

Categories