Adding Dynamic Field to Forms (Chronoform) - php

I am using the popular ChoronoForm plugin for Joomla. I have created a form which contain dynamic fields. A javascript function is used to add multiple "items". A User clicks on the button "Add another item" and the item field is redisplayed for them to insert additional "items". Similar to the form on this website http://www.unclesmoney.co.uk/home3.php (Fill in the Item Details Section and click on "add another item"). When the form is submited the information is sent via email. The problem, only the item that was inserted last is sent and other itens are ignored (not sent) e.g. if I fill out the form with 2 or more items only he detail of the last item that was filled out is sent. Assume I have to write some php or javascript to pass these values to the html template or is there a more simple way? either way I can do with some pointers on how to do acheive this. There is a similar thread on this forum which was a great help but it doesnt show how to modify the html template. Thanks
<div class="form_element cf_dropdown">
<label class="cf_label" style="width: 150px;">Item Type:</label>
<select class="cf_inputbox validate-selection" id="select_19" size="1" title="" name="select_19">
<option value="">Choose Option</option>
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_textarea">
<label class="cf_label" style="width: 150px;">Detail</label>
<textarea class="cf_inputbox required" rows="3" id="text_2" title="Please enter Item Detail" cols="30" name="text_2"></textarea>
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_fileupload">
<label class="cf_label" style="width: 150px;">File</label>
<input class="cf_fileinput cf_inputbox" title="" size="20" id="file_3" name="file_3" type="file" />
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label" style="width: 150px;">File 2</label>
<input class="cf_inputbox required validate-number" maxlength="150" size="30" title="Must contain a value" id="text_4" name="text_4" type="text" />
</div>
<div class="cfclear"> </div>
</div>
</div>
<span id="writeroot"></span>
<input type="button" value="Add Another Item" onclick="moreItems()" />
.....
Javascript
var counter = 0;
function moreItems() {
counter++;
var newFields = document.getElementById('readRoot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}

Chronoforms does a simple replacement in the email template to fill in the form values. Unless you've some code to manipulate that, it is only expecting a single value. What you need to do is add some PHP in the "On Submit Code - before sending email" area in the Form Code tab. A quick loop through the all of the extra fields to combine them in to the single variable name that Chronoforms is expecting should fix your problem.

Related

Handling data from select option form in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have basic login form :
....
<input type="text" required autocomplete="off" name="user_name"/>
<input type="password" required autocomplete="off" name="password"/>
....
But I want user choose username from section option instead of input username from keyboard. So I make a select option form
<div class="col-md-2">
<select name="Grade" id="Grade"
data-live-search="true" class="chosen selectpicker form-control" required>
<option value="">Select Grade</option>
</select>
</div>
<div class="col-md-1 col-sm-12" id="lable1"><id="lable1">user</div>
<div class="col-md-2">
<!--select username-->
<select class="selectpicker form-control" name="user" id="user" autofocus="autofocus" required>
<option value="">Select an Option</option>
</select>
<!--select username-->
</div>
How can I hanlde data from select option form instead of input username to
<input type="text" required autocomplete="off" name="user_name"/>
Accessing HTML <form> elements in PHP
All HTML elements in a HTML form can access by PHP with their name with these variables:
$_POST['NAME'] or $_GET['NAME']
GET or POST depends on your form action.
And the value of these Variables are the value attribute of the HTML element
As described in form.guide Handling select box (drop-down list) in a PHP form
This is an example for select element
HTML :
<p>
What is your Gender?
<select name="formGender">
<option value="">Select...</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</p>
PHP :
<?php
if(isset($_POST['formSubmit']) ) //IF FORM SUBMIT IS PRESSED
{
$varMovie = $_POST['formMovie']; // Other form inputs
$varName = $_POST['formName']; // Other form inputs
$varGender = $_POST['formGender']; //Our Select element
$errorMessage = "";
// - - - snip - - -
}
?>
In your example :
HTML :
<!--select username-->
<select class="selectpicker form-control" name="user" id="user" autofocus="autofocus" required>
<option value="user1">USER ONE</option>
<option value="user2">USER TWO</option>
</select>
<!--select username-->
And you can use in your PHP:
if(isset($_POST['user']) ){
$name=$_POST['user']; //user1 if USER ONE selected or user2 if second one...
}

Select html tag default option tag selecting error print

I need when user not select any database value option tag print error color in to parent div. I used that code for <input type="text"> html tag code to print text field pattern not matching time printing error. code is below
<label class="col-md-3 control-label">Product Title</label>
<div class="col-md-6">
<input type="text" name="product_title" class="form-control" required
placeholder="Product Name" value="" pattern="([A-z0-9À-ž\s]){2,}"
oninvalid="setCustomValidity('Please enter least two characters long name')"
onblur="this.parentNode.parentNode.style.backgroundColor=/^([A-z0-9À-ž\s]){2,}/.test
(this.value)?'inherit':'orange'" >
</div>
That is done for me onblur event.
Then now i want print error color when user not select any database value option in <select></select>tag. this is the default value i given. <option>Select a Manufacturer</option> I need print error when user select this or forget select database values in that <option> tag.
This is the code section
<div class="form-group"> <!-- form-group Start -->
<label class="col-md-3 control-label"> Select a Manufacturer </label>
<div class="col-md-6">
<select class="form-control" name="manufacturer" required="required">
<option>Select a Manufacturer</option>
<?php
$get_manufacturer ="select * from manufactures";
$run_manufacturer = mysqli_query($con, $get_manufacturer);
while($row_manufacturer = mysqli_fetch_array($run_manufacturer)){
$manufacturer_id = $row_manufacturer['manufacturer_id'];
$manufacturer_title = $row_manufacturer['manufacturer_title'];
echo "<option value='$manufacturer_id'>$manufacturer_title</option>";
}
?>
</select>
</div>
How can i do this?
Just do<option value="">Select a Manufacturer</option>.
Please see next link for reference: https://www.w3schools.com/tags/att_select_required.asp
I use J-Query here my code
<select class="form-control" name="manufacturer" required="required" onchange="getval(this);">
<option value="0">Select a Manufacturer</option>
Then i used this script to change background
<script type="text/javascript">
function getval(sel)
{
if(sel.value == "0"){
$("#sel").css("background-color", "orange");
}
}
</script>
Finally i fixed it

Can I send data form cordova app form to joomla web form?

Is it possible to send data from a Cordova to a Joomla web form and then submit the form?
I cannot load the form in to the app and send because of " invalid token " issue.
I need help with the javascript code, the solution I have below is something is not complete but I think I'm going in the right direction.
<script>
function UpdateRecord() {
var name = $("[name='name']").val();
var subject = $("[name='subject']").val();
var select-choice-1']").val() = $("[name='select-choice-1']").val();
var textarea = $("[name='textarea']").val();
var file = $("[name='file']").val();
jQuery.ajax({
type: "POST",
url: "http://website.com/send-form",
data: "name="+ name+"& subject="+ subject+"& select-choice-1="+ select-choice-1+" & textarea="+ textarea+"+" & file="+ file+",
dataType: "html",
cache: false,
success: function(response) {
alert("Email Sent");
}
});
}
</script>
end of javascript
// form on app
<form>
<div class="ui-field-contain">
<label for="text-basic">Text input:</label>
<input type="text" name="name" id="text-basic" value="">
</div>
<div class="ui-field-contain">
<label for="text-basic">Text input:</label>
<input type="text" name="subject" id="text-basic" value="">
</div>
<div class="ui-field-contain">
<label for="select-choice-1" class="select">Custom select:</label>
<div class="ui-select">
<a href="#" role="button" id="select-choice-1-button" aria-haspopup="true" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow ui-li-has-count">
<span> </span>
<span class="ui-li-count ui-body-inherit" style="display: none;">0</span>
</a>
<select name="select-choice-1" id="select-choice-1" data-native-menu="false" multiple="multiple" tabindex="-1">
<option value="16">Item 1</option>
<option value="15">Item 2</option>
<option value="14">Item 3</option>
<option value="13">Item 4</option>
<option value="12">Item 5</option>
<option value="9">Item 6</option>
<option value="8">Item 7</option>
</select>
<div style="display: none;" id="select-choice-1-listbox-placeholder"><!-- placeholder for select-choice-1-listbox --></div>
</div>
</div>
<div class="ui-field-contain">
<label for="textarea">Textarea:</label>
<textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
</div>
<div class="ui-field-contain">
<label for="file">File:</label>
<input type="file" name="file" id="file" value="">
</div>
<div class="ui-field-contain">
<label for="submit-1">Send form:</label>
<div class="ui-btn ui-input-btn ui-corner-all ui-shadow">Send<input type="submit" id="submit-1" onclick="UpdateRecord()" value="Send"></div>
</div>
</form>
the form in the app will post to the form on the website
// joomla web form
<form id="item-submission" action="/submit-a-request/submission/submission/submit-a-request/maintenance-request/a2db427bfdc794d3695d8ac6843cb04d" method="post" name="submissionForm" accept-charset="utf-8" enctype="multipart/form-data">
<input type="text" name="elements[0c7f7be5-059b-4cd3-8641-d88f4199af01][0][value]" value="" size="60" maxlength="255">
<input type="text" name="elements[bc797942-f95f-438e-ad05-e44b2199e9a8][0][value]" value="" size="60" maxlength="255">
<select id="elements_itemcategoryvalue" name="elements[_itemcategory][value][]" title="Category" size="7">
<option value="16">Item 1</option>
<option value="15">Item 2</option>
<option value="14">Item 3</option>
<option value="13">Item 4</option>
<option value="12">Item 5</option>
<option value="9">Item 6</option>
<option value="8">Item 7</option>
</select>
<textarea name="elements[169f7b63-e778-4a1b-ac22-654c2e248bca][0][value]" cols="60" rows="20"> Description</textarea>
<textarea name="elements[72d27f11-f7b4-4d8e-b076-5c35f8e896de][0][value]" cols="60" rows="20"></textarea>
<input type="text" id="filename054c769a-f6d0-4db0-b784-129f9f0d4d78" readonly="readonly">
<input type="file" name="elements_054c769a-f6d0-4db0-b784-129f9f0d4d78" onchange="javascript: document.getElementById('filename054c769a-f6d0-4db0-b784-129f9f0d4d78').value = this.value.replace(/^.*[\/\\]/g, '');">
<div class="submit">
<button type="submit" id="submit-button" class="button-green">Submit Item</button>
</div>
</form>
Yes, this is certainly possible. Cordova apps simply require a few CORS changes in order to get AJAX working properly.
First, make sure you have the Cordova whitelist plugin.
Second, make sure you have the follow in your config.xml file:
<access origin="*" />
<allow-navigation href="*"/>
That should work, though you may also need to specify a Content-Security-Policy META in your headers. This line is about as safe as you can get in that regard:
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data:">
And of course, you'll also need to make sure you have CORS on your server. If you don't have access to Apache, you can set that with:
<?php
header("Access-Control-Allow-Origin: *");
Hope this helps! :)
EDIT:
As for how to actually send data to the form, that depends on how you want the data to be received. I'm going to assume that as you're attempting to do it via HTML, that you're expecting a submission along the lines of http://website.com/send-form?name=John&age=34.
The main problem with the way you're currently submitting your data is the spaces: data: "name="+ name+"& subject="+ subject+". Strip those.
data: "name="+name+"&subject="+subject+"&select-choice-1="+select-choice-1+"& textarea="+textarea+"+"&file="+file+",
Depending on how you're running the back end, you may need to pass ? at the end of the script, to denote that whatever follows should be considered variables. If this is the case, you would replace url: "http://website.com/send-form" with url: "http://website.com/send-form?".
I'd also recommend looking into the AJAX documentation regarding how to pass the data through to your form, so that you can adjust code accordingly.
EDIT 2
To submit the form through JavaScript, you need to use:
document.forms["loginForm"].submit();
Please note that loginForm in my example corresponds to the form ID, which you haven't set. In this case I used loginForm, so you would need to change your HTML to reflect this:
// form on app
<form id='loginForm'>
It's also possible to submit forms without an ID, but it's a little harder. You should only have one form on your page, so you can probably also use:
document.forms[0].submit();
Without knowing the form ID, as document.forms is actually an array.
See here for a little further reading.
Hope this helps!

PHP or jQuery Auto Populate Select box with textbox data

I want to auto populate a select box I have placed on a new submissions page generated by the Joomla ZOO component.
This is because I have an alphabetical category structure. A-Z
I was hoping to use PHP or jQuery to take the first character from the NAME: text box and to use this to determine the category
I have managed to auto select one of the fields to 'true' which works...
(the 'publish?' checkbox, but I have now hidden it using css)
This is what the user see's, I want the category to automatically select the first letter of the select element and use that to determine the category... E.g. "Alpha" would then auto fill the category to "A"... I will post the source code and php code beneath.
Link to Image as I do not have enough reputation (new account)
http://imageshack.com/a/img607/4210/8gpe.png
I was thinking perhaps PHP or Jquery.
The logical process In my opinion would be to:
1.User Enters Name into Text field
2.Run function when user is off 'onfocus' on the text field.
3.Store First Entered text into variable from Textbox $NameCat (Name Category)
4.Compare stored text variable $NameCat to an Array containing the Category values
5.Set select box value to the compared value of $NameCat
Current parts of this operation I can find...
Source code after page has been compiled:
<td valign="top" width="100%" >
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-top: 5px solid #ffffff;">
<tr align="left" valign="top">
<td colspan="3" style="border-top: 3px solid #ffffff; padding: 3px;">
<div class="main">
<div id="yoo-zoo" class="yoo-zoo documentation-default documentation-default-edit">
<div class="submission">
<h1 class="headline">Add Glossary Item</h1>
<form id="item-submission" action="/the-glossary/new-glossary-entry/submission/submission/edit/glossary-item/df423936f97317ac69cac8270f5edef0" method="post" name="submissionForm" accept-charset="utf-8" enctype="multipart/form-data">
<fieldset class="pos-content creation-form">
<legend>Glossary Item</legend>
<div class="element element-itemstate first">
<strong><div style="display:none;">Publish?</div></strong>
<input type="radio" name="elements[_itemstate][value]" id="elements[_itemstate][value]0" value="0" checked="checked" />
<label for="elements[_itemstate][value]0" id="elements[_itemstate][value]0-lbl" class="radiobtn">No</label>
<input type="radio" name="elements[_itemstate][value]" id="elements[_itemstate][value]1" value="1" />
<label for="elements[_itemstate][value]1" id="elements[_itemstate][value]1-lbl" class="radiobtn">Yes</label>
</div>
<div class="element element-itemname required">
<strong>Glossary Item</strong><input type="text" name="elements[_itemname][value]" size="60" value="" /></div>
<div class="element element-textarea">
<strong>Description</strong><div class="repeatable-content">
<textarea rows="20" cols="60" style="width:100%;height:350px" id="elements_eb90a34e-2f96-4407-99ce-529ec19e8e63_0_value" name="elements[eb90a34e-2f96-4407-99ce-529ec19e8e63][0][value]"></textarea>
<div id="editor-xtd-buttons">
<div class="button2-left" id="">
<div class="image" id="">
<a rel="{handler: 'iframe', size: {x: 800, y: 500}}" href="http://pi.portinfo.co.uk/index.php?option=com_media&view=images&tmpl=component&e_name=elements_eb90a34e-2f96-4407-99ce-529ec19e8e63_0_value&asset=871&author=91" class="modal-button" id="">Image</a></div></div></div>
</div>
<span class="delete" title="Delete Element"></span></div>
<div class="element element-itemcategory last">
<strong>Categories</strong>
<div>
<select id="elements_itemcategoryvalue" name="elements[_itemcategory][value][]" title="Category" size="15" multiple="multiple">
<option value="16"> - Guide To Port Entry Glossary</option>
<option value="17"> . - A</option>
<option value="35"> . - B</option>
<option value="36"> . - C</option>
<option value="37"> . - D</option>
<option value="38"> . - E</option>
<option value="39"> . - F</option>
<option value="40"> . - G</option>
<option value="41"> . - H</option>
<option value="42"> . - I</option>
<option value="43"> . - K</option>
<option value="44"> . - L</option>
<option value="45"> . - M</option>
<option value="46"> . - N</option>
<option value="47"> . - O</option>
<option value="48"> . - P</option>
<option value="49"> . - Q</option>
<option value="50"> . - R</option>
<option value="51"> . - S</option>
<option value="52"> . - T</option>
<option value="53"> . - U</option>
<option value="54"> . - V</option>
<option value="55"> . - W</option>
</select>
</div>
<script type="text/javascript">
jQuery(function($) {
var categories_elem = $('#elements_itemcategoryvalue'), primary_elem = $('#elements_itemcategoryprimary');
if (!categories_elem || !primary_elem) return;
categories_elem.bind('change', function() {
var categories = $(this).val() ? $(this).val() : [], primary = primary_elem.val();
if ($.inArray(primary, categories) == -1) primary_elem.val(categories.length ? categories.shift() : '');
});
primary_elem.bind('change', function() {
var categories = categories_elem.val() ? categories_elem.val() : [], primary = $(this).val();
if ($.inArray(primary, categories) == -1) {
categories.push(primary);
categories_elem.val(categories);
}
});
});
</script></div>
</fieldset>
<p class="info">Fields marked with an asterisk (*) are required.</p>
<div class="submit">
<button type="submit" id="submit-button" class="button-green">Submit Item</button>
</div>
<input type="hidden" name="option" value="com_zoo" />
<input type="hidden" name="controller" value="submission" />
<input type="hidden" name="task" value="save" />
<input type="hidden" name="59bca78d9dbba20feb40df6543d2ff2c" value="1" />
</form>
<script type="text/javascript">
jQuery(function($) {
$('#item-submission').EditItem();
$('#item-submission').Submission({ uri: 'http://pi.portinfo.co.uk/' });
});
</script>
</div>
</div>
<!-- Auto-set Published option to 'true' -->
<script type="text/javascript">
jQuery(function($){
$(".element-itemstate [value=1]")[0].checked = true;
});
</script>
</div>
</td>
</tr>
</table>
</td>
If you have any answers or suggestions please don't feel hesitant to share them, they are greatly appreciated.
P.s. I have only done approx 2-3 weeks of self taught PHP so I know about POST/GET/MYSQL connections/getElementById and other little things like string functions... so I do understand a little about it... but I am not a wizard in PHP yet. Perhaps its jQuery that is needed?
Sorry this website is an internal site, So I cannot give you a link... But if you have any questions at-all I will be AQAP to reply.
Many Thanks
Josh.

Get selected text from dropdown in PHP

I am totally new in PHP, in fact the reason I am doing this is to customize a wordpress plugin so it can fit my need. So far I have a default form, what I am doing now is to add a country dropdown. Here's how I add it
<div class="control-group">
<label class="control-label" for="Country">Country :</label>
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
</div>
So far I only able to retrieve the value of selected item with
$cscf['country'];
How can I get the display text which is the country name in this case ?
You can use a hidden field, and with JavaScript and jQuery you can set the value to this field, when the selected value of your dropdown changes.
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<input type="hidden" name="country" id="country_hidden">
<script>
$(document).ready(function() {
$("#itemType_id").change(function(){
$("#country_hidden").val(("#itemType_id").find(":selected").text());
});
});
</script>
Then when your page is submitted, you can get the name of the country by using
$_POST["country"]
<select name="text selection" onchange="getText(this)">
<option value="">Select text</option>
<option value="1">my text 1</option>
<option value="2">my text 2</option>
</select>
First put a java script function on the attribute "onchange" of the select.
Then, create your function that will transfer the text in a text box by using getElementById
<script>
function getText(element) {
var textHolder = element.options[element.selectedIndex].text
document.getElementById("txt_holder").value = textHolder;
}
</script>
Then create a temporary holder:
<input type="" name="txt_holder" id="txt_holder"> type should be hidden
Then assign it in a PHP variable:
$variableName=$_POST['txt_holder'];
Try this
<?php
if(isset($_POST['save']))
{
$arrayemail = $_POST['cscf'];
$mail = $arrayemail['country'];
$explode=explode('#',$mail);
// print_r($explode);
if(isset($explode[0]))
echo ucfirst($explode[0]);
}
?>
<form method="post">
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
<div class="controls">
<input type="submit" name='save' value="Save"/>
<span class="help-inline"></span>
</div>
</form>
Try it like,
<?php
if(isset($_POST['save']))
{
//Let $_POST['cscf']['country']= malaysia#email.com
// you can explode by # and get the 0 index name of country
$cnt=explode('#',$_POST['cscf']['country']);
if(isset($cnt[0]))// check if name exists in email
echo ucfirst($cnt[0]);// will echo Malaysia
}
?>
<form method="post">
<div class="controls">
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia#email.com">Malaysia</option>
<option value="indonesia#email.com">Indonesia</option>
</select>
<span class="help-inline"></span>
</div>
<div class="controls">
<input type="submit" name='save' value="Save"/>
<span class="help-inline"></span>
</div>
</form>

Categories