Get selected text from dropdown in PHP - 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>

Related

auto populate a text field on a dropdown selection in a tpl file

I have a dropdown like this
<div class="constraint constraint-pattern , panel" {{#unless
constraints.pattern.selected}}style="display:none"{{/unless}} >
<h3 class="full-width">{{__ "Select here"}}</h3>
<select name="constraint" class="select2" data-has-search="false">
<option value="[0-2]">abc</option>
<option value="[2-4]">bcd</option>
<option value="[4-9]">bnm</option>
</select>
</div>
I want to popultate a text area when selecting a value from the dropdown.
<div class="constraint constraint-pattern" {{#unless
constraints.pattern.selected}}style="display:none"{{/unless}}>
<label>
{{__ "Type"}}
</label>
<input type="text" readonly name="patternMask" value="abc"/>
</div>

select tag name not working while using it in php

I have a form in html and I am saving it as search.php:
<form name="myform" action="" method="POST" onsubmit="search_clicked(); return false;">
Keyword<input type="text" name="Keyword" id="Keyword" value="XYZ" required/><!-- value is the default name that appears in the text box-->
<br>
Type
<select name="sel" id="sel" class="form-control" onchange="checkcolors(this.value)">
<option selected value="Users">Users</option>
<option value="Pages">Pages</option>
<option value="Events">Events</option>
<option value="Places">Places</option>
<option value="Groups">Groups</option>
</select>
</form>
<div id="loc_dist_displayarea" style="display:none;">
Location<input type="text" name="Location" value="90007" required/> Distance(meters)<input type="text" name="distance" value="10000" required/>
</div>
<br><br>
<input type="submit" name="Search"/>
<input type="submit" value="clear" id="clear" onclick="return clearclicked()"/>
</form>
and my php script is in the same file:
<div id="body_area" style="display:none">
<?php
echo "hi I am searching ";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
//echo "yes value is selected";
//echo $_POST["Keyword"];
if (isset($_POST['sel'])) {
$selectedval= $_POST["sel"];
echo "$selectedval";
}
//echo $_POST["Location"];
}
echo "no value is selected";
?>
</div>
I am not able to display the $_POST['sel'] while $_POST['Keyword'] is echoed.Please help.
First of all, you arent using good programming practices, you use quotation marks (these " and these ') Indiscriminately. You should only alternate between them when you have them nested.
Next, on the action paramenter you should put the name of the file, even if it's the same.

PHP echo ID and Pop Up form

I have a pop-up form when user clicks "update", there should be a pop up form where user should be able to update but the pop up does not appear because the "a href" has "echo" in the id value. See below: echo $orderId
Update
But if I delete the <?php echo $orderId ?>part, for example Update the pop up works but echo wrong ID.
Below is the form
<div class="popup">
<form method = "post" action="">
<div>
<label for="Order ID">Order ID:</label><?php echo $orderId ?>
</div><br>
<div>
<label for="Order Date">Order Date:</label><?php echo $orderDate ?>
</div><br>
<div>
<label for="Order Total">Order Total:</label><?php echo $totalAmount ?>
</div><br>
<div>
<label for="Preparation Status">Preparation Status:</label>
<select name="preparationStatus"/>
<option value="<?php echo $preparationStatus; ?>">Default(<?php echo $preparationStatus; ?>)</option>
<option value="Pending">Pending</option>
<option value="Prepared">Prepared</option>
</select>
</div><br>
<div>
<label for="Payment Status">Payment Status:</label>
<select name="paymentStatus">
<option value="<?php echo $paymentStatus;?>">Default(<?php echo $paymentStatus;?>)</option>
<option value="Pending">Pending</option>
<option value="Paid">Paid</option>
</select>
</div><br>
<input type="submit" id="btn" name="submit" value="Save" />
<a class="close" href="#close"></a>
</form>
It only echoes id="id=1"
How can I make the popup form appear?

Insert DropDown Values to MYSQL

This is my html form and php file i have to insert my budget dropdown values by select into database
i have to select values from budget and selected value can insert in database
and my data is inserting in my database when i click twice on submit button
<form action="insert.php" method="post" id="register-form" onsubmit=" return add();">
<div class="label"> Name</div><input type="text" id="name" name="name" /><br />
<div class="label">Email</div><input type="text" id="email" name="email" /><br />
<div class="label">Phone Number</div><input type="text" id="phone" name="phone" /><br />
<div class="label">budget</div>
<select id="budget" name="budget">
<option value="">select</option>
<option value="1">0-100</option> <!-- first option contains value="" -->
<option value="2">100-200</option>
<option value="3">200-300</option>
</select>
<br /><br />
<input type="submit" onclick="add()" name="submit" />
<div id="message"></div>
</form>
insert.php
<?php
include("config.php");
if(isset($_POST['submit'])){
$name=$_POST["name"];
$email=$_POST["email"];
$phone=$_POST["phone"];
$budget=$_POST["budget"];
$insert_query="insert into form(name,email,phone,budget) values ('$name','$email','$phone','$budget')";
$con=mysql_query($insert_query);
}
?>
Do the following:
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
Ensure that your field in the database is string/varchar
Re-visit the add() as well. That might be the cause for inserting successfully after the second click.
dropdown pass option value in variable so change the option value which you want to insert in database
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
if u want to select multiple option u use
<select id="budget" name="budget" multiple="multiple">
The $_POST["budget"]; is taking the value that is in the option, in this case the value for "0-100" is 1. If you want the value to be stored in the database use "0-100" as the value.
As far as the insert problem goes, try using this syntax:
$budget=$_POST['budget'];
$query = "INSERT INTO users ( database_budget )
VALUES ('$budget');";
the top value being the value in the database and the bottom value being the post value.
This is assuming that you're connecting to the database correctly. Also use single quotes for post values not double quotes.

Adding Dynamic Field to Forms (Chronoform)

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.

Categories