Got a bit of a confusing situation, I have a form on my webpage that allows a user to enter information into a database...
Form
<form id="insertbill">
Total <input type="text" id="total" name="total" /><br />
Bill name<input type="text" id="bill-name" name="bill-name" /><br />
bill descriptiion <input type="text" id="bill-description" name="bill-description" /><br />
bill colour<input type="text" id="bill-colour" name="bill-colour" />
<input type="button" value="submit" onClick="insertBill();" />
</form>
This form then send the information via AJAX to my php which then inputs it into my DB
AJAX
function insertBill()
{
$.post('insert_bill.php', $('#insertbill').serialize(),
function(data) {
$('#bills').append(data);
});
};
PHP
$uid = $_SESSION['oauth_id'];
$bill = mysql_real_escape_string($_POST['total']);
$billname = mysql_real_escape_string($_POST['bill-name']);
$billdescription = mysql_real_escape_string($_POST['bill-description']);
$billcolour = mysql_real_escape_string($_POST['bill-colour']);
#Insert Record
$query = mysql_query("INSERT INTO `outgoings` (user_id, bill, bill_name, bill_description, bill_colour ) VALUES ('$uid', '$bill', '$billname', '$billdescription', '$billcolour')") or die(mysql_error());
Once this is done, the data is then returned to the webpage, with another form that allows the user to update the record...
Returned data/form
Print "<tr>";
Print "<th>total:</th> <td>".$bill . "</td> ";
Print "<th>bill name:</th> <td>".$$billname . "</td> ";
Print "<th>bill deposit:</th> <td>".$billdescription . "</td> ";
Print "<th>colour:</th> <td>". $billcolour . " </td></tr>";
echo "<th>edit:</th> <td>
<form id='bill-upd'>
<input type='hidden' value='". $billname."' name='billid' id='billid''>
Total <input type='text' id='total' name='total' /><br />
Bill name<input type='text' id='bill-name' name='bill-name' /><br />
bill descriptiion <input type='text' id='bill-description' name='bill-description' /><br />
bill colour<input type='text' id='bill-colour' name='bill-colour' />
<input type='button' value='submit' onClick='updateBill();' />
</form>
</td>";
My problem is that the returned form doesn't update, this is because I need to somehow find the id of the record that was inserted, place it into my form to be returned I think... I hope this isn't too confusing, but has anybody a better way of doing this without reloading the page?
here's what what you could include on your, insert_bill.php, view page:
<script>
var json=<?
$arr=array($bill, $billname, $billdescription, $billcolour);
echo json_encode($arr); ?>;
$('tr td:nth-child(0)').html(json[0]);
$('tr td:nth-child(1)').html(json[1]);
$('tr td:nth-child(2)').html(json[2]);
$('tr td:nth-child(3)').html(json[3]);
</script>
I guess the one outstanding issue is the "id" you says gets returned from your database. Can you explain a little further? Are their multiple forms on your insert_bill.php page? Regardless, you could put that $id into the array and feed it into and html() function like I did with the other variables.
You need to return the data from the database and then change, i imagine if you just need the id you must do.
in PHP
echo mysql_last_insert_id(); // so you get the last inserted id and "return" to the JS
In JS, inside the function data
You get the echo as a return and change the id
and then $("#billid").val(data)
I hope it help you
Related
I'm defining POST here:
else if(isset($_POST["Edit"])){
$result_set = $sqlConn->query("SELECT Art, Ime, Cijena, Kol FROM permatable WHERE Art='{$_POST["Art"]}' LIMIT 1");
$Item = $result_set->fetch_array();
$_POST["Art"] = $Item["Art"];
$_POST["Ime"] = $Item["Ime"];
$_POST["Cijena"] = $Item["Cijena"];
$_POST["Kol"] = $Item["Kol"];
header("Location: EditP.php");
}
Now the Post "Edit" and the if statment is being called just fine. I've also checked the query and it returns everything it needs to.
Now on the header location I get undefined index on all of these vars and thats coming from the following code:
echo "<input type='text' placeholder=".$_POST["Art"]." name='Art'>
<input type='text' placeholder=".$_POST["Ime"]. "name='Ime'>
<input type='text' placeholder=".$_POST["Cijena"]." name='Cijena'>
<input type='text' placeholder=".$_POST["Kol"]." name='Kol'>";
Am I doing something wrong? I can't see any mistakes myself and this has been bothering me for a while now.
That won't work.
you can't send POST with header location
you can try to add them to your url
foreach($item as $key=>$value)
{
$items .= $key."&".$value ;
}
header("Location: EditP.php?".$items);
and in the edit.php page
echo "<input type='text' placeholder=".$_GET["Art"]." name='Art'>
<input type='text' placeholder=".$_GET["Ime"]. "name='Ime'>
<input type='text' placeholder=".$_GET["Cijena"]." name='Cijena'>
<input type='text' placeholder=".$_GET["Kol"]." name='Kol'>";
You cannot redirect the user w/ POST variables. It will not work. Read here. The best way to do it is to create a form and then automatically submit it via JavaScript as soon as the page loads. Example:
<form id="my_form" action="http://example.com" method="post">
<input type="hidden" name="name" value="<?php echo htmlspecialchars($name); ?>">
<input type="hidden" name="email" value="<?php echo htmlspecialchars($email); ?>">
<input type="hidden" name="etc" value="<?php echo htmlspecialchars($etc); ?>">
</form>
<script type="text/javascript">
//Our form submission function.
function submitForm() {
document.getElementById('my_form').submit();
}
//Call the function submitForm() as soon as the page has loaded.
window.onload = submitForm;
</script>
$result_set = $sqlConn->query("SELECT Art, Ime, Cijena, Kol FROM permatable WHERE Art='($_POST["Art"])' LIMIT 1");
Try changing curly braces to small braces.
Well, I used SESSION at the end so I just changed the code to following:
$result_set = $sqlConn->query("SELECT Art, Ime, Cijena, Kol FROM permatable WHERE Art='{$_POST["Art"]}' LIMIT 1");
$Item = $result_set->fetch_array();
$_SESSION["Art"] = $Item["Art"];
$_SESSION["Ime"] = $Item["Ime"];
$_SESSION["Cijena"] = $Item["Cijena"];
$_SESSION["Kol"] = $Item["Kol"];
header("Location: EditP.php");
And for the EditP:
echo "<input type='text' placeholder=".$_SESSION["Art"]." name='Art'>
<input type='text' placeholder=".$_SESSION["Ime"]. "name='Ime'>
<input type='text' placeholder=".$_SESSION["Cijena"]." name='Cijena'>
<input type='text' placeholder=".$_SESSION["Kol"]." name='Kol'>";
I am trying to create a table showing multiple users data. The data in the table will then be able to be edited and updated. Below is an example of the way the form is laid out:
echo "<form action=AdminUpdateLecInfo.php method=post>";
while ($return = mysql_fetch_assoc($result)) {
$phonenumber = "$return[PhoneNumber]";
$number = str_pad($phonenumber, 11, "0", STR_PAD_LEFT);
echo " <tr class='data'>
<input type='hidden' name='id'".$return['ID']."'' value= '".$return['ID']."' />
<td class = 'title'><input class = 'title' type='text' name='title'".$return['ID']."'' value= '".$return['Title']."' /></td>
}
echo "</table>
<input class='submit' type='submit' value='Update Info' />
</form>
Once the table is created the information is passed to the 'update.php' script.
$sql="UPDATE completeinfo SET Title='".$_POST['title'][$return['ID']]."'
WHERE ID = '".$_POST['id'][$return['ID']]."'";
header("Location:Home.html");
The problem I'm having is that I need to add the '".$return['ID']."' to the name of each input field so that not all users details are updated with the same values. I am unsure if I need to apply a foreach loop around this query so that it applies to each user and updates their details. Currently however the update query is not working presumably because the post method is not fetching the values from the form correctly.
Your problem is the name of your fields, use that :
$return_id = $return['ID'];
echo "
<tr class='data'>
<td class = 'title'>
<input type='hidden' name='id$return_id' value='$return_id' />
<input class='title' type='text' name='title$return_id' value='$return_id' />
</td>
</tr>";
Before doing your mysql update, do var_dump($_POST);, you'll be shown the content of the HTTP POST parameters so you can see what they are and how to use them in the query.
You have to use the names of your text fields as array so you can iterate your array
name='title'".$return['ID']."'
name='id'".$return['ID']."'
I have a while loop which is display all information correctly, I want a checkbox allowing me to mark each member of the loop as completed in the database, I don't really know where to start with this and I would appreciate any help.
Below is how far I am, it is generating the loop correctly and displaying the checkbox however it is not being set as completed on loading?
while ($row = mysql_fetch_array($query)){
$task_name = $row['task_name'] ;
$task_description = $row['task_description'];
$task_completed = $row['completed'];
$tasks .= '<div id="tasksBody"><form action="" method="post">Completed? <input name="completed" type="checkbox" if ($task_completed == 1){checked="checked"} /><input type="submit" value="Update"><br /><br /><b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';
}
}
Any advice would be greatly appreciated
You cannot write raw PHP code in a string and hope that it's executed. Inline you also cannot use if, but you have to use the ternary operator.
$tasks .= '<div id="tasksBody">
<form action="" method="post">Completed? <input name="completed" type="checkbox" '.
($task_completed == 1?'checked="checked"':'').
' /><input type="submit" value="Update">
<br /><br />
<b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';
I have this code
require_once("../Packages/Connection.php");
$create_object = mysql_query("SELECT * FROM `Articles` WHERE `group` = 'News' ORDER BY `id` DESC;");
while($row=mysql_fetch_array($create_object))
{
$time = $row[time];
$date = date("H:i M jS o ",$time);
print "<form action='Update.php' method='post' float:left;>
<input hidden='hidden' name='articleId' value='$row[id]'>
<input hidden='hidden' name='method' value='update'>
<textarea name='articleText' rows='3' cols='25'>$row[text]</textarea>
<br />
<input type='submit' value=' Update '>
</form><br />
<form action='Update.php' method='post'>
<input hidden='hidden' name='articleId' value='$row[id]'>
<input hidden='hidden' name='method' value='delete'>
<input type='submit' value=' Delete ' onClick='return confirmDelete()'float:left;'>
</form>
<hr><br />";
}
And it outputs the text alright, it changes the new line to <br /> but every time I update, it adds a new, so first time I enter a text like:
Hi
My name is Jesper
it outputs Hi <br />
My name is Jesper to the database
and second time if i want to change something, like the name..
Hi <br /><br />
My name is JapSeyz
and it continues to add <br />'s.. how do I limit this to only one?
That's because you are using nl2br before storing the text to database. Go and see it there...
The right way is to escape the data (e.g., nl2br) only when viewing. The data in the database should be clear, without any modifications regarding escaping for a particular purpose.
In the <textarea> element, though, new-lines are already handled without need to insert <br> elements in there.
So do not use nl2br when storing data and use it only when printing on a page (not in the form element).
I am fix nl2br bug with my own function:
if (!function_exists('snl2br')) {
function snl2br( $input ) {
return preg_replace('~(\r?\n\s?)+?~',"<br>",$input);
}
}
i hope it will help you.
I have a page which displays my database information. Through each loop, there is a checkbox printed that is to delete the associated entry if checked and submitted. The checkbox names are 'delete[]' and there is the hidden value which contains the row id is named 'id[]'.
This is the relevant part of my form:
<tr><td valign='top'>
<label class='amarillo med' style='color:#C00;'>Delete Section </label>
<input type='checkbox' name='delete[]' />
<input type='hidden' name='id[]' value='" . $row['about_id'] . "' />
</td></tr>
This is my php and query
$deleteCount = count($_POST['delete']);
for ($x = 0; $x < $deleteCount; $x++) {
if(isset($_POST['delete'][$x])) {
$sql = 'DELETE FROM about WHERE about_id = \''.$_POST['id'][$x].'\'';
$result = mysql_query($sql);
}
}
This is what happens.
If three rows are returned and I want to delete only the third, I check the third box and click submit. This deletes the first row that was returned. Again, if three rows are returned and I want to delete the first and third, I submit and the first two rows are deleted.
What it looks like is happening is for every checkbox that is checked, that many rows are deleted starting with the first. Any advice would be greatly appreciated.
The best and much easier way is to use
<input type="checkbox" name="delete[]" value="<?=$row['about_id']?>"/>
and then
foreach($_REQUEST['delete'] as $delID)
{
...
}
Use the following code
<tr><td valign='top'>
<label class='amarillo med' style='color:#C00;'>Delete Section </label>
<input type='checkbox' name='delete[$row['id']]' />
<input type='hidden' name='id[$row['id']]' value='" . $row['about_id'] . "' />
</td></tr>
where $row['id'] is the id of the row