How to update a database value with an imagebutton in php - php

I am working on an php file, winch shows an table with values of the database. every row has an imagebutton at the end, which should change the status, displayed with an image.
so I think short function of it all must be, on click go to script go to php or jQuery?
the buttons also have to give the id of the user to the next script or function, or os there a better way?
here is my basic approach, with no functionality, for the moment
in index.php
include("function.php");
while ($row_user = mysql_fetch_assoc($result_user)) {
$sqle = mysql_query("
SELECT COUNT(*) FROM
places2user
WHERE
user = '".mysql_real_escape_string($row_user['ID'])."'
");
$res23 = mysql_fetch_array($sqle);
echo <table>
echo <tr>
echo "<td>".$row_user['ID']." </td><td><input type=\"image\" src=\"images/".htmlentities($row_user['receive'], ENT_QUOTES)."\" id=\"status\" name=\"status\" ></td>
echo </tr>
echo </table>
}
there is also a function.php, with a changestatus($id) function, which updates the database values, but I think, this is not the right way to code this.

While programming, don't rely on javascript only. I always create form using just html and css, make sure it works, and than add fancy stuff with jQuery or so. This way you know the form will also work without javascript (great for users without it).
But to come to your question:
if you use this, it should do what you want:
<?php echo "<form action='?' method='post'>
<table>
<tr>
<td>".$row_user['ID']." </td>
<td><input type='hidden' name='userId' value='".$row_user['ID']."'/>
<input type=\"image\" src=\"images/".htmlentities($row_user['receive'], ENT_QUOTES)."\" id=\"status\" name=\"status\" ></td>
</tr>
</table></form>";

Related

loading No. in mysql database into a textbox

The following code segment is used by me to load the contents in the database as a table!(each row has a button with value load!
while($row=mysqli_fetch_array($result))
{
echo"
<tr id='rowNum'>
<td >".$row['No.']."</td>
<td >".$row['NIC']."</td>
<td >".$row['DP']."</td>
<td >".$row['DPTime']."</td>
<td >".$row['Telephone']."</td>
<td> <input type='button' id='load' class='btn btn-success' value='Load number' disabled=' '></td>
</tr>";
}
I also have the following text box on the same web page(outside the above table)!
echo "<input type=text name="display number">"
when i press the load button of the the relevant row of the table i want to display the No. of that row in the text box! how can i achieve this?
There are several problems with the code you posted. I'll do my best to get you on your way... You will want to do what you have described with JavaScript which runs on the client. Many people use libraries these days to work with JavaScript but to avoid added confusion I will recommend looking into jQuery and write my solution in the native code.
while($row=mysqli_fetch_array($result)) {
echo("
<tr id='rowNum".$row['No.']."'>
<td>".$row['No.']."</td>
<td>".$row['NIC']."</td>
<td>".$row['DP']."</td>
<td>".$row['DPTime']."</td>
<td>".$row['Telephone']."</td>
<td>
<input type='button' id='load".$row['No.']."' onClick='javascript:document.getElementById(\"displayNumber\").value=this.parentNode.parentNode.firstChild.innerHTML;' class='btn btn-success' value='Load Number'>
</td>
</tr>"
);
}
One big talking point is that you need to make sure that if you assign an id to any HTML element, unless it is a collection then that ID must be unique, otherwise your HTML will be just as invalid as your id. Also: Don't use spaces in names or ids. Just don't.
Finally, to fix up the input which is outside the table where you want the number to dynamically appear:
echo("<input type='text' name='displayNumber' id='displayNumber'>");

getting a field value in html form without submitting the form

i would like to ask if it is possible to get a value from a html form without submitting the form? i cant get this to work in html or php, or maybe i'm missing something. please help. see my working code below:
<FORM name="myForm" action="confirmsold.php" method="post" onsubmit="return validateFormLay()">
<table border=0 width="550" cellspacing=1 cellpadding=3 bgcolor="#353535" align="center">
<tr>
<td bgcolor="#ffffff">Price Basis:</td>
<td bgcolor="#ffffff">
<?php
$connect = mysqli_connect("localhost","root","ilovegym") or die(mysqli_connect_error());
mysqli_select_db($connect, "laborsa") or die(mysqli_error());
$sql = "SELECT Cash_Price,3months,6months,12months,18months FROM itemmaster where Item_Number='".$_SESSION['ITEM#']."'";
$result = mysqli_query($connect,$sql);
echo "<select name='basis'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Cash_Price'] ."'>" . $row['Cash_Price'] ." - ".CashPrice."</option><option value='" . $row['3months'] ."'>" . $row['3months'] ." - ".ThreeMonths."</option><option value='" . $row['6months'] ."'>" . $row['6months'] ." - ".SixMonths."</option><option value='" . $row['12months'] ."'>" . $row['12months'] ." - ".TwelveMonths."</option><option value='" . $row['18months'] ."'>" . $row['18months'] ." - ".EighteenMonths."</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td bgcolor="#ffffff">Downpayment:</td>
<td bgcolor="#ffffff"><INPUT type="TEXT" name="downpayment" size="12" maxlength="11" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;"></INPUT></td>
</tr>
<tr>
<td bgcolor="#ffffff">Balance:</td>
<td bgcolor="#ffffff"><INPUT type="TEXT" name="laybalance" size="12" maxlength="11" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;"></INPUT></td>
</tr>
as you can see, i need to choose from the drowdown a payment basis, then ill input a downpayment. how can i make the balance field show the balance automatically? i only need to subtract the basis-downpayment, difference will be put in the balance field.
Please help anyone.
Thank you.
Client-Side vs Server-Side
PHP works purely on the server side - you can't get the value of a input element without submitting the form using PHP alone. However, you can do this using Javascript - this runs on the client side, rather than the server side - it is executed on the user's computer, rather than the server. This means it can run while they are on the page. Javascript can get the value of a form element for you.
Javascript Code
Like PHP needs the name attribute to figure out which input field contains what information, Javascript needs the id attribute to get the value of an input field. First, here's the HTML code:
<input type="text" id="myAwesomeIdName">
Now, you need to "get" the element - this returns the HTML element as an object that Javascript can work with. This can be done using the function document.getElementById(id), where id is the id of the HTML element. Example:
document.getElementById("myAwesomeIdName")
All that we need to do now is get the value attribute of the input - this is done like so:
document.getElementById("myAwesomeIdName").value;
And that returns the value of the input box.
Putting it together
Here's some code which pops up an alert displaying the contents of an input box:
<input type="text" id="myAwesomeIdName"><br><button onclick="alert(document.getElementById('myAwesomeIdName').value)">Show Value</button>
You need Javascript for this. Give the three elements (<select>, <input> 1 and <input>2 ) also an id="yourid", is somewhat more comfortable writing your javascript code.
Make your second <input> field read-only as the value is a result of a calculation and not a user input.
Write your function in javascript, and let the function be executed when the downpayment is entered, something like oninput=mycode().
But let the function be executed also when the user changes the <select>, something like onchange=mycode().
The javascript function does this (sorry, no code but procedure..)
get the value of the <select> event the user has chosen
get the value of the first <input> field
subtract those values and write the result in the second read-only <input> field
Some considerations:
better let the first <option> </option> show 'Select your method' or something like that, with a zero value attached to it. Then you can be sure the function will be triggered by the user.
your use of the <input> element is incorrect. It should not have a closing tag. </input> is incorrect html as far as I know.
somewhat beside the question, but <td bgcolor="#fff"> better can be <td style="background:#fff">
When the values are OK the user can submit the form.

No data submitted from a form

I have created a simple HTML form containing just one field. When I press submit some PHP code that I have written gets called and outputs text that would include submitted data if everything was working. But no submitted text gets printed by the PHP. The form has been created on a Godaddy HTML page and the form is as follows:
<FORM BORDER="1" action="http://www.bestpro.com.au/wordpress/PHB_action.php"
method="post" enctype="application/x-www-form-urlencoded"
eenctype="multipart/form-data" name="PHBForm" accept-charset="ISO-8859-1"
ienctype="text/plain">
<TABLE>
<TR>
<TD>First name:</TD><TD><INPUT type="text" name="firstname" id="firstname"></TD>
<TD></TD><TD></TD>
<TD> </TD><TD> </TD>
</TR>
<TR>
<TD> </TD><TD> </TD>
<TD> </TD><TD></TD>
<TD> </TD><TD><input type="submit" value="Submit"></TD>
</TABLE>
</FORM>
The PHP code output starts as follows:
This is where we end up.
Using `$_POST["firstname"]` which outputs nothing.
Using `htmlspecialchars($_POST["firstname"])` which also outputs nothing.
Question:
The PHP output doesn't include the value that I entered into the field.
Can anyone see what I am doing incorrectly?
I see nothing wrong here, so I can only assume it is something wrong with how you output it on your PHB_action.php page.
You say that you're placing $_POST['firstname'] on your page, but have you actually made sure to echo or print it to the page?
You can do this like so:
echo $firstname = $_POST['firstname']; // notice the echo placed before
or
$firstname = $_POST['firstname'];
print("$firstname");
EDIT:
I've notice you have put your post data inside of single quotation marks when echoing out to your page.
You must concatenate on your data rather than putting them inside of single quotes when echoing, like so:
echo 'Using' . $_POST['firstname']; // notice the dot in between the string and the post data.
Either that, or you have not installed PHP correctly (or at all) onto your server.
Hope this helps
So, this is pretty straight forward and I have written it up and will explain each bit as i go.
The PHP you need for this is:
<?php
if (isset($_POST['send']))
{
$fname = $_POST['firstName'];
if (!empty($fname))
{
echo "hello $fname";
} else {
echo "Please supply your first name.";
}
}
?>
$_POST['send'] is the name of your submit button, this will be the trigger for your PHP to initiate and run through the rest of the code.
$fname = $_POST['firstName']
This is just where I prefer to store the $_POST as a variable in the event you are going to re use it again it saves time writing the entire thing.
if(!empty)
if the username isn't empty (!empty meaning not empty) then perform the echo of $fname. however if it comes back as empty it will echo the else echo "please supply...;
Now for the form.
<form action="" method="post">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td><input type="submit" name="send"></td>
</tr>
</table>
</form>
Just a straight forward form with a blank action on mine (I prefer to keep the PHP within the same file however I normally relay it back to a Class within a different file.
Each form input (First Name / Submit) must have a name="" value otherwise the PHP cannot read it and run with it.
I hope this makes sense and isn't too puzzling :)
Your input field should be inside tag and method should be post. Like:
<html>
<body>
<Form method=post>
<input id=mytextfield name=mytextfield type=text />
<input type=submit value=Submit />
</Form>
</body>
</html>

Send "Button"-ID on Submit

I have an PHP page, which contains a form with some different input fields, e. g. day, month, year etc.. The form method is POST, only one non-editable field (The user ID) is sent via GET.
Of course, there is a "Submit"-Button, which triggers the form Action (PHP Script on Server).
The form tags contain a table with empty cells too. Now comes my question:
If the user clicks into one of the table cells, the form should be submitted, but additional to the regular form data the ID of the table cell should be transmitted too (If via POST or GET doesn't matter to me). How can I do that?
//Edit 2:
...
<form method="post" action="<?= DOMAIN?>/.../addUserTimetable.php?uid=<?= $user->getUserID() ?>">
<select id="day" name="day">
...
</select>
...
<input name="yearend" id="yearend" ...>
<button type="submit">...</button>
<table class="bordered">
<tr>
<th>Std.</th>
<th>Montag</th>
<th>Dienstag</th>
<th>Mittwoch</th>
<th>Donnerstag</th>
<th>Freitag</th>
</tr>
<?php
for($i=1; $i<13;$i++) {
echo "<tr>";
echo "<th>".$i. "</th>";
for($j=1;$j<6;$j++) {
echo "<td id='h".$i. "d".$j. "' onclick='???'></td>";
}
echo "</tr>";
}
?>
</table>
</form>
...
The server sided procession is fine, but I haven't got any ideas - even after two hours google - how I could transmit the cell id additionally.
That shouldn't be to hard. Have a look at the following example:
<form>
<input type="text" name="something">
<table>
<tr>
<td><input type="submit" name="cel1">
</tr>
<tr>
<td><input type="submit" name="cel2">
</tr>
<tr>
<td><input type="submit" name="cel13">
</tr>
</table>
<input type="submit" value="save">
</form>
By giving the submit buttons in the table cells a name attribute, that name will also be present as a key on the $_REQUEST. Go ahead and var_dump the $_REQUEST and you'll see you can find out in the backend which button got pushed by checking which key exists.
Note that POST / GET is completely irrelevant here, both will work just the same. And obviously you could apply some css to those buttons to make them transparent and lay them on top of the table cells, so they don't look like buttons, but just "capture" the user's click.
One last side note, are you sure you want to send the userID as a GET parameter? That would be very easy for someone with bad intentions to manipulate. Consider not sending the ID at all, but keeping it in the session on the server.

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>");

Categories