connection textfield and html table with database - php

dear all..i have a textfield
<tr>
<td>
<td><input type="text" id="model_name"></td>
</td>
</tr>
and a cell
<tr>
<td><div id="value">//i want data show here after fill textfield</div>
</td>
</tr>
beside that, i've a table "settingdata" in database it consist of 2 field:itemdata and remark..
itemdata's value are "UD" and remark's value are "FM=87.5-108.0MHZ"...
what must i do if i want after type model name "car01UD" at textfield inside
<div id="value"></div>
can show "FM=87.5-108.0mhz"...

if you want "0103" + value of itemdata then do following
<input type="text" id="mod" value="0103<?PHP echo $itemdata ?>">

Looks like you are trying to do basic Ajax functionality. Consider reading some Ajax tutorials to get an idea how to do this.

Related

Retriving a row from database for suggestions

I have a form which will input data from database and print it. Now at the customer name, i want an autosuggestion from the list of customer which are already in database. Eg when i start typing it should start suggesting the list of customer names which are stored in database. This is my current code for that text box.
<td valign="top"><div align="right">Enter Customer Name :</div></td>
<td valign="top"><div align="left">
<input name="customername" class="gentxt" id="customername" maxlength="50" type="text" value="<?php echo $customername; ?>" >
</div></td>
http://www.coderiddles.com/codeigniter-fetch-data-from-database/
Go through this link you will get idea how to retrieve the data form database
Happy Coding :)

Retrieve a variable whos name is has another variable in it

