How to push action form with variable of select? - php

I want to take value from select in html and push it into action url.
So when users select something value of action buyoption will be the value of the selection
I would prefer to do it with php if there is no option to do it with html.
This is my test buy it doesn't change the buyoption to selection variable..
please help
<form action="<?php echo URLROOT; ?>/trades/trade/buyoption/selloption" method="POST">
<div class="row">
<div class="col-md-9 register-right" >
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<h3 class="register-heading">TRADE</h3>
<div class="row register-form">
<div class="col-md-12">
<label for="buy"><b>YOU SEND:</b></label>
<div class="form-group">
<select class="form-control" name="buyoption" id="buyoption">
<option class="hidden" selected disabled>Please select what you want to sell</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">LITECOIN (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div>
<label for="buy"><b>YOU RECEIVE:</b></label>
<div class="form-group">
<select class="form-control" name="selloption" id="buyoption">
<option class="hidden" selected disabled>Please select what you want receive</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">Litecoin (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div>
<input type="submit" class="btnRegister" value="Proceed"/>
</div>
</div>
</div>
</div>
</div>
</div>
</form>

There must not be multiple elements in a document that have the same id value.
You can watch for change event of select element and on Change, manipulate the Action URL.
let buyoption = document.getElementById('buyoption');
let selloption = document.getElementById('selloption');
let postForm = document.getElementById('postForm');
let action = postForm.getAttribute('action');
let replacerFn = function() {
let temp = action;
if (buyoption.value) {
temp = temp.replace('buyoption', buyoption.value);
}
if (selloption.value) {
temp = temp.replace('selloption', selloption.value);
}
postForm.setAttribute('action', temp);
console.log('Action Attribute=> ', postForm.getAttribute('action'));
};
buyoption.addEventListener('change', function() {
replacerFn();
});
selloption.addEventListener('change', function() {
replacerFn();
});
<form action="/trades/trade/buyoption/selloption" method="POST" id="postForm">
<div class="row">
<div class="col-md-9 register-right">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<h3 class="register-heading">TRADE</h3>
<div class="row register-form">
<div class="col-md-12"> <label for="buy"><b>YOU SEND:</b></label>
<div class="form-group">
<select class="form-control" name="buyoption" id="buyoption">
<option class="hidden" selected disabled value="">Please select what you want to sell</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">LITECOIN (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div><label for="buy"><b>YOU RECEIVE:</b></label>
<div class="form-group">
<select class="form-control" name="selloption" id="selloption">
<option class="hidden" selected disabled value="">Please select what you want receive</option>
<option value="btc">Bitcoin (BTC)</option>
<option value="ltc">Litecoin (LTC)</option>
<option value="eur">NATIONAL BANK TRANSFER (EUR/USD/HRK)</option>
</select>
</div><input type="submit" class="btnRegister" value="Proceed" /> </div>
</div>
</div>
</div>
</div>
</div>
</form>

Related

Convert a text field to a selection field drop-down lists

the following code shows a text field for the user to write their city, I would like to convert that text field into a selection field with a drop-down list. My knowledge in php is very basic, I appreciate you can help me.
The code is located in a PHP template of a user profile.
<?php
global $current_user;
wp_get_current_user();
$userID = $current_user->ID;
$user_login = $current_user->user_login;
$state = get_the_author_meta( 'user_state' , $userID );
?>
<div class="row">
<div class="col-md-12">
<div class="couple-profile-main-block dashboard-box">
<form id="user-profile" class="ajax-auth form-horizontal" method="post">
<div class="status"></div>
<?php wp_nonce_field('ajax-user-profile-nonce', 'security'); ?>
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-sm-4"><label><?php esc_html_e('State','theme');?>*</label></div>
<div class="col-sm-8"><input class="form-control" id="state" name="state" type="text" value="<?php echo esc_attr($state);?>"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="col-sm-offset-4 col-sm-8"><button id="couple-profile-on" class="btn btn-pink"><?php esc_html_e('Update Profile','theme');?></button></div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
You’ll need to switch out that text-input to a select input - check out https://www.w3schools.com/tags/tag_select.asp
In your case, something like this should replace where the text input is.
<select class="form-control" id="state" name="state">
<option value=“State1” <?php if ($state == “State1”) echo “selected”; ?>>State 1</option>
<option value=“State2” <?php if ($state == “State2”) echo “selected”; ?>>State 2</option>
<option value=“State3” <?php if ($state == “State3”) echo “selected”; ?>>State 3</option>
</select>

