How to handle a dynamic table with dojo.query() - php

This is going to be a meaty question because I am not sure the best way to handle this.
I have a page that contains a number of dojo inline editors, to allow users to change values, when one entry had been changes a save button will appear to prompt the user to save the information.
The page has a number of rows, contained within DIV tags, which relate to a row in a database table.
<?php if($this->userjobdetails != null) : ?>
<?php foreach($this->userjobdetails as $employment) :?>
<div id="employ_<?php echo $this->escape($employment['historyid']);?>">
<table class="employment-table">
<tr>
<td><Strong>
<span dojoType="dijit.InlineEditBox" editor="dijit.form.TextBox" onchange="markEmploymentForUpdate();" id="cmpy_<?php echo $this->escape($employment['historyid']);?>"><?php echo $this->escape($employment['employername']);?></span>
</Strong>
</td>
<td align="left"><input dojoType="dijit.form.FilteringSelect" store="rolestore" searchAttr="name" name="role" id="roleInput_<?php echo $this->escape($employment['historyid']); ?>" value="<?php echo $this->escape($employment['jobrole']);?>"></td>
<td align="left">
<span dojoType="dijit.InlineEditBox" editor="dijit.form.TextBox" onchange="markEmploymentForUpdate();" id="jtitle_<?php echo $this->escape($employment['historyid']);?>"><?php echo $this->escape($employment['jobtitle']);?></span>
</td>
<td width="15px;">
<input type="hidden" value="<?php echo $this->escape($employment['historyid']);?>" name="employid" id="employid_<?php echo $this->escape($employment['historyid']);?>"/>
<img src="<?php echo $this->baseUrl();?>/images/site/msg/small/msg-remove-small.png" border="0" onmouseover="this.style.cursor='pointer';" onclick="removeEmployer('emply_<?php echo $this->escape($employment['historyid']);?>')"/>
</td>
</tr>
</table>
</div>
<?php endforeach;?>
When the user 'saves' the page I want to then using dojo.xhrPost post the data for the elements on the page, so that the database rows are updated.
How would I go about this, having multiple 'rows'??
Thanks

Take a look at dijit.form.Form — the second example shows how to validate a form and do whatever actions you like when user submits it. AFAIK, dijit.form.Form doesn't care how many fields it has, and collects them dynamically.

Related

Including .php pages after a form has been submitted