Dunno if the title makes sense, but I have a variable which would to put it in basic terms would be called like this:
$_POST['something'+$variable2]
I have a form which is for editing selected records, this form contains entries for all previously selected records:
<form name="input" action="editcar.php" method="POST">
<input type="submit" value="Yes">
while($row = mysqli_fetch_assoc($result))
{
echo'
</div>
<table style="color:white">
<tr>
<td style="text-align:right">Manufacture:</td><td><input type="text" name="manufacture'.$row['carIndex'].'" value="'.$row['make'].'"></td>
<td style="text-align:right">Model: </td><td><input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'"></td>
</tr>
<tr>
<td style="text-align:right">Colour: </td><td><input type="text" name="colour'.$row['carIndex'].'" value="'.$row['colour'].'"></td>
<td style="text-align:right">Reg: </td><td><input type="text" name="reg'.$row['carIndex'].'" value="'.$row['Reg'].'"></td>
</tr>
<tr>
<td style="text-align:right">Price: </td><td><input type="text" name="price'.$row['carIndex'].'" value="'.$row['price'].'"></td>
<td style="text-align:right">Mileage: </td><td><input type="text" name="mileage'.$row['carIndex'].'" value="'.$row['miles'].'"></td>
</tr>
<tr>
<td style="text-align:right">Max MPH: </td><td><input type="text" name="mph'.$row['carIndex'].'" value="'.$row['mph'].'"></td>
<td style="text-align:right">MPG: </td><td><input type="text" name="mpg'.$row['carIndex'].'" value="'.$row['mpg'].'"></td>
</tr>
</table>
</form>
</div> ';
}
?>
</form>
The form is looped for each record previously chosen, to enable mass editing. The isue arouses when I realised I'd have multiple inputs with the same name, so I did:
<input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'">
Placing the primary key of the record it was currently tired to on the end of it's name. Which seemed like a logical way to go about things.
However now I need to call these variables to place in the mysql query and I dunno how to do that, or even if I can.
I have the selected records saved in an array so I have:
foreach ($postid as $carID)
{
$query = "stuff";
mysqli_query($db, $query);
}
Each loop has $carID containing the variables that was put on the end of the form input names.
So something like:
$_POST['something'+$variable2]
is all I can think of but doesn't work.
Any method that works for my overall code is welcome not just a solution to the issue I've made.
Actually your way should work. Just replace the + with . in $_POST['something'+$variable2].
My tip is: use an array as name in your html instead:
<input type="text" name="model[]" value="'.$row['model'].'">
On php-Side you can loop through all $_POST['model'] since its an array now.
You can add the index for every entry in your html, too:
<input type="text" name="model['.$row['carIndex'].']" value="'.$row['model'].'">
PHP uses a dot for concatenation, not + like Java and Javascript:
$_POST['something' . $variable2]
Try something like this:
<form ...>
<?php
while($row = mysqli_fetch_assoc(...):
$index = $row['carIndex'];
?>
<input type="text" name="carmodel[<?php echo $index?>][model]" value="<?php echo $row['model'] ?>">
<?php endforeach; ?>
</form>
This way you will have the data stored in $_POST['carmodel'] as an array indexed by carIndex value as the structure of data in $_POST is defined by names of inputs, here you will have names likee carmodel[1][model] for example so then in post it will be in $_POST['carmodel'][1][model]
you can read here as well
How would I create this array structure in an HTML form?

HTML form to update Mysql with PHP (and HTML)

I've been trying to develop a real estate page where people can add listings. I am new to the world of php mysql. I have been over this problem for over a day and can't figure out where the problem is.
I have a form where people can add data. That's good and working. Now I am starting to have a place where people can add / delete / update their info. I am trying to build this step by step.
This is where a user could pull the information. My problem is with the piece of the code:
edit_form.php?idBAR=$row[id].
Full code below.
<table>
<tr>
<td align="center">EDIT DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"configS_OH.php";//database connection
$order = "SELECT * FROM braaasil_brokerstour.property";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[id]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>$row[day]</td>");
echo ("<td>$row[hours]</td>");
echo ("<td>Edit</td></tr>");
}
?>
</table>
</td>
</tr>
</table>
Then this tutorial try to pass id through the address bar (I don't know much about php to actually say much)
It tries to upload the data into a new form where a person could edit info.
But I can't load the data into the new form. If I use where id=7, I get the info into the form. But this method of passing the info in the address bar like ?idBAR=8... and then try to catch it in the other code (where id=$idBAR), is not working.
Here is the code:
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?php
include "configS_OH.php";//database connection
print $database;
$order = "SELECT * FROM braaasil_brokerstour.property
WHERE id='$idBAR'";
print $idBAR;
$result = mysql_query($order) or die( mysql_error() );
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="idBAR" value="<?php echo "$row[id]"?>">
<tr>
<td>Address</td>
<td>
<input type="text" name="address"
size="20" value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td>Date</td>
<td>
<input type="text" name="day" size="40"
value="<?php echo "$row[day]"?>">
</td>
</tr>
<tr>
<td>Time</td>
<td>
<input type="text" name="time" size="40"
value="<?php echo "$row[hours]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
I tried an tried and tried..
Thank you for your time in advance.
WHERE id='$idBAR'
You haven't assigned $idBAR any value. You need to read it from the $_GET array first:
$idBAR = $_GET['idBAR'];
You should, of course, check that this value exists first, and is acceptable.
I don't see anywhere you have actually used the GET data, just the reference name which is used in GET.
If you first query is working and is getting the $row['id'] value ok - you can verify this when you go to edit_form.php, in your browser URL bar at the top, does it say this:
edit_form.php?idBAR=7
(or whatever number should be there)
If so, then you just need to use the PHP GET. Your data is stored in $_GET[], and in this case, the reference name is idBAR. So your id from your previous page query is sent through the link into your URL, and on your edit_form.php page, you'd use that data as:
$_GET['idBAR']
You can use that, but personally I assign the data to a variable, such as:
$strGetId = $_GET['idBAR'];
Then you can use $strGetId throughout your code.
Also, check things like isset(), empty() etc, just so you know you are working with A) something is actually there, and B) it's not empty etc
if you are putting a variable directly in a string without concatenating, it can't be an array variable; you must concatenate those you also need to surr. so this
echo ("<td>Edit</td></tr>");
should be this
echo ("<td>Edit</td></tr>");
also, it looks like your form is sending data with POST. When you pass form data in the url string after the question mark, that is passing with get.
so...in your form where you want to use that variable, you set it up like this
$idBAR=$_GET['idBAR']; //to get the variable if it was part of the URL
$idBAR=$_POST['idBAR']; //if it was sent with post, as is the case with your form
also, request contains both get and post, so
$idBAR=$_REQUEST['idBAR'];
will work in either case.
The problem is the $row[id] is seen as text just like everything else. You want the value of $row[id]. Instead of
echo ("<td>Edit</td></tr>");
try
echo ("<td>Edit</td></tr>");

