I really need an advice with multiple checkboxes + textboxes in PHP - php

I'm trying to make a mobile repair website and I am currently stuck a little. From my tiny admin panel I can add a "brand", a "service" e.g. repair a display, and add a phone model. The problem is with adding a phone model. Just stay with me. Right now I am adding services with price attached, so there can be "Fix display for 100", "Fix display for 200" etc. So after adding a lot of "services" you can add a phone model and check the services withing a checkbox list. This uses too much space, so I'm stuck with the problem of having just the service names, and inputing the price to the service later (when i add a phone model)
$result3 = mysqli_query($con,"SELECT id,name FROM Brand");
echo '<select class="form-control"name="brandid">';
while ($row3 = mysqli_fetch_array($result3)) {
echo '<option value="'.$row3['id'].'">'.$row3['name'].'</option>';
}
echo '</select>';
?>
Name: <input class="form-control" type="text" name="modelname">
Image: <input class="form-control" type="text" name="modelimage"></br>
Service List: <?php echo '<table class="table table-bordered table-condensed"><tr><th>*</th><th>Service Name</th><th>Price</th></tr>';
$result4 = mysqli_query($con,"SELECT id,servicename, serviceprice FROM service");
while ($row4 = mysqli_fetch_array($result4)) {
echo '<tr><td><lable>';
echo '<input type="checkbox" value="'.$row4['id'].'" id="'.$row4['id'].'" name="servicelist[]"></td><td>'.$row4['servicename'].' </td><td>'.$row4['serviceprice'];
echo '</lable></td></tr>';
}
echo '</table>';
$row4['serviceprice']; - I want to replace this with a text box. But not sure how to send the values. I'm just lost. Should I make a mysql table named Price now? With priceid, serviceid and modelid? And then somehow join all of this when showing the model? Really lost.
Would like to hear a tip from someone more expirienced than me. Thank you.
Alex.
Edit:
I got to this point:
$pricelist=$_POST['price'];
foreach ($pricelist as $k){
$sql="INSERT INTO Price (modelname, serviceid, price)
VALUES ('$name', '$SERVICE_ID_OF_ONE_SERVICE', '$k')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
need to put in service id for each price from an array that i get from a from of checkboxes. should i have foreach within foreach?

Im not i fully understand what you want to do, but if you want to send data from your website using a text box to your php file you can do it as follows.
<form action="yourphpfile" method="post">
<input type= "text" name="price" placeholder="enter your price"/>
<input type="submit" value="submit"/>
</form>
your php
$price= $_POST["price"]
now the variable $price holds the price that you sent. If your php is in the same file as your html you leave action as "" or just write the filename.

Related

Populate textbox with table entry plus one?

I am trying to increment project number based on the last entry. The the primary key PROJECTNOID auto-increments but is not the same format as the project number (Ex: PROJECTNOID = 1 and Project Number = 19000). I don't want this to be a dropdown box even though some of my code shows the opposite.
<?php
connect = mysqli_connect("**", "**", "**", "**");
$query4 = "SELECT PROJECTNOID, ProjectNumber FROM tblProjects ORDER BY
PROJECTNOID";
$result4 = mysqli_query($connect,$query4);
$options4 = "";
while($row4 = mysqli_fetch_row($result4);){
$options4 = $options4."<input value=$row4[0]$row4[1]</input>";
}
?>
Here is the html textbox:
<label for="txtfield">Project Number</label>
<!--<input type="text" id="reqtxtfield" name="projectnumber"
value="<?php ?>" readonly/>-->
<?php echo $options4;?>
But it would look like how you had it but instead of '1' inside the
box it would display '19000' and there would be nothing outside of the
box other than the label "Project Number". As far as i'm aware you can
assign a value to the text box, regardless of whatever the input is. I
would like it to display the value from one field name but actually
contain the value from a different field name. Both are in the same
table of course.
OK - gotcha. Unfortunately, you cannot do that. A textbox can only have one value and the user is always free to change that value, even if you make it read-only. You can test that out by using the developer toolbar in your browser. Probably a good time to mention that all user input should be considered dangerous and you should never trust it. Once they have submitted the form you need to verify it.
What I would recommend in your case is to use a hidden <input> which contains the value you actually want to submit; projectnoid. You can then display the Project Number in any manner you choose.
<form>
<h1>Project Number: 19000</h1>
<input type="hidden" name="projectnoid" value="1">
<input type="submit" name="submit">
</form>
To generate this, you would:
<?php
while($row4 = mysqli_fetch_row($result4)){
$projectnoid = $row[0];
$projectNumber = $row[1];
echo '<h1>' . $projectNumber . '</h1>';
echo '<input type="hidden" name="projectnoid" value="'. $projectnoid .'">
}
PHP:
<?php
$query_5 = "SELECT MAX(ProjectNumber) FROM tblProjects;";
$result_5 = mysqli_query($conn, $query_5);
$row_5 = mysqli_fetch_array($result_5);
$nextproject=$row_5['MAX(ProjectNumber)']+1;
?>
HTML:
<html>
<form class="myform" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>" method="post">
<label for="txtfield">Project Number</label>
<input type="text" id="reqtxtfield" name="projectnumber" value="<?php echo $nextproject ?>" readonly/><br>
After running successful insert query:
echo "<meta http-equiv='refresh' content='0'>"; //REFRESH PAGE TO UPDATE PROJECT NUMBER

Generate data in form from drop down list

been searching around and can't seem to find the answer as yet, the plan is to have 2 drop down lists which will be model and SKU that list data from sql which is fine i got that working but when an item is selected, i need to populate input fields like price, RRP, discount rate and anything else required.
So i have cleaned up what i had into a sample and i need to build a function that when the drop down option is selected another sql command can execute to obtain the rest of the data from that item and fill in other forms. If a sample can be worked out i can expand it across the rest of the giant form that i have prepped.
Once this issue can be worked out i can then work on the php/mysql processing of the data and generate the PDF at the end. But until i can get all the data in the fields im kinda stuck and would appreciate any idea's.
<?php
//connect to the database
$db=mysql_connect ("localhost", "root", "toor") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("db");
// Option 1
$sql="SELECT * FROM `cctv` ORDER BY `cctv`.`SAPCode` ASC ";
$q=mysql_query($sql);
echo "<select name=\"q_option1\">";
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($q))
{
echo "<option value='".$row['SAPCode']."'>".$row['SAPCode']."</option>";
}
echo "</select>";
?>
<form id="form1" name="form1" method="post" action="">
<label for="price">price</label>
<input type="text" name="price" id="price" />
</form>

