I have a large amount of input fields. How can I pass the values into an array so I can pass the array from one page to another?
example:
<input type="text" value=<?php $arry = "compName" ?> placeholder="Company Name" />
Is this legal? How can I do this properly?
EDIT:
In my page, I have "add" and "delete" buttons that will add/delete more input fields. I also have a "preview" button at the bottom. User will add/delete before hitting preview. Preview will collect all input and will call the next page. So the amount of field is unknown.
Here's what my code/markup looks like:
<div class="panel" style="width: 98%; margin:0 auto">
<div class="row">
<div class="large-6 columns">
Advertiser Info: <hr>
<input type="text" value="compName" placeholder="Company Name" />
<input type="text" value= "adEmail" placeholder="Email" />
<input type="text" value="adContName" placeholder="Contact Name" />
<input type="text" value="adPhone" placeholder="Phone # (NO DASHES)" />
<input type="text" value="adStreet" placeholder="Street Address" />
<input type="text" value="adCity" placeholder="City" />
<input type="text" value="adState" placeholder="State/Provice" />
<input type="text" value="adZip" placeholder="Zip/Postal Code" />
<input type="text" value="adCountry" placeholder="Country" />
</div>
<div class="large-6 columns">
Billing Info: <hr>
<input type="text" value="bEmail" placeholder="Email" />
<input type="text" value="bContName" placeholder="Contact Name" />
<input type="text" value="bPhone" placeholder="Phone # (NO DASHES)" />
<input type="text" value="bStreet" placeholder="Street Address" />
<input type="text" value="bCity" placeholder="City" />
<input type="text" value="bState" placeholder="State/Provice" />
<input type="text" value="bZip" placeholder="Zip/Postal Code" />
<input type="text" value="bCountry" placeholder="Country" />
</div>
</div>
</div>
So when the user hits add and EXACT copy of above code will be made.
With that said, I want to be able to use PHP arrays for html values to pass. How can I set my inputs so that I can do this?
You can do some tweaks like:
Using input type elements as array elements.
So that:
<input type="text" name="elem[name]" value="<?php echo $YOUR_VALUE;?>"
<input type="text" name="elem[class]" value="<?php echo $YOUR_VALUE;?>"
<input type="text" name="elem[marks]" value="<?php echo $YOUR_VALUE;?>"
So that the variables are you are posting should be less.
In this case, you are posting only one variable instead of three.
Use Jquery to get all the values of input fields and make a json object of {input_name:value} or array variable of key value and then pass it in argument.
var key_value = {};
$('input').each(function(){
key_value[$(this).attr('name')]= $(this).val();
})
alert(JSON.stringify(key_value));
Now you have an object with field name as key and its value.
Related
I have block:
<div>
<input type="text" name="account[surname][]">
<input type="text" name="account[address][]">
<input type="text" name="account[phone][]">
...
<input type="number" name="account[number][][1]">
<input type="number" name="account[number][][2]">
<input type="number" name="account[number][][3]">
</div>
After submit i get:
{"id":{"13":""},"surname":{"13":"Mike"},"number":{"13":{"1":"623"},"14":{"2":"432"},"15":{"3":"542"}}}
But i need:
{"id":{"13":""},"surname":{"13":"Mike"},"number":{"13":{"1":"623","2":"432","3":"542"}}}
Id - Auto Increment.
Every "account[number][]" create new array. How can I put this data into an one array?
This form is added dynamically (clone).
For static form work this code, but i need dinamic:
<form>
<input type="text" name="account[surname][10]">
<input type="text" name="account[address][10]">
<input type="text" name="account[phone][10]">
...
<input type="number" name="account[number][10][1]">
<input type="number" name="account[number][10][2]">
<input type="number" name="account[number][10][3]">
<button type="submit">Send</button>
</form>
How can I embed http referrer in divi contact forms. I am able send only email, message text fields which are given inbetween %%field id%%. In Divi documentation they given that we need to put field id in between %% like this %%field id%%. But I am unable to add the http referrer or page url. I saw source code like this
"
<label for="et_pb_contact_name_1" class="et_pb_contact_form_label">Name</label>
<input type="text" id="et_pb_contact_name_1" class="input" value="" name="et_pb_contact_name_1" data-required_mark="required" data-field_type="input" data-original_id="name" placeholder="Name">
</p><p class="et_pb_contact_field et_pb_contact_field_1 et_pb_contact_field_last" data-id="email" data-type="email">
<label for="et_pb_contact_email_1" class="et_pb_contact_form_label">Email Address</label>
<input type="text" id="et_pb_contact_email_1" class="input" value="" name="et_pb_contact_email_1" data-required_mark="required" data-field_type="email" data-original_id="email" placeholder="Email Address">
</p><p class="et_pb_contact_field et_pb_contact_field_2 et_pb_contact_field_last" data-id="phone" data-type="input">
<label for="et_pb_contact_phone_1" class="et_pb_contact_form_label">Phone Number</label>
<input type="text" id="et_pb_contact_phone_1" class="input" value="" name="et_pb_contact_phone_1" data-required_mark="required" data-field_type="input" data-original_id="phone" placeholder="Phone Number" pattern="[0-9]*" title="Only numbers allowed.">
</p><p class="et_pb_contact_field et_pb_contact_field_3 et_pb_contact_field_last" data-id="message" data-type="text">
<label for="et_pb_contact_message_1" class="et_pb_contact_form_label">Message</label>
<textarea name="et_pb_contact_message_1" id="et_pb_contact_message_1" class="et_pb_contact_message input" data-required_mark="required" data-field_type="text" data-original_id="message" placeholder="Message"></textarea>
</p><p class="et_pb_contact_field et_pb_contact_field_4 et_pb_contact_field_last" data-id="url" data-type="input">
<label for="et_pb_contact_url_1" class="et_pb_contact_form_label">New Field</label>
<input type="text" id="et_pb_contact_url_1" class="input" value="" name="et_pb_contact_url_1" data-required_mark="not_required" data-field_type="input" data-original_id="url" placeholder="New Field">
</p>
<input type="hidden" value="et_contact_proccess" name="et_pb_contactform_submit_0">
<input type="text" value="" name="et_pb_contactform_validate_0" class="et_pb_contactform_validate_field" />
<div class="et_contact_bottom_container">
<button type="submit" class="et_pb_contact_submit et_pb_button">Send Message</button>
</div>
<input type="hidden" id="_wpnonce-et-pb-contact-form-submitted" name="_wpnonce-et-pb-contact-form-submitted" value="8b651527df" /><input type="hidden" name="_wp_http_referer" value="/data-analytics/test" />
</form>
</div> <!-- .et_pb_contact -->
</div> <!-- .et_pb_contact_form_container -->
Clearly there is hidden field but I dont know how to embed in form. Thanks
You can create new field and set display: none then just copy id of this input field.
Next create below divi code module and paste this
<script> const newInput = document.querySelector('#et_pb_contact_oboz-nazwa_0'); newInput.setAttribute('value', document.title) </script>
Where et_pb_contact_oboz-nazwa_0 - is ID of input field. You have to choose yours
document.title - is a title of page
If you want get url of page change document.title to window.location.href
And of course set this new input field to be not required
I want my hidden input field to get the page title as its value.
This is my testmail.php code.
<p>From: <?php echo $_POST['name']; ?></p>
<p>Subject: <?php echo $_POST['subject']; ?></p>
<p>Email: <?php echo $_POST['email']; ?></p>
<p>Phone Number: <?php echo $_POST['phone']; ?></p>
<p style="width:300px;">Message: <?php echo $_POST['message']; ?></p>
and this is my form code
<form action="testmail.php" method="post" class="cf">
<label for="name">* Name:</label>
<input type="text" name="name" placeholder="Your Name">
<input type="hidden" name="subject" value="<?php value $_POST['pagetitle']; ?>">
<label for="email">* Email:</label>
<input type="text" name="email" placeholder="Enter a valid Email Address">
<label for="phone"> Phone:</label>
<input type="text" name="phone" placeholder="Enter areacode and number">
<label for="message">* Message:</label>
<textarea name="message"></textarea>
<button type="input">Send</button>
</form>
Is there a way to get the title automatically?
Here's a pure javascript way using the onsubmit event.
<form onsubmit="this.subject.value=document.title;">
<input type="text" name="subject" value="" />
<input type="submit" />
</form>
To give you a better understanding of the onsubmit event as the name suggests it executes the contained javascript when the user submits the form. The operation this.subject.value=document.title working from right to left basically says assign the value of document.title to the value attribute of the element with the name of subject in this specific form.
Using your existing form it should look like this (I added the onsubmit event and fixed up errors in your html as well as added the appropriate id's to form elements):
<form action="testmail.php" method="post" class="cf" onsubmit="this.subject.value=document.title;">
<label for="name">* Name:</label>
<input type="text" id="name" name="name" placeholder="Your Name" />
<input type="hidden" name="subject" value="" />
<label for="email">* Email:</label>
<input type="text" id="email" name="email" placeholder="Enter a valid Email Address" />
<label for="phone"> Phone:</label>
<input type="text" id="phone" name="phone" placeholder="Enter areacode and number" />
<label for="message">* Message:</label>
<textarea id="message" name="message"></textarea>
<input type="submit" name="submit" value="submit" />
</form>
This will set the value of your hidden input field once the page has finished loading.
<script>
$(function() {
$('input[name="subject"]').val($('title').text());
});
</script>
You can use JavaScript to do so, assuming you're using jQuery, bind submit event to your form
$('your_form_selector').on('submit', function(){
$('<input />').attr('type', 'hidden')
.attr('name', 'title')
.attr('value', document.title)
.appendTo($(this));
});
I am creating a web page where there are two divs (billing details and shipping details). When the page is loaded, the billing details are automatically displayed and the shipping details remain empty. I have included two radio buttons which allows the user to choose whether or not the shipping details are the same as the billing details. If the user selects yes from the radio buttons then the same details should be displayed in the shipping details.
Note: the details are stored in database are am using php to get the data displayed
At the moment, I have only tried using
<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>
on the first name field just to see if it is working, but i doesnt seem to work.
<div id="leftprofile">
<form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php">
<fieldset class="billing">
<legend>Billing Details</legend><br />
<label for="fname" class="reglabel">First Name:</label>
<input required name="fname" type="text" id="fname" value="<?php echo $fname ?>"/><br />
<label for="lname" class="reglabel">Last Name:</label>
<input required name="lname" type="text" id="lname" value="<?php echo $lname ?>"/><br />
<label for="address" class="reglabel">Address:</label>
<input required name="address" id="address" type="text" value="<?php echo $address ?>"/><br />
<label for="town" class="reglabel">Town:</label>
<input required name="town" id="town" type="text" value="<?php echo $town ?>"/><br />
<label for="postcode" class="reglabel">Post Code:</label>
<input required name="postcode" id="postcode" type="text" value="<?php echo $postcode ?>"/><br />
<label for="phone" class="reglabel">Phone:</label>
<input required name="phone" id="phone" type="text" value="<?php echo $phone ?>"/><br />
<label for="email" id="EmailLabel" class="reglabel">E-mail:</label>
<input required name="email" type="email" id="email" value="<?php echo $email ?>"/><br />
</fieldset>
</form>
</div>
<div id="rightprofile">
<form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php">
<fieldset class="billing">
<legend>Shipping Details</legend><br />
<form>
Same as billing address?
<input type="radio" name="shipping" id="yes" value="yes">Yes
<input type="radio" name="shipping" id="no" value="no">No<br/>
</form>
<label for="fname" class="reglabel">First Name:</label>
<input required name="fname" type="text" id="fname" value="<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>"/><br />
<label for="lname" class="reglabel">Last Name:</label>
<input required name="lname" type="text" id="lname" /><br />
<label for="address" class="reglabel">Address:</label>
<input required name="address" id="address" type="text" /><br />
<label for="town" class="reglabel">Town:</label>
<input required name="town" id="town" type="text" /><br />
<label for="postcode" class="reglabel">Post Code:</label>
<input required name="postcode" id="postcode" type="text" /><br />
<label for="phone" class="reglabel">Phone:</label>
<input required name="phone" id="phone" type="text" /><br />
<label for="email" id="EmailLabel" class="reglabel">E-mail:</label>
<input required name="email" type="email" id="email" /><br />
</fieldset>
</form>
</div>
I have written you some simplified example code. This site contains one form, with two input fields (billing and shipping), and one checkbox to check if the shipping information is the same as the billing information. If the checkbox is checked the code will simply ignore anything typed into 'shipping'.
This would achieve what you are asking for, at least from a PHP perspective. If you are looking more for the "copy the data in those input fields, to those input fields" in real time in the browser, then that is a task for Javascript, and not PHP.
<?php
/* Check if anything was submitted */
if(isset($_POST))
{
/* Retrieve billing information */
$billing_name = $_POST['billing_name'];
$billing_addr = $_POST['billing_addr'];
/* Check if shipping same as billing */
if(isset($_POST['same']))
{
/* Shipping is the same as billing */
$shipping_name = $billing_name;
$shipping_addr = $billing_addr;
}
/* If not, set shipping to the posted value */
else
{
$shipping_name = $_POST['shipping_name'];
$shipping_addr = $_POST['shipping_addr'];
}
$insert = mysql_query(...);
}
?>
<form method="post" action="#" />
Billing information
<label for="billing_name">Name</label>
<input type="text" id="billing_name" name="billing_name" />
<label for="billing_addr">Addr</label>
<input type="text" id="billing_addr" name="billing_addr" />
<label for="same" />Is the shipping information the same as billing information?</label>
<input type="checkbox" id="same" name="same" />
Shipping information
<label for="shipping_name">Name</label>
<input type="text" id="shipping_name" name="shipping_name" />
<label for="shipping_addr">Addr</label>
<input type="text" id="shipping_addr" name="shipping_addr" />
<input type="submit" value="Register" />
</form>
I have this Landing Page that I need to pre-populate with form values after it is submitted. Essentially in the end I have to make the url look like this...
http://example.com/r.php?sid=xx&pub=xxxxx&c1=&c2=&c3=&append=1&firstname=Test&lastname=Smith&address=2211+Commerce+St.&city=Dallas&state=TX&zipcode=75080&email=test#test.com
What I currently have now for the form is...
<form name="regForm" method="get" action="http://example.com/r.php?sid=xx&pub=xxxx&c1=&c2=&c3=&append=1">
<input id="firstname" class="text" type="text" name="firstname"/><br>
<input id="lastname" class="text" type="text" name="lastname" /><br>
<input id="email" class="text" type="text" name="email" /><br>
<input id="address" class="text" type="text" /><br>
<input id="city" class="text" type="text"/><br>
<input id="zipcode" class="text" type="text" maxlength="5" name="zipcode" /><br>
<input type="submit" value="Send Me My FREE List" id="submitBtn2"/>
</form>
How do i create that URL above after the form is submitted? I have been racking my brain all day on this and can't figure it out, i feel like im close.
thanks for the help!
Include the extra parameters as hidden form fields instead of inline query parameters:
<form name="regForm" method="get" action="http://example.com/r.php">
<input type="hidden" name="sid" value="xx" />
<input type="hidden" name="pub" value="xxxx" />
<input type="hidden" name="c1" value="" />
<input type="hidden" name="c2" value="" />
<input type="hidden" name="c3" value="" />
<input type="hidden" name="append" value="1" />
<input id="firstname" class="text" type="text" name="firstname"/><br>
<input id="lastname" class="text" type="text" name="lastname" /><br>
<input id="email" class="text" type="text" name="email" /><br>
<input id="address" class="text" type="text" /><br>
<input id="city" class="text" type="text"/><br>
<input id="zipcode" class="text" type="text" maxlength="5" name="zipcode" /><br>
<input type="submit" value="Send Me My FREE List" id="submitBtn2"/>
</form>
The problem is the input field get variables are causing your url get variables to be truncated, put all your url parameters as hidden values.
<input id="pub" type="hidden" name="pub" value=""/>
<input id="sid" type="hidden" name="sid" value=""/>
I'm assuming that you like the URL that it generates except that you're missing the sid, pub, c1, c2, c3, and append variables. If so, just make hidden inputs like this:
<form id="regForm" ...>
<input id="sid" name="sid" value="" type="hidden" />
<input id="pub" name="pub" value="" type="hidden" />
...
</form>
If you know the values while you're creating the form, then you can do it on the server side. If you don't, then assuming you're using jQuery, you will have to do something like this:
$(function() {
$('#regForm').submit(function () {
$('#sid').val('myNewSidValue');
$('#pub').val('myNewPubValue');
...
});
});