PHP - dynamic fields array not - php

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 />

Related

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>

How to make two rows insert with one button Laravel using ajax

i want to make two inserts in same table.
The table is based on this fields (locale, project_id(foreign key), title, caption).
And the controller looks like here:
public function storeTranslation(Request $request)
{
$projecttranslation = new ProjectTranslation();
$projecttranslation->locale = $request->input("locale");
$projecttranslation->project_id = $request->input("project");
$projecttranslation->title = $request->input("title");
$projecttranslation->caption = $request->input("caption");
$projecttranslation->save();
}
The form for the moment looks like here:
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="locale" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
<form enctype="multipart/form-data" id="myFormTraduccion2" name="myFormTraduccion2"> <!--SECOND FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="title">Language:</label>
<input type="text" id="locale" name="locale" value="es" disabled class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit2" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!--SECOND FORM TO TRANSLATE END HERE -->
</div>
</div>
And the ajax look like this:
$("#createtranslatesubmit").click(function(){
$("#myFormTraduccion").submit();
});
$("#myFormTraduccion").submit(function(e){
e.preventDefault();
$.ajax({
url:'/admin/projects/postUploadTranslation',
type:'POST',
data:$('#myFormTraduccion').serializeArray(),
success: function(){
$("#form2").fadeOut(1000);
$("#form3").fadeIn(2000);
}
});
});
This create only with the first form, the first translation.
I think i should change the view code to this (Two same inputs for each field of database):
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="locale" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale" name="locale" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
</div>
</div>
That's correct? The problem to "store" the data, i think will be a foreach in controller.
And finally, i don't have any idea, how to pass the data in the ajax, with a formdata maybe?
Thanks a lot, any help will be appreciated!
When you submit a form, you send to the server the data of that specific form. Your approach of using multiple forms doesn't work here, because you want to send all data with only 1 specific form submit.
So you have to only create 1 form and separate the different translations with a numeric reference.
Your HTML (note the -0 and -1 used to separate id and name of each input element) :
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale-0" name="locale-0" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project-0" name="project-0" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title-0" name="title-0" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption-0" name="caption-0" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale-1" name="locale-1" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project-1" name="project-1" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title-1" name="title-1" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption-1" name="caption-1" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form>
The controller:
public function storeTranslation(Request $request)
{
$projecttranslation0 = new ProjectTranslation();
$projecttranslation0->locale = $request->input("locale-0");
$projecttranslation0->project_id = $request->input("project-0");
$projecttranslation0->title = $request->input("title-0");
$projecttranslation0->caption = $request->input("caption-0");
$projecttranslation0->save();
$projecttranslation1 = new ProjectTranslation();
$projecttranslation1->locale = $request->input("locale-1");
$projecttranslation1->project_id = $request->input("project-1");
$projecttranslation1->title = $request->input("title-1");
$projecttranslation1->caption = $request->input("caption-1");
$projecttranslation1->save();
}
Of course, it can be easily generalized for N multiple transations and not only 2.
try this out:
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="ProjectTranslation[0][locale]" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="ProjectTranslation[0][project]" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="ProjectTranslation[0][title]" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="ProjectTranslation[0][caption]" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale" name="ProjectTranslation[1][locale]" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="ProjectTranslation[1][project]" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="ProjectTranslation[1][title]" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="ProjectTranslation[1][caption]" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
</div>
So you will get array of ProjectTranslation at controller side
Now at controller side
public function storeTranslation(Request $request)
{
$form_data = $request->get('ProjectTranslation');
foreach($form_data as $form){
$projecttranslation = ProjectTranslation::create($form);
$projecttranslation->save();
}
}

loop to reduce writing many form containers

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
}

Text in first field shows as label for second field

How do we make a text field sync with another text field i.e Adding text in first field shows as label for another text field.Basically i have made a currency exchange skeleton.
<form method='POST' action='index.php'>
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
<input type="text" name="from" value="" placeholder="From Which Currency?" tabindex="1" />
<input type="hidden" name="owner" value="<?php echo $username; ?>" />
</div>
<div class="6u 12u(mobilep)">
<input type="text" name="to" value="" placeholder="To Which Currency?" tabindex="2" />
</div>
</div>
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
<input type="text" name="limit" value="" placeholder="How much volume?" tabindex="3" />
</div>
<div class="6u 12u(mobilep)">
<input type="text" name="fee" value="" placeholder="Your Fee or put Zero(Don't add %age sign)" tabindex="4" />
</div>
<div class="6u 12u(mobilep)">
<input type="text" name="price" value="" placeholder="Your Rate ( Don't add symbols)" tabindex="4" />
</div>
</div>
This is the result i currently see with this code: http://i.imgur.com/dWD3Nu5.png
But i want to make it like this: http://i.imgur.com/34BU8g3.png
Add text of your textbox to label on the keyUp event of that textbox itself.

