I've created a database using mysqli and I have keywords stored in a table called "Keywords". I need to use the keywords stored in this table to display as options on a form in the form of checkboxes.
So, if the keyword has not been added to the db, then the checkbox on the form will not display for users to choose from. What's the best way to achieve this? My sql knowledge is basic but I can have an idea on how the query might look. Just not sure about how to display them as checkboxes.
UPDATE UPDATE UPDATE!
I was able to get my array to display with checkboxes: Here is what I did:
<?php
include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC");
print <<<HERE
HERE;
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
$id = $row["keyID"];
print <<<HERE
<input type="checkbox" id="$key">$key<br />
HERE;
}
?>
NOW MY QUESTION IS: How do I pass the checked value off to my new table (profileTable) a completely separate table containing the form results?
MY FORM CODE LOOKS LIKE THIS (Fri 8/9/13):
<div id="form1profile">
<form method="post" enctype="multipart/form-data" name="addProfile" id="addProfile" >
<fieldset>
<legend>Add/Create a Media Source Profile</legend>
<br>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<th ><label>First Name</label></th>
<td><input name="FName" type="text" id="FName" size="40"></td>
</tr>
<tr>
<th >Middle Name</th>
<td><input name="MName" type="text" id="MName" size="40"></td>
</tr>
<tr>
<th ><label>Last<br>
Name</label></th>
<td><input name="LName" type="text" id="LName" size="40"></td>
</tr>
<tr>
<th><label>Prefix</label></th>
<td><input name="Prefix" type="text" id="Prefix" size="40"></td>
</tr>
<tr>
<th><label>Title
</label></th>
<td><input name="Title" type="text" id="Title" size="40"></td>
</tr>
<tr>
<th><label>Dept</label></th>
<td><input name="Dept" type="text" id="Dept" size="40"></td>
</tr>
<tr>
<th><label>Phone</label></th>
<td><input name="PH1" type="text" id="PH1" size="40"></td>
</tr>
<tr>
<th><label>Other Phone</label></th>
<td><input name="PH2" type="text" id="PH2" size="40"></td>
</tr>
<tr>
<th><label>Email</label></th>
<td><input name="Email" type="text" id="Email" size="40"></td>
</tr>
<tr>
<th><label for="ProfileImg">Photo</label></th>
<td><input name="ProfileImg" type="file" multiple id="ProfileImg" form="addProfile">
</td>
</tr>
<tr>
<th valign="top"><label class="biotextTitle">Bio/Info</label></th>
<td><textarea name="bioLinks" cols="55" rows="25" id="bioLinks" placeholder="Paste Bio/Links here..."></textarea></td>
</tr>
<tr>
<th><label class="Keywords">Keyword</label></th>
<td>
<?php include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC");
foreach ($_POST['keyword'] as $keyword) {
if ($keyword) {
// $keyword is a selected keyword
}
}
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
$id = $row["keyID"];
print <<<HERE
<input type="checkbox" name="keyword[$key]" id="$key">$key<br />
HERE;
}
?>
</td>
</tr>
<tr>
<th><label class="tags">Tags</label></th>
<td><input name="Tags" type="text" id="Tags" size="40"></td>
</tr>
<tr>
<th><input name="SaveBtn" type="submit" id="SaveBtn" formaction="insertProfile.php" formmethod="POST" value="Save"></th>
<td></td>
</tr>
</table>
<br />
</fieldset>
</form>
</div>
You may add a name to your checkboxes:
<input type="checkbox" name="keyword[$key]" value="1">$key<br />
Doing so, when the page is submit you just need to iterate through the selected keywords:
foreach ($_POST['keyword'] as $keyword) {
if ($keyword) {
// $keyword is a selected keyword
}
}
After that you can get all the selected keywords, then you shall just insert them into your table.
change this --
print <<<HERE
<input type="checkbox" id="$key">$key<br />
HERE;
to this
echo "<input type='checkbox' id='tag_$id'>$key<br />";
reason for doing this is so you eliminate all character combinations by just using numeric only. character combo for EX. id="Ava/Flight", id="Ava Flight"
Related
I am facing issue in my dynamic drop down function it not return values i am unable to find where i'm doing wrong.One more issue i am facing regarding this code is that it not open drop down according to menus i.e if i select USA as a country it not show related cities of USA it always show all cities of all countries please set my code i will be highly thankful to you.
function listcatagoery()
{
$query="select * from tblcountry";
$rs=mysqli_query($query);
$html ='<select name="listcatagoery" id="listcatagoery" onchange="submitme();">';
while($row = mysqli_fetch_array($rs))
{
if($cntid == $row['cnt_id'])
{
$html.='<option selected="selected"
value="'.$row['cnt_id'].'">'.$row['cnt_name'].'</option>';
}
else
{
$html.='<option
value="'.$row['cnt_id'].'">'.$row['cnt_name'].'</option>';
}
}
$html.='</select>';
return $html;
}
///////////////////// List sub-catagoery////////
function listsubcatagoery()
{
$query = "select * from tblcity";
$rs=mysqli_query($query);
$html='<select name="listsubcatagoery" id="listsubcatagoery">';
while($row = mysqli_fetch_array($rs))
{
$html.='<option value="'.$row['cit_id'].'">
'.$row['cit_name'].'</option>';
}
$html.='</select>';
return $html;
}
<div id="main">
<div id="right">
<h2>User Registration Here...</h2><br />
<form name="frmadd" id="frmadd" action="" method="post" enctype="multipart/form-data">
<table border="1" cellpadding="5" cellspacing="4" width="90%" align="center">
<tr>
<td>Full Name</td>
<td><input type=text name="textname" size="40" id="textname" required="required"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="rdgender" value="male" size="10">Male
<input type="radio" name="rdgender" value="Female" size="10">Female</td>
</tr>
<tr>
<td>Email</td>
<td><input type=text name="textemail" size="40" id="textemail" required="required"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="textpass" size="40" id="textpass" required="required"><span class="first"></span></td>
</tr>
<tr>
<td>Country</td>
<td>
<?php echo $htmlcountry; ?>
</td>
</tr>
<tr>
<td>City</td>
<td>
<?php echo $htmlcity; ?>
</td>
</tr>
<tr>
<td>Telephone</td>
<td><input type="number" name="txttel" size="11" id="txttel" required="required"></td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="txtphoto" size="40" id="txtphoto"></td>
</tr>
<tr>
<td>Signin</td>
<td colspan="2" align="center"><input type="submit" name="btnadd" id="btnadd" value="SigIn Now" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<?php echo $msg; ?>
</td>
</tr>
</table>
</form><br />
</div>
The dynamic HTML form (with some js help) and PHP script below insert user entered values into MySQL successfully, except that all rows from the form are placed in one row on the database. What am I doing wrong?
Dynamic form HTML:
<table id="dataTable" class="form" border="4">
<tbody style="font-size:8pt">
<th>
<td align="center">Company</td>
<td align="center">Project</td>
<td align="center">Sub-Project</td>
<td align="center">Change From</td>
<td align="center">Change To</td>
<td align="center">Activity</td>
<td align="center">Responsible</td>
<td align="center">Dur</td>
</th>
<tr >
<p>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td><input style="width:100px" type="text" readonly="readonly" name="coa[]" value="<?php echo $co; ?>">
</td>
<td>
<select name="Projectname[]" style="font-size:10pt">
<option selected="selected" required="required">Select project</option>
<?php
foreach($proj as $item){
?>
<option value="<?php echo $item; ?>"><?php echo $item; ?></option>
<?php
}
mysqli_close($conn);
?>
</select>
</td>
<td><input style="width:100px" type="text" required="required" name="Subproj[]"></td>
<td><input style="width:130px" type="text" required="required" name="Changefrom[]"></td>
<td><input style="width:130px" type="text" required="required" name="Changeto[]"></td>
<td><input style="width:300px" type="text" required="required" name="Activity[]"></td>
<td><input style="width:90px" type="text" required="required" name="Resp[]"></td>
<td><input type="text" required="required" class="small" name="Durest[]"></td>
</p>
</tr>
</tbody>
</table>
PHP script:
<?php
include("../../db_conn_ci_i.php");
if(isset($_POST)==true && empty($_POST)==false){
$co=$_POST['co'];
$chkbox = $_POST['chk'];
$Projectname=$_POST['Projectname'];
$Subproj=$_POST['Subproj'];
$Changefrom=$_POST['Changefrom'];
$Changeto=$_POST['Changeto'];
$Activity=$_POST['Activity'];
$Resp=$_POST['Resp'];
$Durest=$_POST['Durest'];
}
$pco=implode(',',$co);
$pa=implode(',',$Projectname);
$pb=implode(',',$Subproj);
$c=implode(',',$Changefrom);
$d=implode(',',$Changeto);
$e=implode(',',$Activity);
$f=implode(',',$Resp);
$g=implode(',',$Durest);
$sql=" INSERT INTO projects (co,Projectname,Subproj,Changefrom,Changeto,Activity,Resp,Durest)
VALUES ('.$pco.','.$pa.','.$pb.','.$c.','.$d.','.$e.','.$f.','.$g.') ";
$query = mysqli_query($conn,$sql);
etc,etc
?>
Any advice?
Thank you.
You have added an extra column but not added a value for it. Since you are inserting seven columns with eight values, that's why it's not working for you. Your insert query is like below.
$sql=" INSERT INTO projects (Projectname,Subproj,Changefrom,Changeto,Activity,Resp,Durest)
VALUES ('.$pa.','.$pb.','.$c.','.$d.','.$e.','.$f.','.$g.') ";
$query = mysqli_query($conn,$sql);
I have this PHP/HTML Code:
<form method="post" action="create_quote2.php">
<table width="800" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Select</strong></td>
<td><strong>Image</strong></td>
<td><strong>Name</strong></td>
<td><strong>Sale Price</strong></td>
<td><strong>Quantity</strong></td>
</tr>
<?php
$sql="SELECT * from products ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$counter=0;
while($result=mysql_fetch_array($rs))
{
$counter++;
echo '<input type="hidden" name="product'.$counter.'" value="'.$_POST["checkbox$i"].'" />';
echo '<tr>
<td><input type="checkbox" value="'.$result["sequence"].'" name="checkbox'.$counter.'" /></td>
<td>Image</td>
<td>'.$result["title"].'</td>
<td>£'.$result["saleprice"].'</td>
<td><input type="text" name="qty" id="qty" size="20" /></td>
</tr>';
}
echo '<input type="hidden" name="counter" value="'.$counter.'" />';
?>
</table>
<input type="submit" name="submit" value="Next" />
</form>
so when boxes are checked you go to the next page with this code:
<table width="800" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Image</strong></td>
<td><strong>Title</strong></td>
<td><strong>Sale Price</strong></td>
<td><strong>Trade Price</strong></td>
<td><strong>Quantity</strong></td>
<td><strong>Total Cost</strong></td>
</tr>
<?php
for($i=1; $i<=$_POST["counter"]; $i++)
{
if($_POST["checkbox$i"])
{
$counter++;
$sql="SELECT * from products where sequence = '".$i."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_array($rs);
echo '<tr>
<td>Image</td>
<td>'.$result["title"].'</td>
<td>£'.$result["saleprice"].'</td>
<td>£'.$result["tradeprice"].'</td>
<td> </td>
<td> </td>
</tr>';
}
}
?>
</table>
it works fine and selects all the correct products from the products table but i need a way to get the posted quantity values for each row.
how can i display the posted quantity values on the second page?
P.S. im not worried about SQL injection on this code...
Use <input type="text" name="qty'.$counter.'" id="qty'.$counter.'" size="20" /> on the first page
Then $_POST["qty{$counter}"] or $_POST['qty'.$i] in the relevant cell
As a side note, you may find it easier to use a HEREDOC structure so that you don't have to keep on adding quote marks to echo stuff:
echo <<<BLOCK
<tr>
<td>Image</td>
<td>{$result["title"]}</td>
<td>£{$result["saleprice"]}</td>
<td>£{$result["tradeprice"]}</td>
<td>Quantity - {$_POST['qty'.$i]}</td>
<td> </td>
</tr>
BLOCK;
I found HEREDOC a great help as the quote marks don't blend into one another so much
To get your quantities just change:
<input type="text" name="qty" id="qty" size="20" />
to
<input type="text" name="qty'.$counter.'" id="qty" size="20" />
and then reference the same way you do for other inputs.
I have my login table in mysql is like this
id
fname
lname
email
contactno
userid
password
acctype
status
Now my form is like this
<form name="frm" method="post" action="registerform.php">
<table id="new-account" class="create-an-account" width="100%" border="1" cellspacing="10px" cellpadding="10px">
<tr>
<td width="45%">
<label for="firstname">First Name</label>
<input type="text" style="width:230px;" name="Firstname" id="Firstname" /></td>
<td width="10%"></td>
<td width="45%">
<label for="lastname">Last Name:</label>
<input type="text" style="width:230px;" name="LastName" id="LastName" />
</td>
</tr>
<tr>
<td>
<label for="">Account Type</label>
<select class="select" name="at" id="ValidSelection" style="width:245px;" >
<option value="0">Select Account Type</option>
<option value="agent">agent</option>
<option value="admin">admin</option>
</select>
</td>
</tr>
<tr>
<td><label for="">Email Id:</label></td>
</tr>
<tr>
<td><input type="text" name="email" id="ValidEmail" style="width:230px;"/></td>
</tr>
<tr>
<td><label for="">Contact Number</label></td>
</tr>
<tr>
<td><input type="text" name="contact" id="ValidNumber" style="width:230px" /></td>
</tr>
<tr>
<td><label for=""><strong>Choose Your Login Id:</strong></label>
<input type="text" style="width:230px;" name="LoginId" id="LoginId"/>
</td>
</tr>
<tr>
<td><label for=""><strong>Password: <br /></strong></label></td>
</tr>
<tr>
<td><input type="password" style="width:230px;" name="Password" id="ValidPassword" /></td>
</tr>
<tr>
<td><label for="">Confirm Password:</label></td>
</tr>
<tr>
<td>
<input type="password" style="width:230px;" name="ConfirmPassword" id="ValidConfirmPassword"
/>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="signup" value="Create Account" style="margin-top:20px" /></td>
</tr>
</table>
and for insert data my php code is like this
<?php
if(isset($_REQUEST['signup'])) {
mysql_query("insert into login (`fname`,`lname`,`email`,`contactno`,`userid`,`password`,`acctype`,`status`) values('".$_REQUEST['Firstname']."','".$_REQUEST['LastName']."','".$_REQUEST['email']."','".$_REQUEST['contact']."','".$_REQUEST['LoginId']."','".$pswd."','".$_REQUEST['at']."','active')");
}
?>
Now here when I am reloading the page it is automatically inserting the last entered values to the database. So here can someone kindly tell me what is the issue here? Any help and suggestions are welcome.
If you reload the page after submitting a form, it will keep the POST data. To solve this follow the below things :
You can redirect to some other page after inserting the data, use header("location:new_page.php")
You can unset REQUEST, use unset($_REQUEST) after insert
after inserting the $_POST data use redirect to avoid this situation .
even you can redirect to same page like this :-
header('Location: '.$_SERVER['PHP_SELF']);
exit;
I wonder whether someone may be able to help me please.
I've put together a form and php code (below) that allows an administrator to search for member records from a mysql database using the email address as the search criteria.
HTML Form
<form name="memberpasswordresetform" id="memberpasswordresetform" method="post" action="search.php">
<div class="container">
<p align="justify">Member Details </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26%" height="25"><strong>Email Address </strong></td>
<td width="4%"> </td>
<td width="70%"><input name="email" type="email" id="email" size="50" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm Email Address </strong></td>
<td> </td>
<td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
</tr>
<tr>
<td height="25"><label>
<input type="submit" name="Submit" value="search" />
</label></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Last Name </strong></td>
<td> </td>
<td><input name="lname" type="text" id="lname" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>New Password</strong></td>
<td> </td>
<td><input name="newpass" type="password" id="newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm New Password </strong></td>
<td> </td>
<td><input name="conf_newpass" type="password" id="conf_newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><input type="submit" name="save" value="save" /></td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
PHP Script
<?php
include("admin/link.php");
include("admin/opendb.php");
mysql_select_db ("userdetails");
$term = $_POST['email'];
$sql = mysql_query("select forename, surname, email address from userdetails where emailaddress like '%$email%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> First Name: '.$row['forename'];
echo '<br/> Last Name: '.$row['surname'];
echo '<br/><br/>';
}
?>
The search functionality works fine, but I can't work out how to populate the forename and surname fields on my form from the records retrieved from my database. I've been looking for, and found examples on how to do this, if I want to simply show the data as a table, but I can't find any that explain how to populate the fields in a form.
I just wondered whether it would be at all possible that someone could provide some guidance please on how I can do this.
Many thanks
The previous answer will work fine if short tags are enabled on the server. You should stick to the long syntax as below in case you change hosting providers at a later date.
<input name="fname" type="text" id="fname" size="30" value="<?php echo $row['surname']; ?>" />
just set the value of your input element
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" value="<?= $row['surname'] ?>" /></td>
</tr>