I have a list of products that I wish to be editable. When a user hits the edit button, then the content of only the selected product needs to be changed (for example to a textbox so the user can edit the title on the fly). But how do I prevent php to echo for example a textbox to all the products- I guess it would do that automatically?
I also guess that i should use some Jquery stuff to make the content editable :P ?
The list is being looped like this:
$items = $mysqli->query("SELECT product_name, product_id FROM products");
while($products = $items->fetch_assoc(){
echo $products['product_name'];
echo 'Edit me';
}
As your first commenter pointed out, PHP alone is not enough here. You'll need on-page JS code that can communicate the changes in the browser, and a PHP script that can take those changes and work them back into the database. You can either write that yourself, or use proven libraries that exist specifically for this purpose, like http://backbonejs.org/ or http://angularjs.org/
These are model/view frameworks that let you show a view of your database data on a page, while keeping them editable, updating the database records when you update the entry online. But be warned: if you've never worked with MVC frameworks, you get to look forward to probably being very confused at first. The approach is completely different from the much simpler "get data from db with PHP, generate page content, send off to client, the end" approach.
Not necessarily the most efficient, but if there aren't a huge number of products how about including a simple form for each product but just hiding it until the 'Edit' link is clicked?
The list/forms:
$items = $mysqli->query("SELECT product_name, product_id FROM products");
while($products = $items->fetch_assoc(){
echo "<span>" . $products['product_name'] . "</span>";
echo "<a class='editButton'>Edit</a>";
echo "<form action='products.php' method='post' style='display: none;'>
<input type='hidden' name='product' value='" . $products['prodcut_id'] . "' >
<input type='text' name='title' value='" . $products['product_name'] . "' >
<input type='submit' value='Update' >
</form>";
echo "<br/>";
}
The jQuery:
$(".editButton").click(function(){
//Hide the text entry and the edit link
$(this).prev().hide();
$(this).hide();
//Show the form
$(this).next().show();
});
If you'd rather not reload the page to submit changes you could submit them via ajax too for a more dynamic user experience.
Related
I have to make an online shop.
I generate my products from a database like this:
$sql = "SELECT * FROM wblouses";
$result = mysql_query($sql);
if (! $result)
{
echo "eroare db database-item";
}
else
while($db_field = mysql_fetch_assoc($result)) {
echo "<div class=\"col-md-4\"><div class=\"thumbnail\"><form method=\"POST\" action=\"addToChart.php\" ><img style=\"display: block;\" src=";
echo $db_field["poza"]." height=\"250\" width=\" 400 \"> <p class=\"prices\">
<span class=\"price\" data-color=\"401\" >
<span class=\"currentPrice\"> ".$db_field["pret"]." LEI </span>
</span>
<input type=\"username\" name=\"username\">
<select id=\"myselect\" name=\"myselect\">
<option value=".$db_field["ID"].">SIZE</option>
<option value=\"XS\">XS</option>
<option value=\"S\">S</option>
<option value=\"M\">M</option>
<option value=\"L\">L</option>
</select>
<input type=\"submit\" value=\"add to chart\">
</p></form><button id=\"ilas\" onClick=\"fct(this.id)\">B3</button></div></div> ";
}
In another .php file I manage a database which represents a list of items selected from the one posted. How do I pass the id of a particular item to another file, I only seem to get the last generated id.
How can I pass some arguments to another php file?
The problem is that every product has it`s own generated div,dropdown and button with the same name and ID , how I find out in .php which product I refer to(it always refer to the last added product)?
#Skrrp
#TEster
Simple answer, you can't pass both bits of data from the same select.
You could do some funky concatenation, such as;
<option value=\"" . $db_field["ID"] . ";XS\">XS</option>
and then do a string spilt on the other side. This is silly. Don't do this.
If you have multiple products that you want the user to select from, what you want is 2 select drop-downs, one for product and one for size. If certain products are available only in certain sizes you will need some JavaScript to sort out the second.
What you are probably looking for is a hidden data field. It looks like you have already selected the product (and its ID) by the stage you generate this form. Rather than putting the $db_field["ID"] in the select, put it in its own control.
<input type=\"hidden\" name=\"id\" value=\"" . $db_field["ID"] . "\">
Then when you get to your next page, $_POST["id"] will contain the product ID you need.
As the above guys said,
I recommend using a hidden field to store the value.
$id = $db_field["ID"];
<input type='hidden' name='id' value='$id'>
Then just collect it using $_POST['id'] on the other php script.
I want to display a catalog view of students with php. To be more precisely I have the web page in which I have used jQuery, Ajax to load some parts of the page. What I want to do is to to display a list of students like a catalog with their information in which every item of the list has included first name ,last name, username etc. Also, this item must contain a button. Once the button is clicked the list view should be dismissed and another view with full details of the students should be loaded in the same area.
#Vainglory07 of course I have tried a lot and did not find out any solution. Ok I am posting my code but it is very basic:
<script type="text/javascript">
$(function(){
$("#$data").bind('click', function(){
window.alert("It works");
}
)}
);
</script>
Here is the code in php:
$data=mysql_query("SELECT * FROM perdoruesi WHERE id>2 ");
while($db_field=mysql_fetch_array($data)){
echo "<tr><td>";
echo $db_field['emri']. " ";
echo $db_field['mbiemri']. " ";
echo $db_field['username']. " ";
echo $db_field['passw']. " ";
$data = $db_field['username'];
echo "<button id = '$data'>".$data."</button>";
echo "</td></tr>";
}
when I click the button I want to execute the script but it does nothing. Many thanks for your help.
To match $('#data') you should output id='data':
echo "<button id='data'>".$data."</button>";
This code: id='$data' gives each button a different id (which is probably not what you want)
I want to get details from one site. That web page is having 3 different select box:
1) Choose Branch
2) Choose Semester
3) Choose Exam Year and then click on show button. This site is using AJAX to show table(output).
I tried with HTML dom parser but don't know the parameters to be passed with that. How to automize such thing which works(submit) with AJAX?
My code is:
<?php
include('simple_html_dom.php');
$html = file_get_html("http://www.abc.com/Engineering-Degree/ExamPapers/ExamPapers.aspx");
foreach($html->find('select[id=Branch]') as $branchSelect)
{
echo $branchSelect;
foreach($html->find('select[id=Semester]') as $semSelect)
{
echo $semSelect;
foreach($html->find('select[id=Exam]') as $examSelect)
{
echo $examSelect;
echo "<input type='submit' value='Show' id='BranchSemesterExamBtn'/>";
}
}
}
?>
Those selects are using get method to retrieve data maybe. Goto you browser console(F12) and change those select options. You may get the links there.
I have researched many places to find an answer to this question, but they never quite answer my real question: What is the best/approved way to move to a new page within the same website? I have read that it is bad to use window.location because search engines will think you are hiding something. But, when I don't want to open a new window (window.open), then I don't know how else to do it. I use href anchors in links and form actions, where appropriate. But when I have menus or buttons with onclick, then I need something else.
Here's an snippet of my code:
my javascript: (with one option commented)
function gotoCat() {
var catcdF = document.catSelect.catcd.value;
<?php
echo "window.location.href='http://www.mysite.org".$pgmdir."services/busMenu.php?catF='+catcdF; ";
/*
echo "window.open('http://www.mysite.org".$pgmdir."services/busMenu.php?catF='+catcdF,'','resizable=1,scrollbars=1,toolbar=1,top=50,left=300,width=950,height=800,location=0'); ";
*/
?>
}
My dynamic SELECT list in a form (within PHP):
echo " <select name='catcd' id='catcd' size='8' onclick=gotoCat() > \n";
// display list of categories
if ($numcats == 0) { // print message text only
echo "<option value='0' >".$catMsg."</option> \n";
}
else {
for ($i=1; $i<=$numcats; $i++) {
$catcd_db = $catAry[$i][1];
$catName_db = $catAry[$i][2];
echo "<option value='".$catcd_db."'> ".$catName_db." </option> \n";
}
}
echo "</select>";
So, as you can see, I just want a method to allow the user a choice and then automatically go to the correct web page once selected. This is not always in a select list. Often it's when they want to exit or get an error:
if (mysqli_connect_errno()) {
echo "<br/> <p style='text-align:center;'> <button type='button'
class='buttonStyle' style='padding: 4px 20px;' value='Exit' ";
echo "onClick=\"window.location.href='http://www.mysite.org/services/catSelbus.php?rc=1&func=Rev'\" > ";
echo "Exit </button></p> ";
}
I cannot use "go back" because they need to go to a prior page, not the form they came from.
So, unless my navigation methods are really off-the-mark, I guess I need to know the acceptable method for using javascript onClick to move to the next page in the same website. Is window.location okay, or should I use something else?
Any opinions or suggestions are welcome!
To navigate to another page using Javascript, use:
window.location.href = "url";
That's how it's done and there's nothing wrong about it.
For the sake of argument, you could create a hidden link and simulate a click on it, but as I said, there's really no need.
You can use php header('location') instead:
<form action="submit.php">
<input type="hidden" value="test" name="hidden1" />
<input type="submit" Value="Exit" ... />
submit.php
<?php
if (isset($_POST['hidden1'])
{
header('Location: http://www.mysite.org/services/catSelbus.php?rc=1&func=Rev');
exit;
}
?>
More info about header('Location ...');:
http://php.net/manual/en/function.header.php
Instead of a hidden, you use your select's value and get it via the $_POST variable.
Hi i want to get changed text value from JQuery but i can't select edit text with JQUERY because edit texts generated from php while loop that this php code query on database and get value of edit texts and in my program i have edit button for every edit texts and when the user changed value of edit text i select new value and when user click edit button send this value from get method to another php page with jquery $.ajax function and send new value to that php code with ajax.But i don't know how can i select edit text that it's value changed because i don't know id of that edit text!.And when i set one id for every edit text i only get first edit text value from $("#id").change().val();.I use below code but it doesn't work.I am beginner in java script and don't know how fix this problem!.
var testAnswer;
function setNewTestAnswer(id){
testAnswer = $("#id").val();
}
function sendToEdit(pID,phID,thDate,type){
var info = 'pId='+pID+'&phId='+phID+'&testAnswer='+testAnswer+'&thDate='+thDate+'&type='+type;
}
2nd function use testAnswer that user changed in edit text.
php code
<?php
include 'Connect.php';
if(match($_POST['pId'], "/^[\d]+$/") ){
$pId = $_POST['pId'];
$result = mysql_query("select pName, pID, phName, phID, testHistoryDate, type, testAnswer from patient join reception using(pID) join physician using(phID) join testHistory using(rID) join test using(tID) where pID = $pId",$connection);
}
else
die("Insert true value");
while($row=mysql_fetch_array($result)){
echo "<tr><td>";
echo $row["pName"].'</td>';
echo '<td>'.$row["phName"].'</td>';
echo '<td>'.$row["testHistoryDate"].'</td>';
echo '<td>'.$row["type"].'</td>';
$type = $row['type'];
$testHistoryDate = $row['testHistoryDate'];
?>
<td>
<span id='spryTanswer'>
<input type='text' name='tAnswer' id='tAnswer' value='<?php echo $row['testAnswer']; ?>' />
</span>
</td>
<td>
<input type='submit' value='Edit' name='edit' id='edit' onclick="sendToEdit('<?php echo $row['pID'] ?>','<?php echo $row['phID'] ?>', '<?php echo $row['testHistoryDate'] ?>', '<?php echo $row['type'] ?>')" />
</td>
</tr>
<?php } ?>
tl;dr
So it isn't completely clear what you are trying to do here but I can explain a couple things that might help.
in html ids should be unique. You dont have to obey this rule for your page to work but you have found one of the consequences if breaking it: jQuery will only find the first one.
it is a good idea to base html ids on some unique attribute of your data eg the table row id.
You can get more creative with your jQuery selectors for example
$('input[type="text"]') // gets all the text inputs
Use classes. When you want to be able to easily select all of a group of html elements you should give them all the same class name. one element can have multiple class names and many elements can share a class name. you can then select them by class name using jquery like this:
$('.myclassname')
I think you need to change your php to look more like this:
<span class='spryTanswer'>
<input type='text' name='tAnswer' id='tAnswer-<?php echo $row['id'] ?>' value='<?php echo $row['testAnswer']; ?>' />
</span>
Since you're creating elements inside a php loop, you must be sure that every element has a unique id (or no id at all). You can use either an incrementing index, or some unique value in your array. At first glance, seems that $row['pID'] is a good candidate:
<input id='edit_<?php $row['pID'] ?>' type='submit' value='Edit' ... />
After that you should be able to target individual elements.