loop to reduce writing many form containers - php

I have 12 forms that need to be in my page, however they don't really differ that much, for example:
<label>Player 1*</label>
<div class="form-group">
<input class="form-control" name="h-p1-fn" placeholder="First name" type="text"/>
</div>
<div class="form-group">
<input class="form-control" name="h-p1-ln" placeholder="Last name" type="text" />
</div>
<div class="form-group">
<input class="form-control" name="h-p1-nr" placeholder="Number" type="number" min="0" max="99" step="1" />
</div>
<label>Player 2*</label>
<div class="form-group">
<input class="form-control" name="h-p2-fn" placeholder="First name" type="text" />
</div>
<div class="form-group">
<input class="form-control" name="h-p2-ln" placeholder="Last name" type="text" />
</div>
<div class="form-group">
<input class="form-control" name="h-p2-nr" placeholder="Number" type="number" min="0" max="99" step="1" />
</div>
As you can see the forms do not differ much. The only differences:
labels increment(Player 1*, Player 2*, Player 3* and etc. the asterisk is for labels 1 to 5), input name increments (h-p1-fn, h-p2-fn and so on).
How can i reduce this code, maybe using a loop? All of this is in a .php file, so it would be better if it were an loop.

You can do this way:
<?php
for($i=1; $i<=12; $i++) {
?>
<label>Player <?php echo $i; ?>*</label>
<div class="form-group">
<input class="form-control" name="h-p<?php echo $i; ?>-fn" placeholder="First name" type="text"/>
</div>
<div class="form-group">
<input class="form-control" name="h-p<?php echo $i; ?>-ln" placeholder="Last name" type="text" />
</div>
<div class="form-group">
<input class="form-control" name="h-p<?php echo $i; ?>-nr" placeholder="Number" type="number" min="0" max="99" step="1" />
</div>
<?php } ?>

what you want can be achieved with this code
$cnt=1;
for($i=0;$i<5;$i++){
echo '
<label>Player .'$cnt'.*</label>
<div class="form-group">
<input class="form-control" name="h-p.'$cnt'.-fn" placeholder="First name" type="text" />
</div>
<div class="form-group">
<input class="form-control" name="h-p.'$cnt'.-ln" placeholder="Last name" type="text" />
</div>
<div class="form-group">
<input class="form-control" name="h-p.'$cnt'.-nr" placeholder="Number" type="number" min="0" max="99" step="1" />
</div>';
$cnt++;
}

You can also try this:
for($i=0;$i<=12;$i++){
?>
<label>Player<?php echo $i ?>*</label>
<div class="form-group">
<input class="form-control" name="h-p<?php echo $i; ?>-fn" placeholder="First name" type="text" />
</div>
<div class="form-group">
<input class="form-control" name="h-p<?php echo $i; ?>-ln" placeholder="Last name" type="text" />
</div>
<div class="form-group">
<input class="form-control" name="h-p<?php echo $i; ?>-nr" placeholder="Number" type="number" min="0" max="99" step="1" />
</div>
<?php
}

Related

Pop up contact form disappears when clicking the submit button

The form is based on bootstrap here is the code:
When i try to validate the submition of the for using this very simple code.
Then the form just disappears. Any insights guys would be greatly appreciated.
Thanks
New gal to coding.
<div class="container">
<div class="row">
<div class="col-sm-12 contact-form">
<form id="contact" method="post" class="form" role="form">
<div class="row">
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="name" name="name" placeholder="FIRST NAME" type="text" required autofocus />
</div>
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="name" name="lastname" placeholder="LAST NAME" type="text" required
autofocus />
</div>
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="email" name="email" placeholder="EMAIL" type="email" required />
</div>
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="name" name="phone" placeholder="PHONE" type="text" required autofocus />
</div>
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="name" name="address1" placeholder="ADDRESS 1" type="text" required
autofocus />
</div>
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="name" name="address2" placeholder="ADDRESS 2" type="text" required
autofocus />
</div>
<div class="col-xs-6 col-md-6 form-group">
<input class="form-control" id="name" name="city" placeholder="CITY" type="text" required autofocus />
</div>
<div class="col-xs-3 col-md-3 form-group">
<input class="form-control" id="name" name="state" placeholder="STATE" type="text" required autofocus />
</div>
<div class="col-xs-3 col-md-3 form-group">
<input class="form-control" id="name" name="zipcode" placeholder="ZIPCODE" type="text" required autofocus />
</div>
<h1 class="h1-contact">WHAT SERVICES ARE YOU INTERESTED IN?</h1>
</div>
<div class="row">
<div class="col-sm-6">
<h3>SELECT ALL THAT APPLY:</h3>
<label class="checkbtn">item 1
<input type="checkbox" checked="checked" name="item5" value="item5">
<span class="checkmark"></span>
</label>
<label class="checkbtn"> item 2
<input type="checkbox" name="item4" id="item4">
<span class="checkmark"></span>
</label>
<label class="checkbtn">item 3
<input type="checkbox" name="item3" id="item3">
<span class="checkmark"></span>
</label>
<label class="checkbtn">item 4
<input type="checkbox" name="item2" id="item2">
<span class="checkmark"></span>
</label>
<label class="checkbtn">item 5
<input type="checkbox" name="example1">
<span class="checkmark"></span>