HTML - form acting weird. Not redirecting to URL and using GET instead of POST

I'm a CS student and I have a DB project due in less than 24hrs! This is really annoying because I just need to forms to access my DB. Anyway, I have this form that works perfectly, while the second form does not work. Instead of posting and directing to the correct URL the second form re-loads the current page with the variables in the URL. Anybody have any ideas?
<form role="form" method="post" action="../controller/AddPerson.php">
<div class="box-body">
<div class="form-group">
<label for="newReservationFirstName"> First name</label>
<input type="text" class="form-control" name="newReservationFirstName" placeholder="Enter first name">
<label for="newReservationLastName"> Last name</label>
<input type="text" class="form-control" name="newReservationLastName" placeholder="Enter last name">
<label for="newReservationPhoneNumber"> Phone Number</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-phone"></i>
</div>
<input type="text" class="form-control" name="newReservationPhoneNum" data-inputmask='"mask": "(999) 999-9999"' data-mask/>
</div><!-- /.input group -->
<label for="newReservationStreetAddress"> Street Address</label>
<input type="text" class="form-control" name="newReservationStreetAddress" placeholder="Enter street address">
<label for="newReservationCity"> City</label>
<input type="text" class="form-control" name="newReservationCity" placeholder="Enter city">
<label for="newReservationState"> State</label>
<select class="form-control" name="newReservationState">
<?php
$result = getTableOrderBy('States','stateName');
while($row = mysql_fetch_array($result)) {
echo "<option value=".$row[stateAbbr].">".$row[stateName]."</option>";
} ?>
</select>
<label for="newReservationZip"> Zip Code</label>
<input type="text" class="form-control" name="newReservationZip" placeholder="Enter zipcode">
</div>
<button type="submit" class="btn btn-success btn-lg">Add New Customer</button>
</div>
</form>
This is the form that doesn't work correctly, both pages exist on the server:
<form role="form" method="post" action="../controller/AddEmployee.php">
<div class="box-body">
<div class="form-group">
<label for="newEmployeeFirstName"> First name</label>
<input type="text" class="form-control" name="newEmployeeFirstName" placeholder="Enter first name">
<label for="newEmployeeLastName"> Last name</label>
<input type="text" class="form-control" name="newEmployeeLastName" placeholder="Enter last name">
<label for="newEmployeePhoneNumber"> Phone Number</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-phone"></i>
</div>
<input type="text" class="form-control" name="newEmployeePhoneNum" data-inputmask='"mask": "(999) 999-9999"' data-mask/>
</div><!-- /.input group -->
<label for="newEmployeeStreetAddress"> Street Address</label>
<input type="text" class="form-control" name="newEmployeeStreetAddress" placeholder="Enter street address">
<label for="newEmployeeCity"> City</label>
<input type="text" class="form-control" name="newEmployeeCity" placeholder="Enter city">
<label for="newEmployeeState"> State</label>
<select class="form-control" name="newEmployeeState">
<?php
$result = getTableOrderBy('States','stateName');
while($row = mysql_fetch_array($result)) {
echo "<option value=".$row[stateAbbr].">".$row[stateName]."</option>";
} ?>
</select>
<label for="newEmployeeZip"> Zip Code</label>
<input type="text" class="form-control" name="newEmployeeZip" placeholder="Enter zipcode">
<p></p>
<p></p>
<label for="newEmployeeFirstName"> Account Username</label>
<input type="text" class="form-control" name="newEmployeeUsername" placeholder="Enter username">
<label for="newEmployeeLastName"> Account Password</label>
<input type="text" class="form-control" name="newEmployeePassword" placeholder="Enter password">
<label for="newEmployeePhoneNumber"> Social Security Number</label>
<input type="text" class="form-control" name="newEmployeeSocial" placeholder="Enter SSN">
<div class="form-group" name="newEmployeePrivileges">
<br>
Privileges :
<select name="newEmployeePrivileges">
<option value="admin">Admin</option>
<option value="admin">Non-Admin</option>
</select>
</div>
<button type="submit" class="btn btn-success btn-lg">Add New Employee</button>
</div>
</div>
</form>
----------------------------------EDIT ----------------------------------------------
I tried making a another really simple form on some extra space and it still didn't work. I have no idea what could be cause it to do this.
<form method="post" action="post" action="../controller/AddEmployee.php">
<button type="submit" class="btn btn-success btn-lg">Add New Employee</button>
</form>
It could be that the button tag you are using to submit the form is causing it behave strangely. Try swapping out the button tag for an input. So:
<form method="post" enctype="multipart/form-data" action="../controller/AddEmployee.php">
<input type="submit" class="btn btn-success btn-lg" name="submit" >Add New Employee</input>
</form>
Also, I noticed you've included two 'action' attributes in your example form :-)

Categories