while($row = mysql_fetch_array($result))
{
$a=$row['diagnosis'];
$b=$row['icd_code'];
echo' <div class="form-group"><div class="col-lg-8">
<input class="form-control" type="text" name="disease" value="'.$a.'">';
echo '</div>
<div class="col-md-11 pull-right mrgT10">
<input type="text" class="tags" name="icd" value="';echo $b.'" />
</div>
</div>'; } ?>
This is my php coding.When i process the form using anotherpage php with the following code
<?php
include('connect.php');
if(isset($_POST['submit']))
{
echo $disease=$_POST['disease'].'<br/>';
echo $icd=$_POST['icd'];
}?>
It will get only the last value like this.
MONOPLEGIA
447637006
Is there anyway to get all the values.Any suggestion or tips would be appreciated.
You can make it your textfields name as name="disease[]" , name="icd[]" instead of name="disease" and name="icd[]"
and you can retrive like
$diseases=$_POST['disease'];
foreach($diseases as $key=>$disease){
echo $disease;
echo $icd=$_POST['icd'][$key];
}
Also, change this value="';echo $b.'" into value="'.$b.'"
try this :
while($row = mysql_fetch_array($result))
{
$a=$row['diagnosis'];
$b=$row['icd_code'];
echo' <div class="form-group"><div class="col-lg-8">
<input class="form-control" type="text" name="disease[]" value="'.$a.'">';
echo '</div>
<div class="col-md-11 pull-right mrgT10">
<input type="text" class="tags" name="icd[]" value="';echo $b.'" />
</div>
</div>'; } ?>
and call the value at other php :
<?php
include('connect.php');
if(isset($_POST['submit']))
{
foreach($_POST['disease'] as $a=>$key){
echo $disease=$a[$key].'<br/>';
echo $icd=$_POST['icd'][$key];
}
}?>
hope this can help you.
You need to pass array so that you will get all values of result and serialize it and submit it to another form.
on next page you need to unserialize the array and you will get the whole array.
please try this code
<?php
while ($row = mysql_fetch_row($result)){
$a[] = $row[3];
$b[] = $row[4];
}
$var1 = serialize($a);
$var2 = serialize($b);
?>
**another_page.php**
<?php
print_r($_REQUEST);
$var1 = unserialize($_POST['b']);
print_r($var1);
die();
Hope U will get some thing...
Related
I have two php files insert.php and function.php
In insert.php
<form action="<?php echo htmlspecialchars( $_SERVER['PHP_SELF'] ); ?>"
method="post">
<input type="hidden" name="Entry" value="<?php print $data; ?>"/>
<hr>
<h3>Entry your new entry</h3>
<?php
include('function.php');
$query_start = "SHOW COLUMNS FROM $data";
$result = mysqli_query( $conn, $query_start );
while($row = mysqli_fetch_array($result)){?>
<label for="<?php echo "$row[0]";?>"><?php display_text($row[0]);?></label>
<br>
<?php display_value($row[0]);
} ?>
<input class="btn btn-primary" type="submit" name="add" value="Add Entry">
</form>
And in function.php
<?php
function display_value($row){?>
<input class='form-control' type='text' placeholder='".$row."' name='".$row."'>
}?>
But I am not getting the form on the web page.
firstly you need to add a <form> tag
<?php
include('function.php');
$query_start = "SHOW COLUMNS FROM $data";
$result = mysqli_query( $conn, $query_start );
while($row = mysqli_fetch_array($result)){
$data = display_value($row[0]);
echo $data;
}
?>
you're just calling that function, for printing the values inside the function, you need to return data from function and store or print the data from you're calling.
$data = display_value($row[0]);
here variable $data will store the output, also you need to return the result from your function
<?php
function display_value($row) {
$data = '<input class="form-control" type="text" placeholder="'. $row .'" name="'. $row .'"><br /><br />';
return $data; //this return will be printed on another page
} ?>
I guess with "form" he meant the fields not the form tag.
#Rohit Sahu answer is wrong.. you dont need to pass the output with a return. Your way of doing it is not a beautiful way. But it is even working this way.
Are you sure your Mysql Query give results?
Example Code:
<?php
// functions.php
function display_value($row) { ?>
<input class='form-control' type="text" placeholder="<?php echo $row;?>" name="<?php echo "$row";?>"><br><br>
<?php } ?>
// insert.php
<form action="....">
<?php
$arr = array(1,2,3,4);
foreach($arr as $row) {
display_value($row);
}?>
</form>
Output:
<form action="....">
<input class='form-control' type="text" placeholder="1" name="1"><br><br>
<input class='form-control' type="text" placeholder="2" name="2"><br><br>
<input class='form-control' type="text" placeholder="3" name="3"><br><br>
<input class='form-control' type="text" placeholder="4" name="4"><br><br>
</form>
Result: It is working fine. You should better post all code to understand where is your mistake. It could be some whitespaces in files, it could be no results from MYSQL.. there are many options. It is difficult to say this way.
Here in this code I am accepting data as an array and inserting it in the database. As I am using while loop I am having multiple input fields with same name as you can see in the HTML code below. What I want to do is, I want to add a check that if two fields are same then echo error saying two fields cannot be the same else insert the data. I could do it if there were two different input fields for the data like if($field1 == $field2){ echo "Error!"; else insert data. But here the field is only one with is giving multiple fields in a while loop. How can I add this check?
HTML form code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php while($pro = $priq->fetch()){ extract($pro); ?>
<div class="row tbpad">
<div class="col-sm-3 col-xs-6">
<p><i class="fa fa-caret-right"></i> <?php echo $bk_title; ?></p>
</div>
<div class="col-sm-1 col-xs-1">
<div class="form-group">
<input type="text" class="form-control" name="priority[]" value="<?php echo $pr_priority; ?>">
<input type="hidden" class="form-control" value="<?php echo $bk_id; ?>" name="bkid[]">
</div>
</div>
</div>
<?php } ?>
<input type="submit" class="btn btn-orange" value="Set Priority" name="priority">
</form>
PHP code:
if(isset($_POST['priority'])){
$priority = (!empty($_POST['priority']))?$_POST['priority']:null;
$bkid = (!empty($_POST['bkid']))?$_POST['bkid']:null;
$iNumSets = count($priority);
for($i = 0; $i < $iNumSets; $i++){
$str = "INSERT INTO priorities(pr_book, pr_by, pr_priority)
VALUES('$bkid[$i]', '$sess', '$priority[$i]')";
$res = $PDO->prepare($str);
$res->execute();
if($res){
echo '<script>alert("Priority added successfully!");window.location = "view-order.php";</script>';
}else{
echo '<script>alert("Server Error! Please try back later.");window.location = "add-order.php";</script>';
}
}
}
Since $priority is an array which you get from $_POST['priority'], you could do the following:
$num_original = count($priority);
$num_unique = count(array_unique($priority));
if ($num_original != $num_unique) {
echo "One or more item were idendical!";
}
Also make sure you use mysql_real_escape_string() or similar methods on your $_POST input before you pass it into the SQL query. Otherwise your code is vulnerable to SQL injections!
$query = "SELECT * FROM abc";
if ($result = $db->query($query)) {
$row = $result->fetch_assoc();
while ($row = $result->fetch_assoc()) {
$data[] = array("a"=>$row["a"],
"b"=>$row["b"],
"c"=>$row["c"],
"d"=>$row["d"]
);
}
$result->close();
}
$db->close();
<?php foreach ($data as $row) { ?>
<form action="">
<div class="col-lg-3">
<input type="text" value="<?php echo $row['a'] ?>"/>
</div>
<div class="col-lg-3">
<input type="text" value="<?php echo $row['b'] ?>"/>
</div>
<div class="col-lg-3">
<input type="text" value="<?php echo $row['c'] ?>"/>
</div>
<div class="col-lg-3">
<input type="text" value="<?php echo $row['d'] ?>"/>
</div>
</form>
<?php } ?>
Above is the exact code that I used to retrieve all the row in a table. The strange part is that I have 5 row and it only show 4. The first row is missing. I wonder why.
In your code, you're calling $row = $result->fetch_assoc(); just before starting your while loop. This line is "consuming" your first raw and when you're entering in the loop, you move the cursor to the second row by calling this same method a second time.
The condition of the while loop is executed BEFORE the content, here you'll find more info about PHP While loop
To fix your code, remove this first line.
I have a form that has a select field that loads the options dynamically from a db result query. Here is what the code looks like. See the description text input afterwards? I need the code to return the description of the item selected under productID. How do I go about this? Thanks very much for all replies.
<div class="row-fluid">
<div class="span3">
<label>SKU</label>
<?php echo '<select name="ITEM" id="user" class="textfield1">';
while($res= mysql_fetch_assoc($sql))
{
echo '<option value="'.$res['productID'].'">';
echo $res['SKU'] ;
echo'</option>';
}
echo'</select>';
?>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label>Description</label>
<input type="text" name="description" value=""/>
</div>
</div>
You can do this in 2 ways:
First way is by redirecting the page having a $_GET parameter which will contain the product id:
<div class="row-fluid">
<div class="span3">
<label>SKU</label>
<?php echo '<select name="ITEM" id="user" class="textfield1"
onchange="document.location=\'my-page.php?pid=\' + this.value">';
while($res= mysql_fetch_assoc($sql))
{
echo '<option value="'.$res['productID'].'"';
// LATER EDIT
if(isset($_GET['pid']) && $_GET['pid'] == $res['productID'])
echo 'selected="selected"';
// END LATER EDIT
echo '>';
echo $res['SKU'] ;
echo'</option>';
}
echo'</select>';
?>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label>Description</label>
<?php
if(isset($_GET['pid']) && is_numeric($_GET['pid'])) {
$sql = mysql_query("SELECT description
FROM products
WHERE product_id='" . mysql_real_escape_string($_GET['pid']) . "'");
$row = mysql_fetch_assoc($sql);
}
?>
<input type="text" name="description" value="<?=$row['description']?>"/>
</div>
</div>
Second way is to have an ajax call and fill description input dynamically, without refresing the page
// this is the JS code
$(document).ready(function(){
$('#user').change(function(){
$.POST("my-ajax-call-page.php",
{pid: $("#user").val()},
function(data){
$('input[name="description"]').val(data.description);
}, "json");
});
});
and your my-ajax-call-page.php should be like this:
<?php
include("mysql-connection.php");
$sql = mysql_query("SELECT description
FROM products
WHERE product_id='" . mysql_real_escape_string($_POST['pid']) . "'");
$row = mysql_fetch_assoc($sql);
echo json_encode("description" => $row['description']);
?>
You will find many examples and documentation for using jQuery library on jQuery library website
<div class="row-fluid">
<div class="span3">
<label>SKU</label>
<?php echo '<select name="ITEM" id="user" class="textfield1" onchange="showDesc()">';
$desHTML = "";
echo "<option value='0'>Please select</option>"
while($res= mysql_fetch_assoc($sql))
{
echo '<option value="'.$res['productID'].'">';
echo $res["SKU"] ;
echo'</option>';
$desHTML .="<div class'descBox' id='".$res['productID']."' style='display:none'>".$res['description']."</div>";
}
echo'</select>';
?>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label>Description</label>
<?php echo $desHTML; ?>
</div>
</div>
Now create one js function and call on onchange of select box.
Js function Hint:
$(".descBox").hide();
$("#"+selectedItemValue).show();
Let me know if you need any help for JS function.
You haven't shown us your SQL query, but I assume that you have a column named description and you are selecting this column in your query too.
So then, you can use jQuery to insert description of selected item to input
<div class="row-fluid">
<div class="span3">
<label>SKU</label>
<?php echo '<select name="ITEM" id="user" class="textfield1">';
$js_array = array(); // This variable will contain your Javascript array with descriptions
while($res= mysql_fetch_assoc($sql))
{
echo '<option value="'.$res['productID'].'">';
echo $res['SKU'] ;
echo'</option>';
// Fill your array with descriptions; ID of item will be the index of array
$js_array[$res['productID']] = $res['description'];
}
echo'</select>';
?>
<script>
var description;
<?php
foreach($js_array as $description => $id)
{
echo("description['".$id."'] = '".$description."';\n");
}
?>
$(document).ready(function(){
$('#user').change(function(){
$("#description").val(description[$('#user').val()]);
})
});
</script>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label>Description</label>
<input type="text" name="description" id="description" value=""/>
</div>
</div>
Be sure to not forget to add id attribute to your input type="text"
So I have a form that users fill out with some radio buttons. The values from the radio buttons get passed to MySQL. I now want to pull those values from the database, display them in a table on a different page, and apply different styles to them with span tags.
Here's the code from the form:
<input class="radio_style" type="radio" name="job_type" value="fulltime"/>Full-time<br/>
<input class="radio_style" type="radio" name="job_type" value="parttime"/>Part-time<br />
Here's the code for the page where I want to display it:
<div class='job_type_div'>
<?php if($job_type=='fulltime') {?>
<span class='job_type_style'>
<?php echo $row['job_type']; ?>
</span>
<?php if($job_type=='parttime') {?>
<span class='job_type_style2'>
<?php echo $row['job_type']; ?>
</span>
<?php } ?>
<?php } ?>
</div>
So ideally, the "fulltime" value will have one style and the "parttime" value will have another style. But when I try running this code, nothing happens. I'm definitely connecting to the database correctly. And the row name is properly labelled "job_type". Any ideas on where I might be going wrong? Any help would be greatly appreciated :)
First of all, your form should be something like so:
<form action="page_you_want_to_display.php" method="POST">
<label for="type">Job Type:</label>
<label for="fulltime">
<input class="radio_style" id="fulltime" name="job_type" type="radio" value="fulltime">
Fulltime
</label>
<label for="parttime">
<input class="radio_style" id="parttime" name="job_type" type="radio" value="parttime">
Part Time
</label>
<input name="submitted" type="submit" value="Submit">
</form>
The page you want to display on should look something like this:
if(isset($_POST["submitted"])){
$job_type = $_POST['job_type'];
echo '<div class="job_type_div">';
if($job_type=='fulltime'){
$res = mysql_query("SELECT * FROM jobs WHERE job_type='fulltime'");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="fulltime">';
echo $row['job_title'].' - '.$row['job_type'];
echo '</div>';
echo '<br>';
}
} elseif ($job_type=='parttime'){
$res = mysql_query("SELECT * FROM jobs WHERE job_type='parttime'");
while ($row = mysql_fetch_assoc($res)) {
echo '<div class="parttime">';
echo $row['job_title'].' - '.$row['job_type'];
echo '</div>';
echo '<br>';
}
}
echo '</div>';
}
and CSS:
.fulltime {
margin:0px;
padding:5px;
width:300px;
background:#9C0;
color:#fff;
}
.parttime {
margin:0px;
padding:5px;
width:300px;
background:#069;
color:#fff;
}
Tested:
Hope this helps
may be problem in your php. Is there some logic?
$job_type=null;
if($job_type=='fulltime'){
...
if($job_type=='parttime'){
...
}
}
did you set $job_type variable? May be you need something like this:
<div class='job_type_div'>
<?php if($row['job_type']=='fulltime') {?>
<span class='job_type_style'>
<?php echo $row['job_type']; ?>
</span>
<?php } elseif($row['job_type']=='parttime') {?>
<span class='job_type_style2'>
<?php echo $row['job_type']; ?>
</span>
<?php } ?>
</div>
I don't believe that the conditions will work the way you implemented it, try doing it like this:
<?php
echo "<div class='job_type_div'>";
if($job_type=='fulltime') {
echo "<span class='job_type_style'>"
//etc...
While you fetching your array from Data Base you need to use MYSQL_BOTH, to fetch columns by Name.
mysql_fetch_array($res, MYSQL_BOTH)
So you should have something like this:
$job_type_query = "SELECT * FROM `job`; ";
$res = mysql_query($job_type_query) or die(mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_BOTH))
{
echo $row['job_type'];
}
form.php
if you initially set one 'selected' in your form, dont need to check if its set, simple set db like so:
...
mysql_query("UPDATE X SET (job_type='{$_GET['job_type']}') WHERE Y");
...
display.php
As you will probably be making a stylesheet, reference the selectors with the job_type labels, which you put in your database
while($row = mysql_fetch_assoc($resultsource))
echo "<div class='job_type_div'> <span class='{$row['job_type']}_style'>{$row['job_type']}</span>";