how to validate the text fields related to each other in a form

I have a form that has many text fields. Among them I have an issue with textfields (seating area and price). They can me added more and more according to the user clicks.
Here are my conditions:
1 - if (either of them) seating area textfield is empty or the price textfield is
empty the form should not be submitted.
2 - I have already used jquery plugin to validate some text fields but not all.
3 - As the field(seating area and price are related to each other in my condition
I have used their name as array to post all the value in array like:
<tr>
<td>
<input type="text" name="fldSeatingarea[]" id="fldSeatingarea[]">
</td>
<td>
<input type="text" name="fldPrice[]" id="fldPrice[]">
</td>
</tr>
<tr>
<td>
<input type="text" name="fldSeatingarea[]" id="fldSeatingarea[]">
</td>
<td>
<input type="text" name="fldPrice[]" id="fldPrice[]">
</td>
</tr>
<tr>
<td>
<input type="text" name="fldSeatingarea[]" id="fldSeatingarea[]">
</td>
<td>
<input type="text" name="fldPrice[]" id="fldPrice[]">
</td>
</tr>
4 - and whenever the user clicks add more the table row is inserted with fields
5 - I have not validate these fields(fldSeatingarea[] and fldPrice[])
6 - User must put value to both field of same row or to none of same tbl row.
SO my question is: how am I gonna find either of the two fields (fldSeatingarea[] and fldPrice[]) of the same table row are not empty when the form is submitted?
If either of the fields of same table row is empty the form should not be submitted.
I am doing this small experiment using html, php, and a little bit of javascript.
<tr>
<td>
<input type="text" name="fldSeatingarea[]" id="fldSeatingarea_1">
</td>
<td>
<input type="text" name="fldPrice[]" id="fldPrice_2">
</td>
</tr>
<tr>
<td>
<input type="text" name="fldSeatingarea[]" id="fldSeatingarea_2">
</td>
<td>
<input type="text" name="fldPrice" id="fldPrice_2">
</td>
</tr>
Use Id's Like this to reference the fields for validation.. otherwise it will not work.
I don't particularly agree with how you have used IDs on your form. I would keep a JS array variable to indicate the selected seating areas, while you populate the form.
Then you can change the ID of the element to be fldSeatingarea_3 or fldSeatingarea_5 and target both area and price elements by looping through the array for (var n in selection) { //check the 'fldSeatingarea_'+n field }
if you wana concept than you can do it with parent and child concept through jquery
what you have to do find all tr in table and than for each tr find their fldSeatingarea[] and fldPrice[]
after it validate it if you find both are empty return false for submit event
here are some link which may help you more for it
http://api.jquery.com/child-selector/
http://api.jquery.com/parent/
Just my opinion
After submiting the form all the field(fldSeatingarea[] and fldPrice[])
is passed in an array.both of them..
You can used count to compare the how many value are inserted and how many fields are added and if they ddon`t have the same value the form will triger and not submit unless they have the same value.

show result into html table from database

i have textfield inside html table..i want after i type something like "name" in another cell can show all information or description about people who have that name...that information are take from "member_table"...
<tr>
<td>
<td>
<td><input type="text"........></td>
</td>
</td>
</tr>
<tr>
<td> //i want information show here
</td>
<tr>
what's code for it?
Well the answer would be in 3 part.
<tr>
<td> //i want information show here
</td>
<tr>
modify this to
<tr>
<td> <div id="divResult"></div></td>
<tr>
Now you need to write javascript
<script>
// Get the data from the text field
var txtName = document.getElementById("txtName").value;
// Assuming there is a Ajax Function
ajax(txtName);
//Call back function for ajax
function callBack()
{
if(ajaxObj.... )
{
document.getElementById("divResult").innerHTML = ajaxObj.responseText;
}
}
</script>
The third part is getting the required data from Mysql server from PHP.
<?php
$name = $_REQUEST['txtName'];
echo getDataFromMySQL($name);
?>

Categories