Here's my HTML code:
<form method = "get" action = "PHPFile.php">
Type:<select name = "type">
<option value = "1">1</option>
<option value = "1">2</option>
<option value = "1">3</option>
</select>
Your Type:<input type = "text" name = "yourType" value = "$varType;">
Fee:<input type = "text" name = "fee" value = "$varFee;">
</form>
I want to get the value of my select "type" and pass it to my textbox "yourType".
And each type options has corresponding fees:
For Type 1 = 250
Type 2 = 90
Type 3 = 90
Here is my Php code:
<?php
$varType = $_GET("type");
if($varType == 1){
$varFee = 250;
}
else {
$varFee = 90;
}
?>
But I always get an parse error on getting the value of $varType
<?php
$varType = null;
if(isset($_GET['type'])) {
$varType = $_GET['type'];
if($varType == 1){
$varFee = 250;
}
else {
$varFee = 90;
}
}
?>
<form method = "get" action = "PHPFile.php">
Type:<select name = "type" ONCHANGE="if(this.value!='-1') location = this.options[this.selectedIndex].value; else return;">
<option value = "-1">---</option>
<option value = "?type=1">1</option>
<option value = "?type=2">2</option>
<option value = "?type=3">3</option>
</select>
Your Type:<input type = "text" name = "yourType" value = "<?php echo $varType; ?>">
Fee:<input type = "text" name = "fee" value = "<?php echo $varFee; ?>">
</form>
Related
I need to sort publications by the data that is received from the form.
I get this error:
Fatal error: Uncaught [42000] - SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sity = 'sity1' make = 'make1' model = 'model1' volume = '' mileage = '' numOfOwn' at line 1 trace: #0 D:\PHP\OSPanel\domains\test\libs\rb.php(1080): RedBeanPHP\Driver\RPDO->runQuery('SELECT * FROM p...', Array) #1 D:\PHP\OSPanel\domains\test\libs\rb.php(4245): RedBeanPHP\Driver\RPDO->GetAll('SELECT * FROM p...', Array) #2 D:\PHP\OSPanel\domains\test\libs\rb.php(12311): RedBeanPHP\Adapter\DBAdapter->get('SELECT * FROM p...', Array) #3 D:\PHP\OSPanel\domains\test\libs\rb.php(13324): RedBeanPHP\Facade::query('get', 'SELECT * FROM p...', Array) #4 D:\PHP\OSPanel\domains\test\index.php(31): RedBeanPHP\Facade::getAll('SELECT * FROM p...', Array) #5 {main} thrown in D:\PHP\OSPanel\domains\test\libs\rb.php on line 810
Why can't I pass a query string as an argument to a function?
I tried to come up with a different algorithm, so as not to pass the string $sql into the argument of the R::getAll() function, but it didn’t work.
Here is the form itself (in index.php):
<form class = "search_form">
<select name = "region">
<option value = "none" hidden = ""> Select a region </option>
<option value = "region1"> Region # 1 </option>
<option value = "region2"> Region # 2 </option>
<option value = "region3"> Region # 3 </option>
<option value = "region4"> Region # 4 </option>
</select>
<select name = "sity">
<option value = "none" hidden = ""> Select a city </option>
<option value = "sity1"> City # 1 </option>
<option value = "sity2"> City # 2 </option>
<option value = "sity3"> City # 3 </option>
<option value = "sity4"> City # 4 </option>
</select>
<select name = "make">
<option value = "none" hidden = ""> Select a make </option>
<option value = "make1"> Make # 1 </option>
<option value = "make2"> Make # 2 </option>
<option value = "make3"> Make # 3 </option>
<option value = "make4"> Make # 4 </option>
</select>
<select name = "model">
<option value = "none" hidden = ""> Select a model </option>
<option value = "model1"> Model # 1 </option>
<option value = "model2"> Model # 2 </option>
<option value = "model3"> Model # 3 </option>
<option value = "model4"> Model # 4 </option>
</select>
<input type = "number" name = "volume" min = "0" step = "any" placeholder = "Enter engine volume">
<input type = "number" name = "mileage" min = "0" placeholder = "Enter vehicle mileage">
<input type = "number" name = "numOfOwners" min = "0" placeholder = "Enter the number of hosts">
<button type = "submit" class = "search-btn btn btn-warning"> Search </button>
</form>
index.php:
$sql = "SELECT * FROM publication";
if(!empty($data)){
$sql .=" WHERE";
$bindings = [];
$i = 0;
foreach ($data as $key => $value) {
$bindings[$i] = $value;
$i++;
if($data->next){
$sql .= " $key = ? AND";
}else{
$sql .=" $key = ?";
}
}
$publications = R::getAll($sql, $bindings);
exit(json_encode($publications));
}else{
$publications = R::findAll('publication', "ORDER BY `id` DESC LIMIT ?, ?", array($from, $publication_on_page));
}
main.js:
$('.search-btn').click(function (e){
e.preventDefault();
let region = $('select[name="region"]').val(),
sity = $('select[name="sity"]').val(),
make = $('select[name="make"]').val(),
model = $('select[name="model"]').val(),
volume = $('input[name="volume"]').val(),
mileage = $('input[name="mileage"]').val(),
numOfOwners = $('input[name="mileage"]').val();
let formData = new FormData();
if(region != 'none'){
formData.append('region', region);
}
if(sity != 'none'){
formData.append('sity', sity);
}
if(make != 'none'){
formData.append('make', make);
}
if(model != 'none'){
formData.append('model', model);
}
if(volume != ''){
formData.append('volume', volume);
}
if(mileage != ''){
formData.append('mileage', mileage);
}
if(numOfOwners != ''){
formData.append('numOfOwners', numOfOwners);
}
$.ajax({
url: 'index.php',
type: 'GET',
dataType: 'json',
processData: false,
contentType: false,
cache: false,
data: formData,
success (data) {
$("#publ").html('');
for(value in data){
$("#publ").append(
'<div class="col-md-6" style="background-color: red;">' + data[value]['id'] + '</div>'
);
}
}
});
});
Tell me if I'm doing sorting right?
It seems that $data->next is not working here, maybe because $data is an array and not an object, you can try this:
$sql = "SELECT * FROM publication";
if(!empty($data)){
// Create an array for columns to make the filter
$filters = [];
$bindings = [];
foreach ($data as $key => $value) {
// No need to index for $bindings
$bindings[] = $value;
// Add column and ? to array
$filters[] = "$key = ?";
}
// Apply glue to get the query
$sql .= ' WHERE ' . implode(' AND ', $filters);
// Do you want to order these too?
$publications = R::getAll($sql, $bindings);
exit(json_encode($publications));
}else{
$publications = R::findAll('publication', "ORDER BY `id` DESC LIMIT ?, ?", array($from, $publication_on_page));
}
I have created 3 text fields which are displayed through a for loop. Now my concern is how will I get the values inputted to those text fields. Here's the code. Thank you.
<?php
echo '<form method = "post" action = "http://localhost:8080/sample.php">';
for($a = 0; $a < 3; $a++){
echo '<input type = "text" name = "box"></br>';
}
echo'<input type = "submit" name = "submit" value = "submit">';
if(isset($_POST['submit'])){
$square = $_POST['box'];
echo $square;
}
echo'</form>';
?>
Like Lucia Angermüller suggested you could do something like this
<?php
echo '<form method = "post" action = "http://localhost:8080/sample.php">';
for($a = 0; $a < 3; $a++){
echo '<input type = "text" name = "box' . $a . '"></br>';
}
echo'<input type = "submit" name = "submit" value = "submit">';
and them you could access $_POST['box0'] or $_POST['box1'].
Hope this helps. Good Luck :)
I have 3 PHP files, in select.php file you have to select the number of forms that you want.. the form contain 3 input fields.
as below:
<form method="post" action="index.php" >
Continue insertion with <select name="counters" id="insert_rows">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
rows
<input type="submit" value="Go" />
</form>
After selecting the number of insertions, it will pass the number to index.php which is contain:
<?php
echo "<form method = 'POST' action = 'process.php'>";
for($counter = 0; $counter < $_POST['counters']; $counter++)
{
echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
echo "Service Version : <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
echo "Service type : <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
}
echo "<input type = 'submit' value = 'SEND'/>";
echo "</form>";
?>
the third file process.php contain
<?php
$name = array();
$version = array();
$type= array();
$name = $_POST['name'];
$version = $_POST['version'];
$type= $_POST['type'];
for($counter = 0; $counter < sizeof($name); $counter++)
{
echo "service_name #".($counter + 1).": ".$name[$counter]."<br />";
echo "service_version #".($counter + 1).": ".$version[$counter]."<br />";
echo "service_type #".($counter + 1).": ".$type[$counter]."<br />";
}
?>
and after i pass the value to it, it shows the data correctly as below
service_name #1: noway
service_version #1: v1
service_type #1: Private
service_name #2: bandar
service_version #2: v2
service_type #2: Public
so my question is : how to create a function in 'process.php' file to insert all these values into the database.
I created a table called 'services' contain these columns "name,version,type"
I will be waiting for your support.
thank you
I would recommend to save the first submitted count value in an hidden input in the second form, then you will be able to loop through that and insert your data
<?php
echo "<form method = 'POST' action = 'process.php'>";
for($counter = 0; $counter < $_POST['counters']; $counter++)
{
echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
echo "Service Version : <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
echo "Service type : <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
}
echo '<input type="hidden" name="cntr" value="'.$_POST['counters'].'" />'
echo "<input type = 'submit' value = 'SEND'/>";
echo "</form>";
?>
Now in your process.php you will have:
if (!empty($_POST['cntr'])) {
$query = 'INSERT INTO services (name, version, type) VALUES ';
for ($x = 0; $x < $_POST['cntr']; $x++)
{
$query .= "('".mysqli_escape_string($_POST['name'][$x])."','"
.mysqli_escape_string($_POST['version'][$x])."','"
.mysqli_escape_string($_POST['type'][$x]).
"'),";
}
$query = substr_replace($query, '', -1);
mysqli->query($query);
}
Whatever I prepared is just a sample you can modify it base on your changes
I would like help with my homework.
I need to use a select menu and then put the selection of the select tag into a function. Now when I tried doing this I get an undefined error (Which is weird since I did define it) I have tried to do the full if system. But I soon gave up because it was just way to much work, while there is probbaly a better way of doing this.
Here is my code:
<form action=<?php echo $_SERVER['PHP_SELF'];?>>
Vertrek:
<select name="vertrek">
<option value="amsterdam">Amsterdam</option>
<option value="utrecht">Utrecht</option>
<option value="denhaag">Den Haag</option>
<option value="rotterdam">Rotterdam</option>
</select>
Aankomst:
<select name="aankomst">
<option value="amsterdam2">Amsterdam</option>
<option value="utrecht2">Utrecht</option>
<option value="denhaag2">Den Haag</option>
<option value="rotterdam2">Rotterdam</option>
</select>
<br/><br/>
<input type="submit" value="Berekenen">
<p>------</p>
De berekende reiskosten zijn:
<?php
reiskosten(isset($_POST['vertrek']), isset($_POST['aankomst']));
?> Euros's
And the "reiskosten" function:
<?php
function reiskosten($vertrek, $bestemming)
{
$reiskosten = array();
$reiskosten[1] = array();
$reiskosten[2] = array();
$reiskosten[3] = array();
$reiskosten[4] = array();
//Amsterdam naar iets
$reiskosten[1][1] = 0;
$reiskosten[1][2] = 30;
$reiskosten[1][3] = 60;
$reiskosten[1][4] = 90;
//Utrecht naar iets
$reiskosten[2][1] = 30;
$reiskosten[2][2] = 0;
$reiskosten[2][3] = 40;
$reiskosten[2][4] = 20;
//Den Haag naar iets
$reiskosten[3][1] = 60;
$reiskosten[3][2] = 40;
$reiskosten[3][3] = 0;
$reiskosten[3][4] = 10;
//Rotterdam naar iets
$reiskosten[4][1] = 90;
$reiskosten[4][2] = 20;
$reiskosten[4][3] = 10;
$reiskosten[4][4] = 0;
echo($reiskosten[$vertrek][$bestemming] . " Euro's ");
}
?>
The first thing you'll want to fix is that you're calling your reiskosten() function with the wrong arguments. What you mean to do is pass form values to that function, but if you look carefully, you'll see that you're passing the return values of the isset() function, each of which is going to be a boolean (TRUE/FALSE) value, not the value from the form element itself. That said, once you fix that, you'll realize that even the value of the form element is not quite what you'll want either. At that point you'll either adjust what the value="" attributes contain, or re-evaluate the way you're indexing things on the back end. Good luck!
Your values are strings no integers.
$reiskosten[$vertrek][$bestemming] is not defined since its
ie. $reiskosten['rotterdam']['rotterdam2'] is not defined.
You might want to change your array keys to strings or the option value to integers.
your form is GET. You are checking for POST variables.
The isset() function returns true or false, so the following line is incorrect since you appear to want to send the values through:
reiskosten(isset($_POST['vertrek']), isset($_POST['aankomst']));
Change it to:
if( isset($_POST['vertrek']) && isset($_POST['aankomst']) ) {
reiskosten($_POST['vertrek'], $_POST['aankomst']);
}
Also, the values for vertrek and aankomst will be whatever the value field in the HTML option is, so it will be a name, not a number.
You can address it by using associative arrays like:
$reiskosten = array();
$reiskosten['amsterdam'] = array();
$reiskosten['utrecht'] = array();
$reiskosten['denhaag'] = array();
$reiskosten['rotterdam'] = array();
And the same for the sub-array definitions where you set the prices.
Your form should be:
<form action=<?php echo $_SERVER['PHP_SELF'];?>>
Vertrek:
<select name="vertrek">
<option value="1">Amsterdam</option>
<option value="2">Utrecht</option>
<option value="3">Den Haag</option>
<option value="4">Rotterdam</option>
</select>
Aankomst:
<select name="aankomst">
<option value="1">Amsterdam</option>
<option value="2">Utrecht</option>
<option value="3">Den Haag</option>
<option value="4">Rotterdam</option>
</select>
<br/><br/>
<input type="submit" value="Berekenen">
<p>------</p>
De berekende reiskosten zijn:
<?php
$reiskosten = reiskosten($_POST['vertrek'], $_POST['aankomst']);
echo $reiskosten . "Euros's";
?>
option values should (in this instance) be integers.
remove isset() as this only checks if they are set and does not actually return their values.
Create a new variable ($reiskosten) which will be the returned result of the function, and echo it out.
Your PHP should be:
<?php
function reiskosten($vertrek, $bestemming)
{
$reiskosten = array();
$reiskosten[1] = array();
$reiskosten[2] = array();
$reiskosten[3] = array();
$reiskosten[4] = array();
//Amsterdam naar iets
$reiskosten[1][1] = 0;
$reiskosten[1][2] = 30;
$reiskosten[1][3] = 60;
$reiskosten[1][4] = 90;
//Utrecht naar iets
$reiskosten[2][1] = 30;
$reiskosten[2][2] = 0;
$reiskosten[2][3] = 40;
$reiskosten[2][4] = 20;
//Den Haag naar iets
$reiskosten[3][1] = 60;
$reiskosten[3][2] = 40;
$reiskosten[3][3] = 0;
$reiskosten[3][4] = 10;
//Rotterdam naar iets
$reiskosten[4][1] = 90;
$reiskosten[4][2] = 20;
$reiskosten[4][3] = 10;
$reiskosten[4][4] = 0;
$return = $reiskosten[$vertrek][$bestemming];
return $return;
}
?>
No need to echo out here, just return the value.
Hi am using a jquery code like this
$(".selfont").change(function(event){
$('#dav').val();
window.location ='?davQ=' + $('#dav').val() + '&pathogenQ=' + $('#pathogen').val() + '&topicQ=' + $('#topicF').val() ;
});
I want to keep the dropdown value selected by the user in each dropdown boxes. But at present the value is not the one selected by the user, its always showing the first value. How can I set the dropdown field with value selected by the user using jquery? Please help me.
My first select box code is like below
<select name="dav" id="dav" style="width: 275px" class='selfont' >
<option value='' class=''>Select one</option>
<?php
$test = mysql_query("SELECT DISTINCT DataVersion FROM olivesdeptable ORDER BY DataVersion DESC");
$i=1;
while($numval=mysql_fetch_array($test))
{
print "<option value=\"".$numval['DataVersion']."\">".$numval['DataVersion']."</option>";
$i=$i+1;
}
?>
</select>
Even if we select value it will show as "Select one" in the field.
javascript code for dropdown fields
<script type="text/javascript">
if (document.getElementById("dav").selectedIndex < 1)
{
document.getElementById('pathogen').selectedIndex = "";
document.getElementById('pathogen').disabled = true;
}
if (document.getElementById("pathogen").selectedIndex < 1)
{
document.getElementById('topicF').selectedIndex = "";
document.getElementById('topicF').disabled = true;
}
if (document.getElementById("topicF").selectedIndex < 1)
{
document.getElementById('ind').selectedIndex = "";
document.getElementById('ind').disabled = true;
}
if (document.getElementById("ind").selectedIndex < 1)
{
document.getElementById('subind').selectedIndex = "";
document.getElementById('subind').disabled = true;
}
if (document.getElementById("subind").selectedIndex < 1)
{
document.getElementById('countryR').selectedIndex = "";
document.getElementById('countryRF').options.length = 0;
document.getElementById('countryRF').selectedIndex = "";
document.getElementById('countryR').disabled = true;
document.getElementById('countryRF').disabled = true;
}
</script>
even the value is updated, the second drop down box is showing as disabled ?
Next dropdown field markup is as below
<select name="pathogen" id="pathogen" style="width: 275px" class='selfont' >
<option value=''>Select one</option>
<?php
$test = mysql_query("SELECT DISTINCT Pathogen FROM olivesdeptable where DataVersion='$davQ' ORDER BY Pathogen ASC");
$i=1;
while($numval=mysql_fetch_array($test))
{
print "<option value=\"".$numval['Pathogen']."\">".$numval['Pathogen']."</option>";
$i=$i+1;
}
?>
</select>
only first dropbox value is working for next dropbox value is storing in url but in page the value shows as 'Select one' ? Please help to sort
$(document).ready(function () {
$('#dav').val(getURLParameter('davQ'));
$('#pathogenQ').val(getURLParameter('pathogenQ'));
$('#topicQ').val(getURLParameter('topicQ'));
$(".selfont").change(function (event) {
window.location = '?davQ=' + $('#dav').val() + '&pathogenQ=' + $('#pathogen').val() + '&topicQ=' + $('#topicF').val();
});
function getURLParameter(name) {
return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]);
}
});
<?php
$test = mysql_query("SELECT DISTINCT DataVersion FROM olivesdeptable ORDER BY DataVersion DESC");
$i=1;
$selected = '';
// make compare with the value you want to select with the parameter form url
// here I assume $_GET['davQ'] holds the value to $numval['DataVersion']
if($_GET['davQ'] == $numval['DataVersion']) $selected = 'selected';
while($numval=mysql_fetch_array($test))
{
echo "<option value=\"".$numval['DataVersion']."\" $selected>".$numval['DataVersion']."</option>";
$i=$i+1;
}
?>