How to align text input and label when validation appears - php

Got a form table below which contains form elements such as text inputs and validation messages:
$error_user = (!empty($errors['user']))?$errors['user']:"";
$error_email = (!empty($errors['email']))?$errors['email']:"";
echo "<form action='./forgotpass.php' method='post'>
<table>
<tr>
<td></td>
<td id='errormsg'>$errormsg</td>
</tr>
<tr>
<td>Username</td>
<td><input type='text' name='user' value='$user'/><br/>".$error_user."</td>
</tr>
<tr>
<td>Email</td>
<td><input type='text' name='email' value='$email'/><br/>".$error_email."</td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='resetbtn' value='Reset Password' /></td>
</tr>
</table>
</form>";
Now I am displaying each validation message below the relevant text inputs but the problem is the allignment. If I display the message underneath the text input as I am doing now, then the text input moves up a little bit to occupy the space below for the validation message, meaning the text input is not leveled with its label e.g Email text input not aligned with this label when validation appears.
My question is that how can I get the label and text inputs to stay align with each other when the validation message appears underneath

If you use this:
echo "<form action='./forgotpass.php' method='post'>
<table>
<tr>
<td></td>
<td id='errormsg'>$errormsg</td>
</tr>
<tr>
<td valign='top'>Username</td>
<td valign='top'><input type='text' name='user' value='$user'/><br/>Username Error</td>
</tr>
<tr>
<td valign='top'>Email</td>
<td valign='top'><input type='text' name='email' value='$email'/><br/>Email Error</td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='resetbtn' value='Reset Password' /></td>
</tr>
</table>
</form>";
You should get the look you want. The reason the message distorts the row is because it is enlarging one cell so the cell with the labels in are being aligned vertically in the middle. Notice the valign:top? That will push everything to the top of the cell so everything in the row on the first line, will be in line.
See this JSFiddle

Add style="vertical-align: top" to all TD tags. You may also need to add a little padding to the label cells, to align them properly with the content in the input boxes.

create another <td> element for displaying error message like this edit
<tr>
<td>Username</td>
<td><input type='text' name='user' value='$user'/></td>
<td>".$error_user."</td>
</tr>

also, besides everything mentioned, and if it doesn't screw you anything else, i would try adding a space in the empty td's like this:
<td> </td>
<td id='errormsg'>$errormsg</td>
it will make things equally distributed

Related

How to get the entries of field generated by a loop

Please I need assistance on how to get the input values of field that was generated by a loop:
if($moreDetails){
while($row=mysqli_fetch_assoc($moreDetails)){
$id="$row[id]";
$sname="$row[sname]";
$fname="$row[fname]";
$sub="$row[$subject]";
echo "
<tr>
<td>$id</td>
<td>$sname</td>
<td>$fname</td>
<td><label for='score'></label>
<input type='text' class='form-control' name='score'/>
<input type='hidden' name='assessment'/>
</td>
</tr>
";
}
}
My challenge is to get the input of the score entries.
You should give the inputs array-style names.
if($moreDetails){
while($row=mysqli_fetch_assoc($moreDetails)){
$id="$row[id]";
$sname="$row[sname]";
$fname="$row[fname]";
$sub="$row[$subject]";
echo "
<tr>
<td>$id</td>
<td>$sname</td>
<td>$fname</td>
<td><label for='score'></label>
<input type='text' class='form-control' name='score[$id]'/>
<input type='hidden' name='assessment[$id]'/>
</td>
</tr>
";
}
}
Then when you're processing the form, the $_POST parameters will be arrays, so you can access $_POST['score'][$id] and $_POST['assessment'][$id].
It's also strange that you don't have value="something" in the hidden input. Are you using Javascript on the client side to fill this field in automatically?

Retrieving form data into a php processor

I am having a problem understanding how to make the foreach function work. I have a form, that uses check boxes. I tried loading them as an array of check boxes, but I couldn't get the foreach function to work for me. I don't properly understand all of this. Let me post my form data real quick:
<form method="post" action="interests.php" name="interests">
<table width="500" cellspacing="3" cellpadding="3" bgcolor="#FF80FF" bordercolor="#800000">
<tr>
<td><input type="checkbox" name="check1" value="1"></td>
<td><input type="checkbox" name="check2" value="2"></td>
<td><input type="checkbox" name="check3" value="3"></td>
<td><input type="checkbox" name="check4" value="4"></td>
</tr>
<tr>
<td><input type="checkbox" name="check5" value="5"></td>
<td><input type="checkbox" name="check6" value="6"></td>
<td><input type="checkbox" name="check7" value="7"></td>
<td><input type="checkbox" name="check8" value="8"></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></td>
<td colspan="2"><input type="reset"></td>
</tr>
</table>
</form>
Here is my php processor so far..:
<?php
for(x=1;x>8;x++){
$loop=
echo 'Your interests have been updated into the database, and those files will now</br>';
echo 'begin showing up in the files area on your files sections.';
?>
I know, not much, because I am stuck. I am banging my head. I need to parse this out, and check for values filled, so I can input the data into a database. Seems simple enough, but for some reason I cant get it!
After your edit , i suggest to give all your checkbox name='check[]' and in your php code you will get all values of inputs that are checked in $_POST['check'] without trying to do it manually,this code will echo all checked values :
<?php
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
echo $check.'<br/>'; //echo checked value
}
}
?>
First answer :
To minimize your HTML code using php use modulus ($a % $b Modulus Remainder of $a divided by $b) to show <tr> element before every 4 inputs
Try this :
<form method="post" action="interests.php" name="interests">
<table width="500" cellspacing="3" cellpadding="3" bgcolor="#FF80FF" bordercolor="#800000">
<?php
for($i=1;$i<=8;$i++){
if (($i-1)%4==0) echo "<tr>";
echo "<td><input type='checkbox' name='check$i' value='$i'/></td>";
}
?>
<tr>
<td colspan="2"><input type="submit"></td>
<td colspan="2"><input type="reset"></td>
</tr>
</table>
</form>