I'm attempting to make a master-detail search page for my intranet that will allow me to search both staff and hardware from the same page. I've gotten part of the way there but I've gotten myself stuck and need some help.
I have the inventory page (index.php) setup and I've included a form with two button - "Staff" and "Hardware" - to include the master-details .php pages I've designed.
The code for this is as follows:
<div class="content">
<h1>Inventory</h1>
<p></p>
<form action="" method="post">
<input type="submit" name="Staff" value="Staff" />
<input type="submit" name="Hardware" value="Hardware" />
</form>
<p></p>
<p></p>
<?php
if (isset($_POST['Staff']))
{
include 'details.php';
include 'lib/staffsearch.php';
};
if (isset($_POST['Hardware']))
{
include 'detailhw.php';
include 'lib/hwsearch';
};
?>
</div>
Again, this works fine - I click "staff" and it loads both my "staffsearch.php" and "details.php" pages. This is where I run into trouble however. My staffsearch.php includes a form that queries my staff database and returns the matching results in a table. I should be able to click on one of the results and the detailed information is displayed in the details box above. What happens though is that I click on the "Staff" button, my includes are displayed, I enter the search criteria, click "search" and get a blank page as a result.
If I click on the "staff" button again though my includes are reloaded along with the correct search result. Accessing the details however results in the same problem - once my results are displayed and I click on one instead of the details I receive a blank page until I click the "staff" button again - then my includes are reloaded and my results are displayed.
Is there anyway that when I click the search button on my staffsearch form that I can force the index page to reload the includes without having to click the "staff" button again?
Thanks, ahead of time for any advice as I'll throw in the mandatory. "I'm new to php" and my Googling hasn't really netted me any good results. If you need anymore information just ask!
EDIT 1
The code for my searchstaff.php page is as follows:
<table width="598px" border="1" align="center">
<tr valign="middle">
<td alight="left" colspan="3"> <form id="form1" name="form1" method="get" action="">
<label for="name">Search:</label>
<input type="text" name="name" id="name" /><input type="submit" name="search" id="search" value="Submit" style="margin-left:10px";/>
</form>
</td>
<tr>
<td align="center" width="40px">No.</td>
<td width="300px">Name</td>
<td width="158px">IP Address</td>
</tr>
<?php do { ?>
<tr>
<td align="center"><?php echo $row_inventorystaff['staff_id']; ?><?php echo $row_DetailRS1['staff_id']; ?></td>
<td> <?php echo $row_inventorystaff['f_name']; ?> <?php echo $row_inventorystaff['l_name']; ?><?php echo $row_DetailRS1['f_name']; ?> <?php echo $row_DetailRS1['l_name']; ?></td>
<td><?php echo $row_inventorystaff['ip_address']; ?><?php echo $row_DetailRS1['ip_address']; ?></td>
</tr>
<?php } while ($row_inventorystaff = mysql_fetch_assoc($inventorystaff)); ?>
</table>
<br />
<table border="0" style="margin-left:15px;" >
<tr>
<td><?php if ($pageNum_inventorystaff > 0) { // Show if not first page ?>
First
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_inventorystaff > 0) { // Show if not first page ?>
Previous
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_inventorystaff < $totalPages_inventorystaff) { // Show if not last page ?>
Next
<?php } // Show if not last page ?></td>
<td><?php if ($pageNum_inventorystaff < $totalPages_inventorystaff) { // Show if not last page ?>
Last
<?php } // Show if not last page ?></td>
</tr>
</table>
<p style="margin_left:15px">Records <?php echo ($startRow_inventorystaff + 1) ?> to <?php echo min($startRow_inventorystaff + $maxRows_inventorystaff, $totalRows_inventorystaff) ?> of <?php echo $totalRows_inventorystaff ?>
</p>
I have screen shots of the process of clicking through the index and performing a search that I can upload if it helps the explanation.
I would use a radio button to make a decision which will be easier to parse. Double submit buttons are a funny thing and some browsers dont like that.
<input name="dept" value="staff" type="radio"> Staff
<input name="dept" value="hardware" type="radio"> Hardware
<?php
if( isset($_POST['dept'] && $_POST['dept'] == 'staff' ) {
include 'staffForm.php';
} else {
include 'hardwareForm.php';
}
?>

Catching data from multiple checkboxes in a POST request

Many thanks for checking this question. I have a simple email system on my website between users. Below is a section of code which is a foreach loop pulling out each email in a users inbox and displaying the subject and author etc. I have a delete checkbox to go with each one. I am struggling to see how I get catch the emails that have been selected for deletion in a POST request. I am thinking that it probably involves an array but not sure. All checked boxes have the same value of 'delete' so it should be easy enough, but I can't find a solution
<!-- LOOPING THROUGH ALL THE RESPONSES TO THE GIVEN THREAD -->
<?php foreach($messages as $message):?>
< div class="message_strip">
<table>
<tr>
<td>
<?php $id = $message->sender_id;?>
<img style="width:30px; margin:2px;"
src="../<?php echo grab_thread_thumbnail($message->sender_id); ?>"/>
</td>
<td>
<div>
<?php echo $message->style_email($message->message_id);?>
<div class="sender">
<?php $user=User::find_by_id($message->sender_id);
echo htmlentities($user->first_name);?>&nbspwrote on
<?php echo datetime_to_text($message->time); ?>
</div>
</td>
<form action="message_folder.php" method="post">
<td class="delete_checkbox">
<input type="checkbox" class="cb-element" name="delete" value="delete">
</td>
</form>
</tr>
</table>
</div>
</a>
<?php endforeach; ?>
Don't wrap the checkbox input in the form tags. Wrap your entire foreach in the form tags, and you'll need a submit button.
Assuming you want the message_id:
<input type="checkbox" class="cb-element" name="delete[]" value="<?=$message->message_id ?>">
Then you will have an array of message_ids that were checked in $_POST['delete'].
You need to give each checkbox a unique name, e.g. name='delete[$message->sender_id]', then iterate over the POST'ed array.

issue when inserting data in DB using submit button

i define below codes to show my products on website. I fetch my products details from Database.
When i click on the enquiry submit button it is not selecting the same id and inserting another product id in to database. Please see below codes which i use to send details in database using enquiry Submit Button
Please see below the updated codes, these are all codes which i am using to inserting data when clicking on enquiry submit button
<?php
session_start();
include'database.php';
$userid=$_SESSION['userid'];
$c=mysql_query("select 'productid','productprice' from products order by rand()");
while(list($productid,$productprice)=mysql_fetch_array($c)):
?>
<td align="center">
<table class="newproducttd" align="center">
<tr>
<td class="code" align="center"><?php echo $productid; ?></td>
<td class="price" align="center"><?php echo $productprice; ?></td>
</tr>
<tr>
<td class="button" align="center"><input type="submit" class="enquiry" name="enquiry" value="ENQUIRY" /></td>
</tr>
</table>
</td><br>
<?php
endwhile;
if($_REQUEST['enquiry'])
{
mysql_query("insert into orders values('$userid','$productid','$productprice')");
}
?>
Your table in your mysql database probably has the columns in a different order. To make sure, specify the columns in your insert query before specifying the values.
You're getting the wrong value for $productid because it's assigning the last value of it in your while loop, as your insertion code (and therefore your reference to it) is below that loop, you want to send the product id and price to the page accessible by $_REQUEST when the user clicks submit, to do this in your case you can use hidden form fields to store the values of each product, and a separate form for each element in your loop.
Also you want to put your submission code above your loop.
Depending where this data comes from you might want to escape it before inserting it in the database, I haven't in this example.
<?php
session_start();
include'database.php';
$userid=$_SESSION['userid'];
//check for submission and insert if set
if($_REQUEST['enquiry'])
{
//use the userid session for userid, and submitted values for productid and productprice
mysql_query("insert into orders values('$userid,'{$_REQUEST['productid']}','{$_REQUEST['productprice']}')");
}
$c=mysql_query("select 'productid','productprice' from products order by rand()");
while(list($productid,$productprice)=mysql_fetch_array($c)):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<td align="center">
<table class="newproducttd" align="center">
<tr>
<td class="code" align="center"><?php echo $productid; ?></td>
<td class="price" align="center"><?php echo $productprice; ?></td>
</tr>
<tr>
<td class="button" align="center"><input type="submit" class="enquiry" name="enquiry" value="ENQUIRY" /></td>
//hidden form fields to store data to send to page
<input type="hidden" name="productid" value="<?php echo $productid;?>">
<input type="hidden" name="productprice" value="<?php echo $productprice;?>">
</tr>
</table>
</form>
<?php
endwhile;
?>

Is POST information from jquery $.post sent in parent page

I have a parent page which has a drop down list in it. using the onchange event, data is posted to a second page using $.post(). This page uses the posted variables to output mysql data with a checkbox for each line. This page output is then inserted into the parent page using jquery $('#DIV name').html(output).show();
The user can then see the mysql table rows with corresponding checkboxes. They then select the checkboxes they want and say delete. Delete is a form submit button.
My question is, when they click delete how do I take the form data from the second page and post it with that of the parent page so that I can then use $_POST[] to get the checkbox info and delete the selected table rows?
example of the parent page code is:
javascript/jquery
<script type="text/javascript">
function get(row){ //row being processed, defined in onchange="get(x)"
('getpeopleinjobs.php',{ //data posted to external page
postvarposition: form["position"+row].value, //variable equal to input box, sent to external page
postvarjob: form["job"+row].value, //variable equal to input box, sent to external page
postvarperson: form["person"+row].value, //variable equal to drop down list, sent to external page
postrow: row}, //variable equal row being processed, sent to external page
function(output){
$('#training'+row).html(output).show(); //display external results in row div
//popupWindow = window.open('t4.php?variable1Name=Vicki&variable2Name=Maulline','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
});
}
</script>
Form data is
<tr>
<td>
<?PHP echo $i; ?>
</td>
<td>
<input type=text NAME="position<?PHP echo $i; ?>" id="position<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,0);?>">
</td>
<td>
<input type=text NAME="job<?PHP echo $i; ?>" id="job<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,1);?>">
</td>
<td>
<SELECT NAME="person<?PHP echo $i; ?>" id="person<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" onchange="get(<? echo $i; ?>);">
<OPTION VALUE=0 >
<?=$optionpeople?>
</SELECT>
</td>
<td onclick="train(<? echo $i; ?>);" style="color:grey; cursor: pointer;">
<div id="training<?PHP echo $i; ?>"><font color=grey size=2></div>
</td>
<td>
</td>
</tr>
<?PHP
$i++;
$r++;
}
?>
The second page or page called by jquery, the output is:
echo
"
<table border=0 width=400>
<tr>
<td width=20>
</td>
<td width=150>
<b>Position<b>
</td>
<td>
<b>Job<b>
</td>
</tr>
";
while($line = mysql_fetch_array($result))
{
echo "
<tr>
<td>
";
?>
<input type=checkbox name="delete[]" id="delete[]" value="<?php echo $rows['id']; ?>">
<?PHP
echo "
</td>
<td>
";
echo $line['position'];
echo "
</td>
<td>
";
echo $line['job'];
echo "
</td>
</tr>
";
}
?>
<tr>
<td>
<input type=submit name="update" id="update" value="Update">
</td>
</tr>
</table>
<?PHP
}
To repeat I want the table checkbox element from the second page posted with the form data of the parent page.
Is this possible as my testing hasnt given me any luck.
Am I doing something wrong or do I need to modify the jquery code?
Thanks as always for the assistance.
There is no second page. Really - you're loading HTML content from the second page, but it's being inserted into the parent page. All content, from your browsers perspective, is in the same page. The fields should be included as long as they're inside the DOM inside the element. Use the developer tools for your browser or Firebug for Firefox to make sure that the content is placed within the form element in the DOM. The developer tools should also be able to show you exactly which variables are submitted to the server when the form is submitted.
The 'action' parameter of 'form' will indicate where the content gets submitted (and it seems you've left that part out of your HTML, so it's impossible to say if you've left out or just not included it in the paste.

process hidden field with jquery and calculate

I have an html table I've updated to use checkboxes to be able to delete multiple files:
<table>
<thead>
<tr>
<th>Camera Name</th>
<th>Date Created</th>
<th>Video Size</th>
<th>Video Length</th>
<th>
<button type="submit" class="deletebutton" name="delete_video" value="Delete" title="Delete the selected videos" onClick="return confirm('Are you sure you want to delete?')">Delete</button><br>
<input type="checkbox" name="radioselectall" title="Select All" />
</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num_videos;$i++)
{
//do stuff
//Note: I'm looping here to build the table from the server
?>
<tr >
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo setlocalTime($result_videos[$i]["video_datetime"]); ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo ByteSize($result_videos[$i]["video_size"]); ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo strTime($result_videos[$i]["video_length"]); ?>
</td>
<td>
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<input type="checkbox" name="radioselect" title="Mark this video for deletion"/>
<input type="hidden" name="video_name" value="<?php echo $result_videos[$i]["video_name"]; ?>" />
</form>
</td>
</tr>
I started with first creating some jquery code to create a select all/deselect all button in the table heading and just a test to show I can find which boxes are checked. That all works:
//selectall checkboxes - select all or deselect all if top checkbox is marked in table header
$("input[name='radioselectall']").change(function()
{
if( $(this).is(':checked') )
{
$("input[type='checkbox']","td").attr('checked',true);
}
else
{
$("input[type='checkbox']","td").attr('checked',false);
}
});
//process checkboxes - which ones are on
$(".deletebutton").click(function() {
$("input[name='radioselect']:checked").each(function(i){
alert(this.value);
});
});
So the part where I'm stuck is I don't know where to go from here. I need to pass all the video_names of all the videos selected (with the checkboxes). video_name is part of a hidden field in my form. So I need to pass that to my php function when the delete button is selected. Not really sure how to tackle this. Hope this makes sense.
Simple solution maybe:
Change your checkboxes to have a value of 1 and a name of $result_videos[$i]["video_name"]; in the table rows, make the form encapsulate the whole table.
Then on submit you can do something like:
foreach ($_POST as $key => $value) {
//Delete $key (the name) where $value == 1
}
Your approach is fine if you want to access the hidden fields through jQuery at a given point but once you submit the form and lose the DOM, the PHP processing page will have no way to relate the selected checkboxes with the hidden fields as there is no consistent naming scheme.
One approach would be to use the loop counter to suffix the two in order to pair them up:
<input type="checkbox" id="radioselect_<?= $i ?>" title="Mark this video for deletion" value="1" />
<input type="hidden" id="video_name_<?= $i ?>" value="<?php echo $result_videos[$i]["video_name"]; ?>" />
This would be good if you want to relate more than the two fields. Otherwise, you could just use the video_name as the value of the checkbox, as Ing suggested.

Categories