form fields display cached values - zend-framework

My policyInfoAction redirects my form to clientInfoAction. It stores the empty field errors and then validates the fields in the session variables and redirects it to the client-info page if it contains errors.
It works fine. But the problem is the next time I hit the /client-info page in a new tab it shows the form values in the fields. I have to hit the refresh page to clear it out. I do not want it to display cached data when I open the link in a new tab. What should I do?
public function clientInfoAction(){
//If there are some errors and some valid fields, display the valid fields
$client=$this->session->client;
$state=$this->session->state;
unset($this->session->client, $this->session->state); // delete from the
// assign the values to the view
$this->view->client = $client;
$this->view->state = $state;
}
Here is my view:
<form action ="/pdp/policy-info/" method='post'">
<label for="client_name">Client Name: </label>
<input type="text" name="client_name" id="client_name">
<?php if (!empty($this->client_error)) echo "<font size='2' color ='#C11B17'>".$this->client_error."</font>"; ?>
<br><br>
<label for="state">State: </label>
<select name="state" id='state'>
<option id='state' value="" selected="selected"></option>

Related

A server side validation problem at razor page

Below is a small part of my data insert form.
My problem is;
The first form object is for the name of the Class room. The field is required and I wanna validate it at server side . In normal it works for sure. But since the next form object is an dropdown menu which get filled from a table of my database, the validation doesn't work. When i post it with empty class room field I get a error.
Normaly it is expected that the server side validation work and stop the posting action right ?
But it doesn't.
What do I miss here ? Thank you.
PS: The teacher field in DB is nullable and when i type something in the class room textbox the form works w/o any problem.
...
...
<div class="col-8 form-floating p-2">
<input type="text" asp-for="AddClassRoom.Class" class="form-control" />
<label asp-for="AddClassRoom.Class"></label>
<span asp-validation-for="AddClassRoom.Class" class="text-danger"></span>
</div>
<div class="col-8 form-floating p-2">
<select class="form-select" asp-for="AddClassRoom.Teacher" asp-items="#(new SelectList(Model.ApplicationUser.OrderBy(x => x.NameSurname).ToList(),"Id","NameSurname"))">
<option value="">select...</option>
</select>
<label asp-for="AddClassRoom.Teacher"></label>
<span asp-validation-for="AddClassRoom.Teacher" class="text-danger"></span>
</div>
...
...
With reference to you comment:
Error is; null parameter(source). Well this is ok, i know it. But there must be a simple way to validate an insert form who has an Database driven content dropdown menu. Post and don't get null after server side validation.
It seems that the dropdown values are not populated after posting the form. Since you have the dropdown values are coming from DB, you need to get its items again from DB after posting the form, so the razor page can fill it again.
More support can be provided if you share your backend code.
[Sample]
Here is a basic sample depending on your code;
<select class="form-select" asp-for="AddClassRoom.Teacher" asp-items="#(new SelectList(Model.ApplicationUser.OrderBy(x => x.NameSurname).ToList(),"Id","NameSurname"))">
<option value="">select...</option>
</select>
Backend:
public ActionResult OnGet() {
// ...
ApplicationUsers = GetApplicationUsersFromDb();
Return Page();
}
public ActionResult OnPost() {
// ...
ApplicationUsers = GetApplicationUsersFromDb();
Return Page();
}

fatfree framework, repopulate fields after unsuccessful validation

