PHP MySQL submitting dynamic fields, arrays in $_REQUEST - php

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

Related

Refresh the page with new request on change select option

I am trying to achieve something via php.
Here there are couple of options.
<select class="custom-select" id="inputGroupSelect01">
<option selected="">Select Clusters ...</option>
<option value="math-related">Math Related</option>
<option value="science-related">Science Related</option>
</select>
There are Group of subject fields which will appear when the options are changed respectively.
<div class="form-row pb-5" id="math-related"">
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num1" placeholder="English">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num2" placeholder="Math">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num3" placeholder="Science">
</div>
<div class="form-group col-md-3">
<input type="number" class="form-control" id="num4" placeholder="Computer">
</div>
</div>
When the option is changed to Math related OR Science Related option, the related 5-6 subject fields will be populated like image below. Some might have 5, some might have 8.
How to refresh the page and load the new sets of input fields according to selection done via PHP?
Example Image for fields
First of all change your select dropdown with your page url + parameter and make sure to replace YOUR_PAGE_URL with actual url as follows -
<select onChange="window.location.href=this.value" class="custom-select" id="inputGroupSelect01">
<option value="">Select Clusters ...</option>
<option value="YOUR_PAGE_URL?topic=math-related">Math Related</option>
<option value="YOUR_PAGE_URL?topic=science-related">Science Related</option>
</select>
After then just do a GET parameter checking on your page to populate your fields like -
if( issset($_GET['topic']) ){
if( $_GET['topic'] == 'math-related' ){
echo "Add your math related above 5-8 fields html here.";
}elseif( $_GET['topic'] == 'science-related' ){
echo "Add your science related above 5-8 fields html here.";
}
}

Display Dropdown in HTML with PHP

I am android developer and making one admin panel for one of my android application. I know very basic PHP. currently I am displaying input field with value and user can change with text input and can save value with submit button. Its like below
<div class="form-group">
<label class="col-md-3 control-label">Admob On/off :-</label>
<div class="col-md-6">
<input type="text" name="ads_status" id="ads_status" value="<?php echo $settings_row['ads_status'];?>" class="form-control">
</div>
</div>
Instead of input value I want show dropdown menu with two item called on and off. When user load page it should show value based on previous save. Like if user have made setting on it must display on. I am displaying value now from database. Let me know if anyone can suggest me what Should I do for it. Thanks
<div class="form-group">
<label class="col-md-3 control-label">Admob On/off :-</label>
<div class="col-md-6">
<select class="form-control " type="text" name="ads_status" >
<option value="on" <?php if($settings_row['ads_status']== 'on') echo "selected = 'selected'" ?> >On</option>
<option value="off" <?php if($settings_row['ads_status'] == 'off') echo "selected = 'selected'" ?> >Off</option>
</select>
</div>
</div>
I hope this will hep you.

Why do I keep getting Notice: Array to string conversion?

