PHP Forms

Hello everyone,

I have been stuck with this problem for a while now with no solution.
What I want to do is have a form that is populated with data from mysql table.

I want to create a select input that every time I click on one of the items in the list it will automatically change the value of the next input in the form, the order limit input. So, if I click on an item from the tag I want the “order_limit” taken from mysql db to be inserted as the value of:

<input type="number" name="amount" id="amount" class="form-control" min="1" value="1">

Items table @ MySql DB:
tbl.items: item_name,order_limit,ordered,item_department

[php]
$query = “SELECT item_name,order_limit,ordered,item_department FROM items”;
$result = $link->query($query);

echo ‘<select name=“item_name” id=“item_name” class=“selectpicker show-tick” data-placeholder=“Choose item” data-live-search=“true” onchange=“if (this.selectedIndex) amount.value=”’.json_encode($row[1]).’";">’;
while($row = $result->fetch_row())
{
if($row[1] == $row[2])
echo ‘’.$row[0].’ ‘.$row[3].’ - All items ordered’;
else
echo ‘’.$row[0].’’;
}
echo ‘’;
echo ‘’;[/php]

<script type="text/javascript"> var amount = document.getElementById('amount'); </script>

I get an error:

Notice: Undefined variable: row in...

Of course, I understand that error, but what is the solution to make this work?

Hello,

When you use the fetch_row() function and you assign it to the $row variable, the name of the column will be the key in the array not simply numbers. Instead of using $row[0], $row[1], $row[2], and $row[3], you want to use $row[‘item_name’], $row[‘order_limit’], $row[‘ordered’], and $row[‘item_department’]. I’ve changed your code and displayed it below, please tell me if this is what you are looking for and if it works.

[php]$query = “SELECT item_name,order_limit,ordered,item_department FROM items”;
$result = $link->query($query);

echo ‘<select name=“item_name” id=“item_name” class=“selectpicker show-tick” data-placeholder=“Choose item” data-live-search=“true”’;

while($row = $result->fetch_row())
{
if($row[‘order_limit’] == $row[‘ordered’])
echo ‘’.$row[‘item_name’].’ ‘.$row[‘item_department’].’ - All items ordered’;
else
echo ‘’.$row[‘item_name’].’’;
}
echo ‘’;
echo ‘’;[/php]

Again tell me if this works.

Cheers!

Hello OpzMaster,

This is bancko, signed on a different email since gmail sucks, takes forever to receive mails…

The code you have posted in your reply indeed does not have any bugs and work, but it doesn’t solve this problem.
The array is of type MYSQL_BOTH so both numeric and field names would work when pulling data out.

The idea is that I want to echo a form that when ever I click on one of the options in the dropdown menu the “number” input field will be updated accordingly.
I thought about putting an “onclick” function for each of the options inside the select tags but then I couldn’t pass the PHP variable of order_limit to the Javascript function.

There has to be a better to go about solving this issue than I’m thinking.

I explain what I want to do again:

I want to have a dropdown list that when a person clicks one of the options the next input tag (of type number) in the form gets updated with a value, specific to that option, from a mysql table.
i.e.
I have two items to order: Soap, Shampoo.
Order limit for Soap is 2.
Order limit for Shampoo is 1.
When you click Soap in the dropdown the next input in the form will be updated to the value 2.
When you click Shampoo in the dropdown the next input in the form will be updated to the value 1.

Thanks

Solved…

called a function:

function changeValue(val)
{
document.getElementById(‘amount’).setAttribute(“max”,val);
document.getElementById(‘amount’).value=“1”;
}

silly me. :confused:

Sponsor our Newsletter | Privacy Policy | Terms of Service