I have GET route which shows the contact form, and POST route when user submits the form, then in my controller method I do some validation tests on data being submitted.. how would I now send user back to form if data is not valid, with entered data being re-populated in form fields?
I know I can use isset(#POST.fieldname) in my template, but what's the right way of
sending entered data back to that view, and how to redirect user back to the
form? Is the f3->reroute method right way of doing that?
I think you can take as a rule to include input data inside your form views. This way, any form view will be easily reusable with any source of data.
For example:
Your form view:
<form action="" method="post">
<input type="text" name="email" value="{{ ##input.email }}"/>
<input type="text" name="message" value="{{ ##input.message }}"/>
<button type="submit">Submit form</button>
</form>
Your controller class:
class myController {
function get($f3) {
$this->renderForm();
}
function post($f3) {
$post=$f3->clean($_POST);
//validate form data here
if ($form_validated) {//valid form data
} else //invalid form data
$this->renderForm($post);
}
protected function renderForm($input=array()) {
$tpl=Template::instance();
//variant 1:
echo $tpl->render('form.html','text/html',array('input'=>$input))
// or variant 2:
Base::instance()->set('input',$input);
echo $tpl->render('form.html');
}
}
In some other contexts, you can feed a form view with data coming from a db mapper (for example when editing an entry from a back-office): $this->renderForm($mapper->cast())

inserting text box input into URL parameter without GET method

In this form I want to get the value of the text box Enter Techs name: in to the URL but my form method is post.So that my url would look like
http://localhost/cs_3/index.php?page=chat&tech=
Is there any way I could do it.Can't use get method because I don't want the message to be seen in the URL.
<form method="post" >
<label>Enter Username:<input type="text" name="sender"/></label>
<?php if($user['Level'] == 3){?>
<label>Enter Clients name:<input type="text" name="tech"/></label>
<?php }else{ ?>
<label>Enter Techs name:<input type="text" name="tech" /></label>
<?php } ?>
<label>Enter Message:<textarea name="message" rows="8" cols="70"></textarea></label>
<div id="submit"><input type="submit" name="send" value="Send Message"></div>
</form>
You may use action param at your form.
Change it on key press in tech input by javascript (easy with jquery).

Is there any way possible to apply form requirements to a drop down field in a PHP contact form?

I would like to create a requirement that if nothing is selected from a drop down field in my contact form that a message will come up saying "Please choose", and the form will not be able to be submitted unless something is chosen. I have gotten requirements to work on all of my text input forms, but cannot figure out how to create one for the drop down field.
The drop down HTML looks like this:
<div class='container'>
<label for='destemail' > Which department are you trying to reach?*</br> You must select a department.</label></br>
<select name="destemail" id="destemail">
<?php foreach ($emailAddresses as $name => $email) { ?>
<option value="<?php echo htmlspecialchars($name); ?>"><?php echo htmlspecialchars($name) ; ?></option>
<?php } ?></select>
<span id='contactus_destemail_errorloc' class='error'></span>
</div>
I got the other form requirements to work like so:
The HTML -
<div class='container'>
<label for='name' >Your Full Name*: </label><br/>
<input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
<span id='contactus_name_errorloc' class='error'></span>
</div>
The Javascript -
<script type='text/javascript'>
<![CDATA[
var frmvalidator = new Validator("contactus");
frmvalidator.EnableOnPageErrorDisplay();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("name","req","Please provide your name");
</script>
The PHP -
//name validations
if(empty($_POST['name']))
{
$this->add_error("Please provide your name");
$ret = false;
}
I tried the exact same coding for the drop down but with the different id names where appropriate, and it didn't work. Why doesn't this same method work for the drop down?
Help much appreciated!
I can't see what the Validator() code is doing, but you can just check to see whether the select field is empty using Javascript or jQuery.
jQuery way:
if( !$('#destemail').val() ) {
alert('Empty');
}
The problem may lie in that your select box actually does have a value, which is whatever the first value printed out in it is. The Validation function may be checking for any value, and since the select does have one, it returns as valid.
You could set up a default value to show first, something like "Please select a Department", and then do the jquery/javascript check for that text. If it exists, then you know an option has not been selected.

Bootstrap-datepicker not POSTing hidden values correctly

I am using bootstrap-datepicker and need to send both the date selected and a hidden value when user hits the submit button. Code is so:
<div class='input-append date datepicker' data-date=$today data-date-format='mm-dd-yyyy'>
<form class='well' action='Update_Birthday.php' method='POST'>
<input type='hidden' name='ContactID' value=$ContactID>
<input class='span5' size='16' type='text' name='date' readonly>
<span class='add-on'><i class='icon-calendar'></i></span>
<button type='submit' class='btn'>Submit</button>
</form>
</div>
If I click Submit before selecting a date, then ContactID is sent correctly (but date is null of course). When I select a date, then both 'ContactID' and 'date' are identical, i.e. the date selected by the user.
How do I send the date AND hidden values correctly?
Help appreciated.
With this code (essentially what you have already, with quotes added to values):
<div class='input-append date datepicker' data-date='$today' data-date-format='mm-dd-yyyy'>
<form class='well' id='birthdayForm' action='Update_Birthday.php' method='POST'>
<input type='hidden' name='ContactID' value='$ContactID'>
<input class='span5 dateInput' size='16' type='text' name='date' readonly>
<span class='add-on'><i class='icon-calendar'></i></span>
<button type='submit' class='btn'>Submit</button>
</form>
</div>
There's no reason ContactID should be overwritten when a user selects a date.
As for preventing form submission until a date is selected, that will require some JavaScript. I would suggest just prefilling the date input with a value (possibly the previously specified birthday, judging by your code?) - since the field is readonly, users will generally be unable to empty the value, only change the date.
If you prefer to leave it blank, something like this should help:
$('#birthdayForm').on('submit', function(e) {
$('.dateInput').each(function() {
if ($(this).val().length < 1) {
e.preventDefault();
}
});
});