Issue populating textarea with db data

So, like the title said, i'm having an issue while i'm populating a text area that is being used on HTML form, because it is not getting any content in it.
this is the form with the php while.
<?php
while ($row = mysqli_fetch_array($query))
{ echo '
<form action="insert/insertReport.php" id="newReport" method="post">
<div class="form-group">
<label for="clientRep">Cliente</label>
<br>
<input type="text" name="client" class="form-control" id="client" value="'.$row['client'].'">
</div>
<div class="form-group">
<label for="titleRep">Título do Relatório</label>
<br>
<input type="text" name="title" class="form-control" id="title" value="'.$row['title'].'">
</div>
<div class="form-group">
<label for="namefat">Data</label>
<br>
<input type="text" name="date" class="form-control" id="date" value="'.$row['date'].'">
</div>
<div class="form-group">
<label for="localRep">Local</label>
<br>
<input type="text" name="local" class="form-control" id="local" value="'.$row['local'].'">
</div>
<div class="form-group">
<label for="reportRep">Relatório</label>
<br>
<textarea rows="12" name="report" class="form-control" id="report" form="newReport" value="'.$row['report'].'"></textarea>
</div>
<input type="hidden" name="id" class="form-control" id="id" value="'.$row['id'].'">';
}?>
And this is the php query.
$sql = 'SELECT * FROM reports'
Does anyone know what's wrong with it?
Text area does not accept a value attribute. You place the contents between the textarea tags:
<textarea rows="12" name="report" class="form-control" id="report" form="newReport">'.$row['report'].'</textarea>

Form submittion wont read required in the select tag

I have a page where the users can register accounts which looks like this:
The problem is, when the user doesn't or forgot to fill up gender, it just submits the form without an error even if I have a required function on the select tag. It's only the gender that I'm having problems with. The required function works with the other fields.
here's my form:
<div class="main">
<div class="one">
<div class="register">
<center><h3>Add Account</h3></center>
<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
<div>
<label>ID</label>
<input type="text" name="id" required>
</div>
<div>
<label>First Name</label>
<input type="text" name="firstname" required>
</div>
<div>
<label>Last Name</label>
<input type="text" name="lastname" required>
</div>
<div>
<label>Email</label>
<input type="text" name="email" placeholder="user#teamspan.com" required>
</div>
<div>
<label>Username</label>
<input type="text" name="username" required>
</div>
<div>
<label>Password</label>
<input type="password" name="password" required>
</div>
<div>
<label>Street Address</label>
<input type="text" name="street" required>
</div>
<div>
<label>Town/Suburb</label>
<input type="text" name="town" required>
</div>
<div>
<label>City</label>
<input type="text" name="city" required>
</div>
<div>
<label>Phone No.</label>
<input type="text" name="phone">
</div>
<div>
<label>Landline No.</label>
<input type="text" name="landline">
</div>
<div>
<label>Gender</label>
<select name="gender" required>
<option disabled selected hidden>Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<div>
<label>User Levels</label>
<select name="user_levels" required>
<option disabled selected hidden>Select Access Level</option>
<option value="0">Employee</option>
<option value="1">Administrator</option>
<option value="2">Manager</option>
<option value="1">HR</option>
</select>
</div>
<div>
<label>Date</label>
<input type="text" readonly="readonly" name="date" value="<?php echo date("Y/m/d");?>" required>
</div>
<div>
<label>Sick Leave</label>
<input type="text" name="sickleave" required>
</div>
<div>
<label>Vacation Leave</label>
<input type="text" name="vacationleave" required>
</div>
<div>
<label>Picture (Link)</label>
<input type="text" name="picture" value="img/emp/" required>
</div>
<div>
<label></label>
<input type="submit" name="submit" value="Add Account" class="button" style="color: white;" />
<a href="hr_panel.php"><input type="button" value="Back" class="button" style="color: white;" />
</div>
</form>
</div>
</div>
You need to give value="" on first option i.e.Select Gender. Try following code
<div class="main">
<div class="one">
<div class="register">
<center><h3>Add Account</h3></center>
<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
<div>
<label>ID</label>
<input type="text" name="id" required>
</div>
<div>
<label>First Name</label>
<input type="text" name="firstname" required>
</div>
<div>
<label>Last Name</label>
<input type="text" name="lastname" required>
</div>
<div>
<label>Email</label>
<input type="text" name="email" placeholder="user#teamspan.com" required>
</div>
<div>
<label>Username</label>
<input type="text" name="username" required>
</div>
<div>
<label>Password</label>
<input type="password" name="password" required>
</div>
<div>
<label>Street Address</label>
<input type="text" name="street" required>
</div>
<div>
<label>Town/Suburb</label>
<input type="text" name="town" required>
</div>
<div>
<label>City</label>
<input type="text" name="city" required>
</div>
<div>
<label>Phone No.</label>
<input type="text" name="phone">
</div>
<div>
<label>Landline No.</label>
<input type="text" name="landline">
</div>
<div>
<label>Gender</label>
<select name="gender" required>
<option disabled selected value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<div>
<label>User Levels</label>
<select name="user_levels" required>
<option disabled selected hidden>Select Access Level</option>
<option value="0">Employee</option>
<option value="1">Administrator</option>
<option value="2">Manager</option>
<option value="1">HR</option>
</select>
</div>
<div>
<label>Date</label>
<input type="text" readonly="readonly" name="date" value="<?php echo date("Y/m/d");?>" required>
</div>
<div>
<label>Sick Leave</label>
<input type="text" name="sickleave" required>
</div>
<div>
<label>Vacation Leave</label>
<input type="text" name="vacationleave" required>
</div>
<div>
<label>Picture (Link)</label>
<input type="text" name="picture" value="img/emp/" required>
</div>
<div>
<label></label>
<input type="submit" name="submit" value="Add Account" class="button" style="color: white;" />
<a href="hr_panel.php"><input type="button" value="Back" class="button" style="color: white;" />
</div>
</form>
</div>
</div>
Because you havent put value="" in Option
Try By This
<option value="" disable hidden>Select Gender</option>

