Display form fields using loop in twig - php

I am working on a form that collects number of credit card loans a user has, a person can add as many credit card fields as he desires so the problem I am having is how to render the form fields when a user has multiple entries
Here is the twig dump and as you can see in this example user has 2 entries and I would like to use loop to render these fields.
At present the fields look like this
That's because I am using {{ form_rest(form) }} to dump the fields
While checking the source of the creditcard fields this is what i see
<div>
<label class="required">0</label>
<div id="examplebundle_debt_creditcards_0">
<div>
<label for="examplebundle_debt_creditcards_0_balance" class="required">Balance</label>
<input type="number" id="examplebundle_debt_creditcards_0_balance" name="examplebundle_debt[creditcards][0][balance]" required="required" value="3000">
</div>
<div>
<label for="examplebundle_debt_creditcards_0_amountDue" class="required">Amount due</label>
<input type="number" id="examplebundle_debt_creditcards_0_amountDue" name="examplebundle_debt[creditcards][0][amountDue]" required="required" value="300">
</div>
<div>
<label for="examplebundle_debt_creditcards_0_interestRate" class="required">Interest rate</label>
<input type="number" id="examplebundle_debt_creditcards_0_interestRate" name="examplebundle_debt[creditcards][0][interestRate]" required="required" value="3">
</div>
</div>
</div>
So my question is, how can i use a loop to display the correct number of creditcard related fields dynamically?

Related

How show a automatic message after submit a form

I created a contact-form with username, email and a message.
Here is my code;
<form action="thanks.php" method="post">
<div>
<label for="nom">Nom :</label>
<input type="text" id="nom" name="user_name">
</div>
<div>
<label for="courriel">Courriel :</label>
<input type="email" id="courriel" name="user_email">
</div>
<div>
<label for="phone">Numéro de téléphone</label>
<input type="text" id="phone" name="phone_number">
</div>
<div>
<label for="message">Message :</label>
<textarea id="message" name="user_message"></textarea>
</div>
<div class="button">
<button type="submit">Envoyer votre message</button>
</div>
</form>
and I am looking for a solution after submitted this form, to make show a message with username, email and user's message.
something like;
"hi {username}, thank you for your message. we'll contact you by your {email} after reviewed your message"
I would like to know how I can make a new php page to show these details.
You would show the message on the page that processes your form request.
So if you form goes to processForm.php then when processForm.php is done processing the form it can then return whatever HTML you want it with access to the form values passed to it.
This is not really ideal and is generally handled via the page JS/HTML but if you have specific limits that require this then that is how you do it.
As for a specific example there are many on google for "php form processing"

Laravel Bulk Insert

I am trying to find solution to below use case, using Laravel I am inserting bulk products with respective quantity and description. For example product1 with id:pro_1 with its quantity and description, same with other products. If I submit the form how I know for which product the quantity and description is.
#foreach(get_products_by_parent_name("Content") as $product_id => $product_name)
<div class="col-md-4">
<div class="form-group">
<h4><label class="p_name"><input type="checkbox" value="{{ $product_id }}"> {{ $product_name}} </label></h4>
<label>Quantity</label>
<input type="number" min="1" class="form-control"
name="qn_{{$product_id}}">
<label>Description</label>
<textarea class="form-control" name="desp_{{$product_id}}" style="resize: none;"></textarea>
</div>
</div>
#endforeach
Instead of giving all your field names a product ID, you could make them arrays (for example: desp_{{$product_id}} becomes desp[{{$product_id}}]). Looping through the results, you could create a new model with the data given.

Symfony2, Twig and Checkboxes custom twig template

I am using bootstrap 3 for my template and I am aware of the templating that exist within the Symfony Twig Bridge. Below is what I would like to have in the end.
Where I have a grey area is where I would like to have my label at 90% alignment.
Below is what I currently have:
<div class="form-group">
<div id="campaign_channel" class="col-xs-6">
<input id="campaign_channel_1" type="checkbox" value="1" name="campaign[channel][]">
<label for="campaign_channel_1">Facebook</label>
<input id="campaign_channel_2" type="checkbox" value="2" name="campaign[channel][]">
<label for="campaign_channel_2">Twiiter</label>
<input id="campaign_channel_3" type="checkbox" value="3" name="campaign[channel][]">
<label for="campaign_channel_3">LinkedIn</label>
<input id="campaign_channel_4" type="checkbox" value="4" name="campaign[channel][]">
<label for="campaign_channel_4">Instagram</label>
<input id="campaign_channel_5" type="checkbox" value="5" name="campaign[channel][]">
<label for="campaign_channel_5">Youtube</label>
</div>
</div>
Below is what I would like to have:
<div class="form-group">
<div id="campaign_channel" class="checkbox col-xs-6">
<div class="checkbox-label">Channel(s)</div>
<div class="checkbox-clmn-1">
</div>
<div class="checkbox-clmn-2">
</div>
</div>
</div>
If, as shown in the example, we have 5 checkboxes I want to split any number of checkboxes by using the multiples of three. Say have the first three checkboxes in the first column in a case where total number is odd and then if even then checkboxes should be spraed equally in both checkbox columns.
Where exactly can I create this custom template "fields.html.twig" in my Symfony application so that I can actually play around with this idea?
There is fully explained:
http://symfony.com/doc/current/cookbook/form/form_customization.html#form-theming-in-twig
You can create your own widget rendering (for ex. choice_widget) in separate file or inside file where your form is rendering