Inputting multiple values in database from HTML form using PHP

I'm trying to add multiple values into a database using PHP from an HTML. However, I can't just refer to the name attribute of the HTML form because each field in the form is generated by a PHP script. I've tried Googling around, but since I don't exactly know what I'm looking for, my search has been futile.
Here's the bit of code that I use to generate the HTML form:
<form action="input_points.php" method="post">
<?php
while($row = mysql_fetch_array($result)) {
echo $row['Name'] . ' <input type="text" name="userpoints">';
}
?>
<button type="submit" name="add_points">Add Points </button>
</form>
I don't know what names are currently in the directory so I need this piece of php to determine what names are in the database. Afterwards, I want to have a bunch of boxes for people to input points (hence the form). I'm having trouble figuring out how to link the particular text box with the user.
For example, if I have a text box for Bob, how would I link up the input text field that contains the number of points Bob earns with Bob's entry in the database?
I know you can do this with regular form fields:
$userpoints = $_POST['userpoints'];
UPDATE members SET points = $userpoints where $user = "Bob";
But since I have multiple users, how do I link up the correct database entry with the right user? Also, how would I determine which boxes are empty and which boxes are updated with a value?
If you want to update multiple filed then are using array
Please changes some code
<form action="input_points.php" method="post">
<?php
$userCount=mysql_num_rows($result);
echo '<input type="hidden" name="userCount" value="' .$userCount. '">';
while($row = mysql_fetch_array($result)) {
echo '<input type="hidden" name="userid[]" value="' .$row['id']. '">'; //Give the uniq id
echo $row['Name'] . ' <input type="text" name="userpoints[]">';
}
?>
<button type="submit" name="add_points">Add Points </button>
</form>
PHP Code -
$userCount = $_POST['userCount'];
for($i=1; $i=$userCount; $i++){
$userpoints = $_POST['userpoints'];
$userid = $_POST['userid'];
//UPDATE members SET points = $userpoints where $user = $userid;
//YOUR CODE HERE
}
The update you're trying to do is not safe unless you treat values to prevent SQL injection... but if you really want it, instead of mysql_fetch_array(), try using mysql_fetch_assoc().
Using mysql_fetch_assoc() you can extract the keys (database field names) with array_keys(). The keys will be your the name property of your form fields and the values of will be the fields' values.
Hope it helps.
You can use an array to store all the data that you need and add a hidden field that contains the missing data:
<form action="input_points.php" method="post">
<?php
for($i=0; $row = mysql_fetch_array($result); $i++ ) {
echo ' <input type="hidden" name="user[0]['name'] value ='". $row['name'] ."'">';
echo $row['Name'] . ' <input type="text" name="user[$i]['points'] ">';
}
?>
<button type="submit" name="add_points">Add Points </button>
</form>
Problem when you hit submit userpoints contain only the last value previous all values are overwritten
solution
name="userpoints"
must be different each time why not you define it in database and then fetch it just like you fetch $row['Name']?