PHP - dynamic fields array not

I have a form with dynamic fields. When I add a dynamic field and do a var_dump of that field, I am getting only the first result.
Form:
<div class="form-group halltype">
<label class="col-sm-2 col-sm-2 control-label">HallType</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="title1[]" placeholder="Main Title"><br />
<input type="text" class="form-control" name="title2[]" placeholder="Title 2"><br />
<input type="text" class="form-control" name="seating[]" placeholder="Seating Capacity"><br />
<input type="text" class="form-control" name="floating[]" placeholder="Floating Capacity"><br />
</div>
</div>
<div class="form-group addhalltype">
<label class="col-sm-2 col-sm-2 control-label"></label>
<div class="col-sm-10">
<input type="button" class="btn btn-info" id="">Add Hall Type</button>
</div>
Jquery:
$(".addhalltype").click(function() {
$halltype = '<div class="form-group halltype"><label class="col-sm-2 col-sm-2 control-label">HallType</label><div class="col-sm-10"><input type="text" class="form-control" name="title1[]" placeholder="Main Title"><br /><input type="text" class="form-control" name="title2[]" placeholder="Title 2"><br /><input type="text" class="form-control" name="seating[]" placeholder="Seating Capacity"><br /><input type="text" class="form-control" name="floating[]" placeholder="Floating Capacity"><br /></div></div>';
$($halltype).insertBefore(".addhalltype");
});
and if I do var_dump($_POST['title1']), I get
array(1) {
[0]=>
string(4) "1212"
}
you are printing $_POST['title1'] in var_dump it will show you only value that u inserted for name="title1[]".
Here you are using dynamic array but with different name ie name equal to title1[] title1[] seating[] and floating[].
if you want to get all the post values in one name like title[0],title[1],...title[n] to get the result in this format you have to write your above code with this format.
<input type="text" class="form-control" name="title[]" placeholder="Main Title"><br />
<input type="text" class="form-control" name="title[]" placeholder="Title 2"><br />
<input type="text" class="form-control" name="title[]" placeholder="Seating Capacity"><br />
<input type="text" class="form-control" name="title[]" placeholder="Floating Capacity"><br />

Unable to insert row in table using mysqli (PHP)