how to filter and validate select option in form using PHP

$batch = htmlentities($_POST['batch']);
if($batch === 'NULL'){
echo "Batch Failed";
} else {
echo "$bacth";
}
<div class="col-md-12">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-globe"></i></span>
<select class="form-control" name="batch" id="select">
<option selected disabled>Batch Preference</option>
<option value="Weekends">Weekends</option>
<option value="Holidays">Holidays</option>
</select>
</div>
</div>
</div>
How to select those select options in form using php, how i can validate them...
Thank You

Multiselect Search with PDO

I would like to list the selected options. But there are 3 separate selectbox. "Brand", "Fuel" and "Gear". For example, when I select ford brand, it is listed but if I select fuel and gear with brand, I get a wrong list. How should it be?
DATABASE MEANING
TABLE (araclar = "CARS TABLE") arac_marka = "BRANDS" arac_vites = "GEAR" arac_benzin = "FUEL"
AND FORM ACTION (SELF)
<form action="" method="get">
<div class="col-xs-12 col-sm-3 col-md-3">
<select name="arac_marka" class="form-control">
<option value="">Marka Seçin</option>
<? $marsor=$db->prepare('SELECT * FROM araclar');
$marsor->execute();
while ($marcek=$marsor->fetch(PDO::FETCH_ASSOC)){ ?>
<option value="<?=$marcek['arac_marka'];?>">
<?=$marcek['arac_marka'];?>
</option>
<? } ?>
</select>
</div>
<div class="col-xs-12 col-sm-3 col-md-3">
<select name="arac_benzin" class="form-control">
<option value="">Yakıt Tipi</option>
<option value="Benzin">Benzin</option>
<option value="Dizel">Dizel</option>
<option value="Hybrid">Hybrid</option>
<option value="LPG">LPG</option>
</select>
</div>
<div class="col-xs-12 col-sm-3 col-md-3">
<select name="arac_vites" class="form-control">
<option value="">Vites</option>
<option value="Benzin">Otomatik</option>
<option value="Dizel">Manuel</option>
<option value="Hybrid">Yarı Otomatik</option>
</select>
</div>
<div class="col-xs-12 col-sm-2 col-md-3 col-lg-2">
<input class="btn btn-primary" type="submit" name="dtara" value="Filtrele">
</div>
<div class="col-xs-12 col-sm-2 col-md-3 col-lg-2">
Tüm Araçlar
</div>
</form>
AND LISTING
<? if(isset($_GET['dtara'])) {
$marka=$_GET['arac_marka'];
$benzin=$_GET['arac_benzin'];
$vites=$_GET['arac_vites'];
$asql=$db->prepare("SELECT * FROM araclar
WHERE arac_marka='$marka'
OR arac_benzin='$benzin'
OR arac_vites='$vites'");
$asql->execute();
} else {
$asql=$db->prepare('select * from araclar');
$asql->execute();
}
while ($acek=$asql->fetch(PDO::FETCH_ASSOC)){?>
//Listing Here
<? } ?>

get the dynamically created multiple select box values using jquery

I have been reviewing a lot of pages about this and can't seem to figure out why this isn't working.
And I have a doubt in the section of,if i have multiple dynamically created select box and this selected values to be passed to the text box with some calculation before using jQuery. please help me i have tortured in this section.
In the below line of input.php have the html code of get the input.
function addMore() {
$("<DIV>").load("input.php", function() {
$("#product").append($(this).html());
});
}
function deleteRow() {
$('DIV.product-item').each(function(index, item){
jQuery(':checkbox', this).each(function () {
if ($(this).is(':checked')) {
$(item).remove();
}
});
});
}
$(document).ready(function() {
$('.product-item').change(function()
{
var option1 =$(this).find('.orderbox1 option:selected').val();
var option2 = $(this).find('.orderbox2 option:selected').val();
option1 *= $(".orderbox3").val();
option2 *= $(".orderbox3").val();
$(".orderbox4").val(option1-option2);
});
});
</SCRIPT>
</HEAD>
<BODY>
<FORM name="frmProduct" method="post" action="">
<DIV id="outer">
<DIV id="header">
<DIV class="float-left" style="margin-left:150px;"> </DIV>
<DIV class="float-left col-heading">Choose Currency</DIV>
<DIV class="float-left col-heading">Choose Product</DIV>
<DIV class="float-left col-heading">Forex Amount</DIV>
<DIV class="float-left col-heading">Rupee Amount</DIV>
</DIV>
<DIV id="product">
<DIV class="product-item float-clear" style="clear:both;">
<DIV class="float-left"><input type="checkbox" name="item_index[]" /></DIV>
<DIV class="float-left"> Choose Currency:
<select name="item_currency[]" id="orderbox1" class="orderbox1" style="width:150px;">
<option selected="selected">Select</option>
<option value="66">United States</option>
<option value="75">European</option>
<option value="12">Philippines</option>
<option value="17">Qatar</option>
</select></DIV>
<DIV class="float-left"> Choose Product:
<select name="item_size[]" id="orderbox2" class="orderbox2">
<option value="1" selected="selected">Select</option>
<option value="10">Forex Card</option>
<option value="20">Currency Notes</option>
</select></DIV>
<DIV class="float-left"><input type="text" name="item_name[]" id="orderbox3" class="orderbox3"/></DIV>
<DIV class="float-left"><input type="text" name="item_price[]" id="orderbox4" class="orderbox4"/></DIV>
</DIV>
</DIV>
<DIV class="btn-action float-clear">
<input type="button" name="add_item" value="Add More" onClick="addMore();" />
<input type="button" name="del_item" value="Delete" onClick="deleteRow();" />F
<span class="success"><?php if(isset($message)) { echo $message; }?></span>
</DIV>
<DIV class="footer">
<input type="submit" name="save" value="Save" />
</DIV>
</DIV>
</FORM>
Firstly, it looks like you are trying to attach a change handler to a div. This is not possible. From the documentation -
The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> elements.
To get it to work you can change this -
$('.product-item').change(function() {
to -
$('.product-item select, .product-item input').change(function() {
Secondly, to handle dynamic rows you you need to use event delegation. Using .on you can apply the function to dynamic elements. You will need to change the function to -
$("#product").on('change', '.product-item select, .product-item input', function() {
var $line = $(this).parents('.product-item');
var option1 = $line.find('.orderbox1').val();
var option2 = $line.find('.orderbox2').val();
option1 *= $line.find(".orderbox3").val();
option2 *= $line.find(".orderbox3").val();
$line.find(".orderbox4").val(option1 - option2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<DIV id="product">
<DIV class="product-item float-clear" style="clear:both;">
<DIV class="float-left">
<input type="checkbox" name="item_index[]" />
</DIV>
<DIV class="float-left">Choose Currency:
<select name="item_currency[]" id="orderbox1" class="orderbox1" style="width:150px;">
<option selected="selected">Select</option>
<option value="66">United States</option>
<option value="75">European</option>
<option value="12">Philippines</option>
<option value="17">Qatar</option>
</select>
</DIV>
<DIV class="float-left">Choose Product:
<select name="item_size[]" id="orderbox2" class="orderbox2">
<option value="1" selected="selected">Select</option>
<option value="10">Forex Card</option>
<option value="20">Currency Notes</option>
</select>
</DIV>
<DIV class="float-left">
<input type="text" name="item_name[]" id="orderbox3" class="orderbox3" />
</DIV>
<DIV class="float-left">
<input type="text" name="item_price[]" id="orderbox4" class="orderbox4" />
</DIV>
</DIV>
<DIV class="product-item float-clear" style="clear:both;">
<DIV class="float-left">
<input type="checkbox" name="item_index[]" />
</DIV>
<DIV class="float-left">Choose Currency:
<select name="item_currency[]" id="orderbox1" class="orderbox1" style="width:150px;">
<option selected="selected">Select</option>
<option value="66">United States</option>
<option value="75">European</option>
<option value="12">Philippines</option>
<option value="17">Qatar</option>
</select>
</DIV>
<DIV class="float-left">Choose Product:
<select name="item_size[]" id="orderbox2" class="orderbox2">
<option value="1" selected="selected">Select</option>
<option value="10">Forex Card</option>
<option value="20">Currency Notes</option>
</select>
</DIV>
<DIV class="float-left">
<input type="text" name="item_name[]" id="orderbox3" class="orderbox3" />
</DIV>
<DIV class="float-left">
<input type="text" name="item_price[]" id="orderbox4" class="orderbox4" />
</DIV>
</DIV>
</DIV>
</DIV>
Here I'm creating a variable ($line) to represent the row. I can then use find to get the orderboxes for that row.

How to let a button automatically enter values from the database into a form?

This is an image of how the page looks like,
The 2 big blue buttons at the top are buttons that represent a value from the database. The main function of this button is to help the user to fill in the form in a convenient way leaving only the "Description" to be filled in.
This is my code for the page,
<div class="bodycontainer">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">My Records</h3>
</div>
<div class="panel-body">
<?php
require 'dbfunction.php';
$con = getDbConnect();
$day = date("l");
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM timetableschedule WHERE day='" . $day . "'");
while ($schedule = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<div class="admininfobox">
<a class="btn btn-primary">
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>
</a>
</div>
</div>
<?php
}
mysqli_close($con);
}
?>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Record Activity</div>
<div class="panel-body">
<form name="Create New Admin" class="form-horizontal" method="post" action="handlerecord.php">
<div class="form-group">
<div class="col-sm-4">
<label>Academic Institution</label>
<input list="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
<datalist id="AcadInst">
<option value="Singapore Polytechnic (SP)">
<option value="Ngee Ann Polytechnic (NP)">
<option value="Temasek Polytechnic (TP)">
<option value="Republic Polytechnic (RP)">
<option value="Nanyang Polytechnic (NYP)">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Level of Teaching</label>
<input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
<datalist id="LvTeaching">
<option value="Undergraduate Teaching">
<option value="Postgraduate Teaching">
<option value="Continuing Education">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Type of Teaching</label>
<input list="TyTeaching" type="text" class="form-control" placeholder="Teaching Type" name="typeofteaching">
<datalist id="TyTeaching">
<option value="Clinical Teaching">
<option value="Academic Teaching">
<option value="Talk">
<option value="Others (Please specify)">
</datalist>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<label for="startdate">Start Time</label>
<input type="text" class="form-control" placeholder="Select Time" name="starttime">
</div>
<div class="col-sm-4">
<label for="enddate">End Time</label>
<input type="text" class="form-control" placeholder="Select Time" name="endtime">
</div>
<div class="col-sm-4">
<label for="enddate">Description</label>
<input type="text" class="form-control" placeholder="Optional" name="Description">
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<input type="submit" value="Add" class="btn btn-primary">
</input>
</div>
</div>
</form>
</div>
</div>
An overview of the database,
I am not sure on how to code it. Like Once the user click onto the button, the button goes through the database and input the data accordingly into the form ready for the user to submit.
My suggestion is to create an AJAX request on click.
Create a page loadFormData.php
Accept key data to search in database. For eg: id. Then search in database with this key and get the results. Finally encode this to json (json_encode()) and echo the result.
When the user clicks on the button, trigger an ajax (Javascript) function and send the key values to the loadFormData.php
From the returned json result, extract institution name, start time, end time etc and fill this to form fields using JavaScript
$("#blueButton").click(function() {
$.ajax({
type: "GET",
url: "loadFormData.php",
dataType: "json",
success : function(data) {
// extract each item from the variable 'data'
}
});
});
You should create a form and use action to state the method of entering the values that you want.
For example
http://www.w3schools.com/php/php_forms.asp
You can use this to enter the data easily.

Categories