Automatically get logged in user's username in Name field of html form

Currently users logged in have to type their name manually to comment or post but i want to
get the username in the field name of html form inserted automatically. Also at the same time user cannot change the name field.
how can i create such session in username field, i tried but code isnt working..
like this
//create the form to submit comments
//you can add more fields, but make sure you add them to the db table and the page, submitcomment.php
echo "
<a name=\"post\">
<div id=\"submitcomment\" class=\"submitcomment\">
<form name=\"submitcomment\" method=\"post\" action=\"submitcomment.php\" onSubmit=\" return form_Validator(this)\">
<table width=\"100%\">
<tr>
<th colspan=\"2\"><h4><span>Leave your comment:</span></h4></th>
</tr>
<th scope=\"row\"><p class=\"req\">Name:</p></th>
<td><input type= class=\"form\" tabindex=\"1\" id=\"name\" name=\"name\" /></td>
</tr>
<tr>
<th scope=\"row\"><p class=\"opt\">Email:</p></th>
<td><input class=\"form\" tabindex=\"2\" id=\"email\" name=\"email\" /></td>
</tr>
<tr>
<th scope=\"row\"><p class=\"opt\">URL:</p></th>
<td><input class=\"form\" tabindex=\"3\" id=\"url\" name=\"url\" /></td>
</tr>
<tr valign=\"top\">
<th scope=\"row\"><p class=\"req\">Comments:</p><br /></th>
<td><textarea class=\"formtext\" tabindex=\"4\" id=\"message\" name=\"message\" rows=\"10\" cols=\"50\"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type=\"submit\" name=\"post\" class=\"submit\" value=\"Submit Comment\" /><br />
<p>Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted. </p>
<p>In Beta Phase</p>
</td>
</tr>
</table>
<input type=\"hidden\" name=\"tuturl\" value=\"$tuturl\" />
<input type=\"hidden\" name=\"tutid2\" value=\"$tutid2\" />
</form>
</div>
";
}
?>
<?php
session_start();
// after user logs in successfully
$_SESSION['username']=$username;
// to read it
$username=$_SESSION['username'];
// then store $username in database when inserting comment
?>
Try using the following code on your page where you want to add name automatically.
<?php
session_start();
$_SESSION['name']=$name;
$name=$_SESSION['name']; // assign session to name variable
?>
write $name anywhere you want name to be displayed.
then edit your following line of code
<td><input type= class=\"form\" tabindex=\"1\" id=\"name\" name=\"$name\" readonly=\"readonly\" /></td>

Input value entering to database

<td height="25" align="center" valign="middle"><input type='text' name='day2' id='datepicker2' value='Day 2' readonly="readonly" maxlength="50" style="color:#999999"/></td>
</tr>
<tr>
<td height="25" align="center" valign="middle"><input type='text' name='day3' id='datepicker3' value='Day 3' readonly="readonly" maxlength="50" style="color:#999999"/></td>
</tr>
<tr>
<td height="25" align="center" valign="middle"><input type='text' name='day4' id='datepicker4' value='Day 4' readonly="readonly" maxlength="50" style="color:#999999"/></td>
</tr>
<tr>
<td height="25" align="center" valign="middle"><input type='text' name='day5' id='datepicker5' value='Day 5' readonly="readonly" maxlength="50" style="color:#999999"/></td>
The values Day 4, Day 5, Day 6 etc. are enetring into database. How I block these from entering into database?
How I block these from entering into database?
It sounds like you have some system that takes all your values and posts them to the database... But we can not answer your question without seeing the php code.
Forms have little to nothing to do with mysql insert commands. They just receive information from the browser.
It isn't the most popular method but, you can use the placeholder attribute to still display the default value you want to display and leave the value attribute blank. I do this with a jQuery date picker application.
<input type="text" name="day4" id="datepicker4" value="" placeholder="Day 4" readonly maxlength="50" style="color:#999999"/>
This gives a grayed out effect while still showing the user what should go there. When a value is entered the value attribute takes over.
Also, I have just corrected the attribute tags from single quotation marks to double as well as the readonly attribute.