I am trying to insert a row in a table but its giving me syntax error.
Here is the HTML Code:
<form method="post" action="" >
<!--form fields-->
<div class="control-group">
<div class="customer_data">
<div class="controls" id="id_milesPerDay">
<label for="ICAO" class="left-text">ICAO:</label>
<input type="text" id="ICAO" style='text-transform:uppercase' maxlength="4" name="ICAO" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="IATA" class="left-text">IATA:</label>
<input type="text" name="IATA" maxlength="3" style='text-transform:uppercase' id="IATA" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="airportname" class="left-text">Airport Name:</label>
<input type="text" name="airport_name" maxlength="" id="Airport_Name" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="coordinates" class="left-text">Coordinates:</label>
<input type="text" name="coordinates" maxlength="" id="Coordinates" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="Time_zone_UTC" class="left-text">Time Zone UTC:</label>
<input type="text" name="Time_zone_UTC" maxlength="3" id="Time_zone_UTC" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="DST" class="left-text">DST:</label>
<input type="checkbox" name="dst" value="other" id="DST" placeholder="" class="input-xlarge">
</div>
<div class="controls text">
<label for="utc" class="left-text">UTC:</label>
<input type="text" name="utc" maxlength="3" id="UTC" placeholder="" class="input-xlarge">
</div>
<div class="controls text">
<label for="From_date" class="left-text">From Date:</label>
<input type="date" name="from_date" maxlength="" id="fromdate" placeholder="" class="input-xlarge">
</div>
<div class="controls text">
<label for="To_date" class="left-text">To Date:</label>
<input type="date" name="to_date" maxlength="" id="todate" placeholder="" class="input-xlarge">
</div>
<!-- Button -->
<div class="control-group">
<div class="controls">
<input type="submit" value="Submit" id="Submit" class="btn btn-success" name="Submit">
</div>
</div>
</div>
<div class="customer_aircraft">
<div class="controls">
<label for="country_code" class="left-text">Country Code:</label>
<input type="text" name="country_code" maxlength="2" style='text-transform:uppercase' id="countrycode" placeholder="" class="input-xlarge" required>
</div>
<div class="controls">
<label for="Remarks" class="left-text">Remarks:</label>
<textarea cols="25" rows="7" name="remarks" id="remarks" placeholder="" class="input-xlarge"></textarea>
</div>
<div class="controls">
<label for="Country" class="left-text">Country: </label>
<input type="text" name="country" maxlength="" id="country" placeholder="" class="input-xlarge" required>
</div>
</div>
</div>
</form>
</body>
</html>
PHP Code:
<?php
$link=require ("Connection.php");
error_reporting(E_ALL); ini_set('display_errors', 1);
if(isset($_POST['Submit']))
{
$ICAO= mysqli_real_escape_string($link,$_POST['ICAO']);
$IATA= mysqli_real_escape_string($link,$_POST['IATA']);
$Airport_Name= mysqli_real_escape_string($link,$_POST['airport_name']);
$coordinates= mysqli_real_escape_string($link,$_POST['coordinates']);
$Time_zone_UTC= mysqli_real_escape_string($link,$_POST['Time_zone_UTC']);
$DST= mysqli_real_escape_string($link,$_POST['dst']);
$UTC= mysqli_real_escape_string($link,$_POST['utc']);
$from_date = mysqli_real_escape_string($link,$_POST['from_date']);
$to_date = mysqli_real_escape_string($link,$_POST['to_date'];
$country_code= mysqli_real_escape_string($link,$_POST['country_code']);
$remarks= mysqli_real_escape_string($link,$_POST['remarks']);
$Country= mysqli_real_escape_string($link,$_POST['country']);
//inserting records//
$sql="INSERT INTO airport_data (ICAO,IATA,Airport_name,Coordinates,Time_zone_utc,DST,UTC,From,To,Country_code,Remarks,Country) VALUES ('$ICAO','$IATA','$Airport_Name','$coordinates','$Time_zone_UTC','$DST','$UTC',STR_TO_DATE('$from_date', '%m/%d/%y'),STR_TO_DATE('$to_date', '%m/%d/%y'),'$country_code','$remarks','$Country')";
if(mysqli_query($link, $sql))
{
echo "Records added successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
}
?>
Here is the error I receive:
ERROR: Could not able to execute INSERT INTO airport_data (ICAO,IATA,Airport_name,Coordinates,Time_zone_utc,DST,UTC,From,To,Country_code,Remarks,Country) VALUES ('oprn','op','islamabad airport','islam','+9','other','+8',STR_TO_DATE('2015-05-26', '%m/%d/%y'),STR_TO_DATE('2015-05-20', '%m/%d/%y'),'op','p','Pakistan'). You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'From,To,Country_code,Remarks,Country) VALUES ('oprn','op','islamabad airport','i' at line 1
Escape (at least) reserved words in column names.
... `From`, `To`, ...
Full list of MySQL reserved words is in http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html.
from and to are reserve keyword. Avoid to use them as column name. You need to change these column name.
You can find more details here
Actually the format of my date variables was wrong so i had to change it like
$from_date = new DateTime($_POST['from_date']);
$to_date = new DateTime($_POST['to_date']);
Insert the following into the database
$from_date = mysqli_real_escape_string($link, $from_date->format('Y-m-d'));
$to_date = mysqli_real_escape_string($link, $to_date->format('Y-m-d'));
And in query
some thing like
,'".$from_date."','".$to_date."',

Categories