I am trying to update a mysql database but every time I get the Notice: Array to string conversion. I really can't figure out what I'm doing wrong here. Can someone kindly help?
My form
<div class="panel-body">
<form role="form" method="post" action="client_post.php" class="form-horizontal">
<fieldset>
<div class="form-group">
<label class="col-sm-3 control-label">Hospital No:
</label>
<div class="col-sm-5">
<input required class="form-control" name="Hospital_no" placeholder="Patients' Hospital Number">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Date of New Status</label>
<div class="form-group">
<label class="col-sm-3 control-label">New Status</label>
<div class="col-sm-5">
<select required name ="art_status" class="form-control">
<option></option>
<option value = "ART Restart" name="ART Restart">ART Restart</option>
<option value = "ART Transfer Out" name="ART Transfer Out" >ART Transfer Out</option>
<option value = "Pre ART Transfer Out" name="Pre ART Transfer Out">Pre ART Transfer Out</option>
<option value = "Lost to Followup" name="Lost to Followup">Lost to Followup</option>
<option value = "Stopped Treatment" name="Stopped Treatment">Stopped Treatment</option>
<option value = "Known Death" name="Known Death">Known Death</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<input class="btn btn-primary" type="submit" value="Update" name="submit" >
</div>
</div>
</fieldset>
</form>
I'm trying to make the update from this page (client.php)
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
$Hosp_no = ['Hospital_no'];
$art_status = ['art_status'];
$art_date = ['art_start_date'];
$update_client = "UPDATE `pat_reg` SET `art_status` = '$art_status', `art_start_date` = '$art_date' WHERE `Hospital_no` = '$Hosp_no'";
if (mysqli_query($dbcon,$update_client))
{
echo"<script>alert('Record Updated')</script>";
//echo"<script>window.open('client_update.php','_self')</script>";
}
$dbcon->close();
}
Please help.
Using the $_POST superglobal allows you to access the values submitted in the form.
Change the following:
$Hosp_no = ['Hospital_no'];
$art_status = ['art_status'];
$art_date = ['art_start_date'];
to this:
$Hosp_no = $_POST['Hospital_no'];
$art_status = $_POST['art_status'];
$art_date = $_POST['art_start_date'];
PS. You should be escaping the user's input before querying the database (or, ideally, use prepared statements) or you may find yourself being hacked very quickly if released to the public.

Display form fields using loop in twig

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?

Access HTML form data in Symfony2

I am new to Symfony. I have seen a lot of threads regarding this topic, but none have been able to answer the question that I have.
I have a "Contact" form on my site. This contact form submits to path('submit_query'), which calls the submitQueryController. I am not building the form through Symfony and I am not using an object or entity. My problem is that no matter what I do, I am unable to access the form data within the controller. I have tried every thread suggestion that I have seen, and I either get the REQUEST object with a whole bunch of data (none of which is my form data) or I get nothing.
Is there no easy way of accessing the posted form data from within the controller?
My HTML Form:
<form id="contact_form" role="form" action="{{ path('submit_query') }}" method="post">`
<div class="panel-body">
<fieldset>
<div class="form-group">
<label for="name" class="control-label">Name</label>
<input type="text" class="form-control" id="name" placeholder="Full Name" data-validation-error-msg="Please enter your full name" data-validation="length" data-validation-length="min1">
</div>
<div class="form-group">
<label for="email" class="control-label">Email Address</label>
<input type="email" class="col-sm-3 form-control" id="email" placeholder="Email Address" data-validation-error-msg="Please enter a valid email address" data-validation="email length" data-validation-length="min1">
</div>
<div class="form-group">
<label for="number" class="control-label">Contact Number</label>
<input type="text" class="form-control" id="number" placeholder="Contact Telephone Number">
</div>
<div class="form-group">
<label for="subject" class="control-label">Subject</label>
<input type="text" class="form-control" id="subject" placeholder="The subject of your query" data-validation-error-msg="Please enter a subject for your query" data-validation="length" data-validation-length="min1">
</div>
<div class="form-group">
<label for="query" class="control-label">Query</label>
<textarea class="form-control" id="query" rows="5" placeholder="Please enter a detailed description of your query" data-validation-error-msg="Please enter your query description" data-validation="length" data-validation-length="min1"></textarea>
</div>
</fieldset>
</div>
<div class="panel-footer clearfix text-center"><button type="submit" class="btn btn-default">Submit Query</button></div>
</form>
Controller:
When I try:
public function submitQueryAction(Request $request)
{
$data = $request->request->all();
die(var_dump($data));
}
I get an empty array in "$data".
When I try:
public function submitQueryAction()
{
$data = $this->getRequest()->request->all();
die(var_dump($data));
}
I get a vardump of a Request object, but none of the data is mine. I have also tried the solution presented by Access POST values in Symfony2 request object for getting post values without using an object or entity, but I get an error "Call to undefined method Symfony\Component\Form\Form::bindRequest()".
PLEASE HELP.
Thank you in advance.
You are missing the name html attribute for your inputs.
If an element of the form misses this attribute, then its data will not be sent.
From w3.org :
Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.

Categories