How to handle 2 radio buttons per search result with php

When my user searches for game in my DB, I present them with a list of the games that match their search term, and then i want to add a button to each item that allows the user to select "have it" or "want it" then have a single "add" button that adds their selections to their profile.
because you cant have it and want it at the same time, i assumed a radio button would be the best choice.
but now i am lost as to how to handle the buttons. When i click on one selection from the search, it will deselect when i choose have or want on another game.
I understand that i should be creating a separately name form for each search result, but then how would i manage the data when sending it to my controller? Maybe i need incrementing form names and then count how many results and use that in my controller?
Also, i need the gameID associated with the selections so i need to send a hidden value with that data for each selection
maybe i am going about this the wrong way...
heres my code
echo '<div id="UserSearchGameResults">';
echo '<form action="#" method="Post">';
$x = 0;
while($gamesLike != null)
{
$x++;
echo '<div id="game_search_list_item">'.$x.'. '.$gamesLike['title'].'</div>
<span class="gamelistblue">
<input type="radio" name="haveOrWant" value="have" />Have it
</span>
<span class="gamelistorange">
<input type="radio" name="haveOrWant" value="want" />Want it
</span>
<input type="hidden" name="gameID" value="'.$gamesLike['ID'].'" />';
$gamesLike = $statement->fetch();
}
$statement->closeCursor();
echo '<br /> <input type="submit" value="Add Game(s)" />';
echo '</div></form>';
any help is appreciated with the subject
new ideas on how to handle my needs are welcome too.
Dan
Use arrays for the input names. Something like
<input type="radio" name="game' . $gamesLike['ID'] . '[haveOrWant]" value="have" />Have it

PHP/MySQL question [facebook-related]

Okay... I'm having the following problem - in my facebook application I let people add quotes sending them to my DB with their user_id, post_id and date. Then there's a browse page where people see the posts - so far, so good. But then I add a button that lets my users set the said post as their status. Still all good. However when I hit the button what it does is setting all the posts in the DB as my status, one by one until they all print out as my statuses. I'm sure that it is because of the 'while' function I use and because I am not sure how to print out all posts and being able to add to each the said button, holding only the specific post_id from the db ._.'
So in other words, it is a PHP/MySQL problem mainly, so even if you are not familiar with FBML, you can still help me out with the code...
Think of it as a list of posts and each should have a button doing something and being somehow attached to only the specific post.
The code is the following:
.... some code here ....
$query = 'SELECT * FROM tb_table ORDER BY `time` DESC';
$results = mysql_query($query);
---some other code---
while($line = mysql_fetch_assoc($results)) {
echo '<TABLE BORDER=0><TR VALIGN=TOP><TD>';
echo "<fb:profile-pic uid=".$line['userid']." size='square' facebook-logo='true'></fb:profile-pic></TD>";
echo "<TD>".$line['postid']."<br>"."<br>"."Posted by: ".$data['first_name'].$data['last_name']."<br>".date("F j, Y, g:i a", $line['fb_time'])."</TD>";
//Problems start from here
echo $facebook->api_client->users_setStatus($line['postid']) ;
echo '<div id="statusdiv" style="display:<?=$visibility;?>;">
<form method="POST">
<input type="submit" value="change status" />
</form>
</div> ';
echo "</TR></TABLE><br>";
The thing you're doing now is every time you loop through your mysql resultset you call the users_setStatus function which then sets your status (for every iteration).
You want to add a hidden input field to the form which contains the $line['postid'] and then call users_setStatus after a POST of the form. In that way you only change your status once.
Update
Remove the call to users_setStatus from the while loop.
Change your form like this:
echo '<div id="statusdiv" style="display:<?=$visibility;?>;">
<form method="POST">
<input type="submit" value="change status" />
<input type="hidden" name="line" value="'.$line['postid]'.'" />
</form>
</div> ';
Then, catch the POST variable upon submitting. With something like:
if (isset($_POST['line'])) {
echo $facebook->api_client->users_setStatus($_POST['line']) ;
}
See http://www.w3schools.com/php/php_forms.asp for more info.

Categories