Php Insert Lots of data to mysql

Hi
I have an evaluation form with more than 300 fields, where there are several parts. i don't want to make 300 rows in mytable.Can someone give me a method how to save my data as it need to be easy to retrive to make some stats.
Thanks.
Edit:
Something like that with lots of differnt parts
<form action="" method="post" id="OKF" name="OKF" autocomplete="off">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="25%" class="tableft"><strong>Date </strong></td>
<td width="25%"><input name="controldate" type="number" id="controldate" /></td>
<td width="25%"><strong>Start Time</strong></td>
<td><input type="text" name="startime" id="startime" class="required"/></td>
</tr>
<tr>
<td class="tableft"><strong>Controller</strong></td>
<td><input type="text" name="controller" id="controller" /></td>
<td><strong>End Time</strong></td>
<td><input type="text" name="endtime" id="endtime" class="required"/></td>
</tr>
<tr>
<td class="tableft"><strong>Hotel Name</strong></td>
<td><input type="text" name="otelname" id="otelname" class="required"/></td>
<td><strong>Company Name</strong></td>
<td><input type="text" name="compres" id="compres" class="required"/></td>
</tr>
<tr>
<td class="tableft"><strong>No Stars</strong></td>
<td><input type="text" name="stars" id="stars" class="required"/></td>
<td><strong>No Room</strong></td>
<td><input type="text" name="roomnum" id="roomnum" class="required"/></td>
</tr>
</table>
<br>
<h1>Part 1</h1>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="40%" class="tabtopmid">Question</td>
<td width="4%" class="tabtopmid"></td>
<td width="17%" class="tabtopmid"><strong>Point</strong></td>
<td width="37%" class="tabtopmid"><strong>Comment</strong></td>
</tr>
<tr>
<td>Question 1</td>
<td>20</td>
<td><input name="puan_1" id="puan_1" type="text"></td>
<td><textarea name="cmt_1" id="cmt_1"></textarea></td>
</tr>
<tr>
<td>Question 2</td>
<td>20</td>
<td><input name="puan_2" id="puan_2" type="text"/></td>
<td><textarea name="cmt_2" id="cmt_2"></textarea></td>
</tr>
<tr>
<td>Question 3</td>
<td>20</td>
<td><input name="puan_3" id="puan_3" type="text"/></td>
<td><textarea name="cmt_3" id="cmt_3"></textarea></td>
</tr>
<tr>
<td>Question 4</td>
<td>20</td>
<td><input name="puan_4" id="puan_4" type="text"/></td>
<td><textarea name="cmt_4" id="cmt_4"></textarea></td>
</tr>
<tr>
<td>Question 5</td>
<td>20</td>
<td><input name="puan_5" id="puan_5" type="text"/></td>
<td><textarea name="cmt_5" id="cmt_5"></textarea></td>
</tr>
</table>
<input name="submit" type="submit" id="submit" value="Submit" alt="Submit" title="Submit">
depending on your fields would recommend you to make more than one table
form
- id
1
n
questions
- id
- type
- value
- label
- form_id
and then you could store the answer of the users in a third table
easier to you would be names like formname[foobar], formname[foobar_1] and so on
then you are able to do something like:
$sql = "INSERT INTO answers (name, value) VALUES ";
foreach ($_POST['formname'] as $key => $value) {
$sql .= "('".$key."', '".$value."'),";
}
$sql = substr($sql, 0, -1).";";
mysql_query($sql);
please note, this is only an example.. no security, no valiation, nothing
So this is a hotel registration form , i take it.
Well, the Questions should definitely be a separate table. Then, i guess, there should be a table for Reservations ( containing hotel_id, room_id, customer_id, date, additional info about reservation ) .. then a Customers table and Hotels table, and Rooms table ... and maybe few more.
It's impossible to tell, without knowing the the entire list of fields.
I never worked with 300 columns in one table. My suggestion is use JSON or someother serialized data technic for columns. Grouping related fields can save time in any case.
Recently used;
column = value (variable1:value1-variable2:value2-var3:val3)
adv_room_numbers(text) = "Room:2-Bedroom:3-Kitchen:1-Dining Room:1,etc..room:1,etc..room:1,etc..room:1" // I can use explode() to extract fields
adv_online(enum) = "active"
adv_contact(text) = "Name:Name Surname-Tel:65465-City:Cityname-Address:address details"
Is it all coming from one big form? If so, it might just make sense to put all the data into one array, serialise it, and log it to a file. Then analyse the data after with a more specialist tool.
If you do want to put it into a database, you need to spend some time breaking out the schema - I'm sure people would help but at the moment, the definition is too vague to say anything meaningful.
It might help you to use a framework to map between the front end form and the database. Symfony is excellent at form generation - you just write the files into a YAML file and run a command.

Categories