PHP MySQL submitting dynamic fields, arrays in $_REQUEST

I've got a form that has users fill out information for request. In a section of the form, I have an "Add More Phone" button that dynamically adds more text field and drowdown selections so users can add multiple types of phone numbers. Since the number of telephones that will be submitted is unknown, I am trying to figure out how to send all the data into the server so it can be inserted into the database.
Here's my html:
<div class="form-group">
<label class="col-md-4">Tel:</label>
<div class="col-md-8">
<input class="form-control required" name="phones[]" maxlength="14" type="text" placeholder="(888) 888-8888">
</div>
</div>
<div class="form-group">
<label class="col-md-4">Phone Type:</label>
<div class="col-md-8">
<select class="form-control" name="phonetypes[]">
<option value="0"><b>Choose a phone type</option>
<option value="1">Main</option>
<option value="2">Fax</option>
<option value="3">Mobile/Direct</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4"></label>
<div class="col-md-8">
<input onclick="addRow(this.form);" type="button" value="Add Another Phone" name="addphone" class="btn btn-info"></input>
</div>
</div>
The addRow(this.form) appends a new set of fields where you can enter more phone number and the phone types. This is where I'm not sure how the request is going to take the field values since we don't know how many phones will be added.
I have the request as:
if(isset($_REQUEST['action'])) {
$phones[] = $_REQUEST['phone'];
foreach($_REQUEST['phone'] as $phone) {
$phone['phone'];
}
$phonetypes[] = $_REQUEST['phonetype'];
foreach($_REQUEST['phonetype'] as $phonetype) {
$phone['phonetype'];
}
I definitely have no clue on where to go about this from here. Any help appreciated!
Every request parameter can only be sent once with a given name, if it is not an array. So you need to adapt your code to
either use a different name for each phonetype and phone
or use a name as phonetype[] to mark is as an array

send completed form to email using this form

I was hoping someone could let me know the php code to send the contents of this form through to my email.
Thanks!
I really have absolutely no idea when it comes to php so any help that you could provide would be very much appreciated!
Get in touch to discuss your project!
<input id="af-showreq" class="af-show-input" type="checkbox" name="showreq" />
<form class="af-form" id="af-form" novalidate>
<div class="af-outer">
<div class="af-inner">
<label for="input-title">Title</label>
<input type="text" name="title" id="input-title">
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="input-name">Name</label>
<input type="text" name="fullname" id="input-name" required>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="input-email">Email address</label>
<input type="email" name="email_address" id="input-email" required>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="input-country">Company</label>
<input type="email" name="country" id="input-country" required>
</div>
</div>
<div class="af-outer">
<div class="af-inner">
<label for="input-phone">Phone Number</label>
<input type="email" name="phonenumber" id="input-phone">
</div>
</div>
<input type="submit" value="Send it over!" />
</form>
</section>
</div>
</div>
first of all we want to turn the form into a post request. So when someone hits the submit button it will post the form data to your server. You can do this by changing your form tag to have a action (the page on your site you want to post back to") and method="POST".
<form class="af-form" action="" id="af-form" method="POST">
For more information on that look here.
http://www.w3schools.com/html/html_forms.asp
For post to work you need to make sure all your tags that you want to send over have a name tag. Currently your submit button doesn't change it to:
<input type="submit" name ="submit" value="Send it over!" />
Next on the page the post data gets sent to we want to make sure that we have post data. In php all post data is stored in a super variable called POST. So we can check if there is data in here. If there is then we can continue into making the email
if(isset($_POST['submit']){
//the rest of the code should be done in here
//as we only want to send the email if we have made a post request
}
Finally we can now take this information to send you an email
we can get input information by doing the following
$example = $_POST['email_address']
example will now have the information that people put in the email_address input box
sending an email in php is done with the mail() function. An example of this can be seen below
mail("whoto#email.com", "I am a subject", "I am the content of the email);
for more information on the mail function look at http://php.net/manual/en/function.mail.php

Categories