Sticky form undefined offset - php

I have a 5x2 HTML table: http://jsfiddle.net/duxTU/
The table is enclosed in a form.
When the form is submitted it redirects to same page
The input field values are stored in an associative array
The values are displayed in respective fields where they were initially entered.
Code:
<?php
if(isset($_POST['submit'])) {
$contacts_array array(
$_POST["name1"] => $_POST['name2'],
$_POST["name3"] => $_POST['name4'],
$_POST["name5"] => $_POST['name6'],
$_POST["name7"] => $_POST['name8'],
$_POST["name9"] => $_POST['name10']);
$array_filtered=array_filter($contacts_array);
$arrayKeys = array_keys($array_filtered);
$arrayValues = array_values($array_filtered);
}
?>
The array has been filtered to omit null values that may creep in.
Now, how do I make the the HTML form display values? I know about sticky forms but since i must keep the form size constant(5x2), I am trying to use code similar to following
<input type="text" value="<?php echo $arrayKeys[0]; ?> ">
<input type="text" value="<?php echo $arrayValues[0]; ?> ">
... till $arrayKeys[4] and $arrayValues[4], respectively.
This gives me undefined offsets, for example $arrayKeys[5] was not set as no value was entered in the respective form column before submission.
Any solution to this issue??

Maybe this would work for you:
<?php
function getPost($param){
return isset($_POST[$param])?$_POST[$param]:"";
}
?>
<form action="#" method="post">
<table>
<tr>
<td>Name</td><td>Age</td>
</tr>
<tr>
<td><input type="text" name="name1" value="<?= getPost("name1") ?>"></td>
<td><input type="text" name="name2" value="<?= getPost("name2") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name3" value="<?= getPost("name3") ?>"></td>
<td><input type="text" name="name4" value="<?= getPost("name4") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name5" value="<?= getPost("name5") ?>"></td>
<td><input type="text" name="name6" value="<?= getPost("name6") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name7" value="<?= getPost("name7") ?>"></td>
<td><input type="text" name="name8" value="<?= getPost("name8") ?>"></td>
</tr><tr>
<td><input type="text" name="name9" value="<?= getPost("name9") ?>"></td>
<td><input type="text" name="name10" value="<?= getPost("name10") ?>"></td>
</tr>
<tr>
<td><input type="submit" value="submit" name="submit"></td>
</tr>
</table>
</form>
The getPost helper was created because I didn't want to have to type the conditional every time.
You can filter out the nulls for whatever other form processing you are doing.

If you just want to display values, a foreach would work great:
foreach ($array_filtered as $k=>$v) {
echo "<input type='text' value='".$k."'>";
echo "<input type='text' value='".$v."'>";
}
foreach loops through an array. $k is the current key. $v is the current value.
Of course, this should be inside of your form HTML.

Related

unserialize(): Error at offset 9 of 13 bytes

I am getting this error? What does it mean?
unserialize(): Error at offset 9 of 13 bytes
This is what I get after I serialize and store the array in database:
a:3:{i:0;s:6:
And at the time of unserialize, it gives the error! What should I do?
I want my original array back and want to display it.
I have tried other post, like basencoder/decode, but thats not working too..
Create1.php(passing serialized array to next page through hidden input)
<form action="create2.php" method="POST">
<table cellpadding="10">
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $name; ?>" readonly="readonly" ></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="mobile" value="<?php echo $mobile; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $email; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Company</td>
<td><input type="text" name="company" value="<?php echo $company; ?>" readonly="readonly" /></td>
</tr>
<tr>
<input type="hidden" name="original_list" value="<?php echo serialize($original_list); ?>" />
<input type="hidden" name="xerox_list" value="<?php echo serialize($xerox_list); ?>" />
</tr>
<tr>
<td><input type="submit" value="Confirm" /></td>
</tr>
</table>
</form>
Create2.php(getting and storing serialized array in database)
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$company = $_POST['company'];
$original = $_POST['original_list'];
$xerox = $_POST['xerox_list'];
echo $sql = "INSERT INTO users (name,mobile,email,company,original,xerox)
VALUES ('$name','$mobile','$email','$company','$original','$xerox')";
mysqli_query($con,$sql);
I'm not sure it's safe to use binary strings in HTML (HTTP is a text protocol after all). I suggest using JSON encoding instead of serialization.
<input type="hidden" name="original_list" value="<?php echo json_encode($original_list); ?>" />
<input type="hidden" name="xerox_list" value="<?php echo json_encode($xerox_list); ?>" />
When accessing the entries you can then do:
json_decode($row['original'],true); //you get the idea

html dynamic table print to php

I need some help...
I've created a form with a dynamic table in it. I would like to learn how to pass it through php and echo/print it to a php page to save the page as pdf.
I am stuck on this part. I've looked everywhere for some answers and I cant seem to be finding anything.
<form action="display_form.php" method="POST">
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
<tr>
<td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
<td><INPUT type="text" name="step" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
<td><INPUT type="text" name="url" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
<td><INPUT type="text" name="process" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
<td>
<SELECT name="pass-fail" style="width:100px;">
<OPTION value="Pass">one</OPTION>
<OPTION value="Fail">two</OPTION>
</SELECT>
</td>
<td><INPUT type="text" name="comment" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
</table>
<INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />
<INPUT type="submit" value="Send"/>
I have called the php file display_form.php where I would like to display the dynamic table data in the. The function to save it as a pdf is in there as well. But I am mainly after the printing of the form to the php file. I am having no luck there at all!
I'm not worried on posting the table to mysql. I have other ways where i don't need it in mysql
I would really appreciate the help
2 things
you have not closed tr
Next
Make all input elements as array. Then you can fetch them in your php file
<td style="width:20px;"><INPUT type="checkbox" name="chk[]" /></td>
<td><INPUT type="text" name="step[]" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
<td><INPUT type="text" name="url[]" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
<td><INPUT type="text" name="process[]" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
<td>
<SELECT name="pass-fail[]" style="width:100px;">
<OPTION value="Pass">one</OPTION>
<OPTION value="Fail">two</OPTION>
</SELECT>
</td>
<td><INPUT type="text" name="comment[]" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
In your php file, use foreach loop to get all the values.
$i = 0;
$array = array();
foreach ($_POST['step'] as $row) {
$array[$i]['step'] = $row;
$array[$i]['chk'] = isset($_POST['ckh'][$i]) ? 1 : 0;
$array[$i]['url'] = $_POST['url'][$i];
$i++;
//...//so on
}
print_R($array);
As I can see, the javascript addRow function should add another tr-row in the table (the closing is missing by the way).
Now you have in every table row inputs with the same names. That won't work.
You need to say, that they are arrays, to access them via $_POST['step'][0] and so on.
Just give them name="step[]" and the form will automatically count the index for you.
Use name='step[]',name='url[]',name='process[]' as input type for your add row functionality,
Now when you submit form you are getting array then store that array as below.
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
Now use foreach loop.
foreach($step as $key => $row){
echo $step[$key];
echo $url[$key];
echo $process[$key];
}
Now format your data in html then generate pdf as per below link.
http://phptopdf.com/
if (isset($_POST)) {
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
$pass_fail = $_REQUEST['pass-fail'];
$comment = $_REQUEST['comment'];
foreach ($step as $key => $row) {
{
?>
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
<tr>
<td><INPUT type="text" name="step[]" style="width:160px;" value="<?php echo $step[$key]; ?>"/></td>
<td><INPUT type="text" name="url[]" style="width:62px;" value="<?php echo $url[$key]; ?>"/></td>
<td><INPUT type="text" name="process[]" style="width:63px;" value="<?php echo $process[$key]; ?>"/>
</td>
<td><input name="pass-fail[]" style="width:100px;" value="<?php echo $pass_fail[$key]; ?>"/></td>
<td><INPUT type="text" name="comment[]" style="width:190px;" value="<?php echo $comment[$key]; ?>"/>
</td>
</tr>
</table>
<?php }
}
}
Working just fine now!!
here the table is displaying correctly

PHP MySQL invoice materials

I'm having some troubles getting this code to work properly. Our employees has access to our backend system on our website. This is how the input fields and such are set up:
<?
$materialer_query = $db->query("SELECT faktura_materialer.id as materialer_id, faktura.*, faktura_materialer.* FROM faktura, faktura_materialer WHERE faktura_materialer.faktura_id = faktura.id");
while($mat = $materialer_query->fetch_object()){
?>
<tr>
<td><input style="width:450px;" class="text-input" type="text" name="beskrivelse_nu" value="<? print $mat->beskrivelse; ?>"></td>
<td><input style="width:60px;" class="text-input" type="text" name="enhed_nu" value="<? print $mat->enhed; ?>"></td>
<td><input style="width:30px;" class="text-input" type="text" name="stk_nu" value="<? print $mat->stk; ?>"></td>
<td><input style="width:205px;" class="text-input" type="text" name="vejl_eks_moms_nu" value="<? print number_format($mat->vejl_eks_moms,2,",","."); ?>"></td>
<td></td>
<td><img src="images/icons/slet.gif" width="16" alt="Slet" /></td>
<input hidden name="materiale_id" value="<? print $mat->materialer_id; ?>">
</tr>
<?
}
?>
I want to be able to update every material in one page. I've tried to mess around with this, even added a hidden input field in the while loop, which contains every material id. And this is how I currently update the table:
$beskrivelse_nu = addslashes($_POST['beskrivelse_nu']);
$enhed_nu = addslashes($_POST['enhed_nu']);
$stk_nu = addslashes($_POST['stk_nu']);
$vejl_eks_moms_nu = addslashes($_POST['vejl_eks_moms_nu']);
$db->query("UPDATE faktura_materialer
SET beskrivelse = '".$beskrivelse_nu."',
enhed = '".$enhed_nu."',
stk = '".$stk_nu."',
vejl_eks_moms = '".$vejl_eks_moms_nu."'
WHERE id = '".$_POST['materiale_id']."' AND
faktura_id = '".$_faktura->faktura_id."'
");
Everytime I change something in the input fields it just change every row to the same..
I'll hope you all understand what I want :)
Because you generate the fields through the loop you end up with multiple set of fields with the same field names.
In other words, if your $materialer_query returns 4 rows, then your html will have 4 times <input hidden name="materiale_id" value="<? print $mat->materialer_id; ?>">...
If you really need to have all the fields on the same page, then you can do it in two ways:
First option. You can create separate form per row. and submit them separately.
while($mat = $materialer_query->fetch_object()){
?>
<form action="--somewhere-to-process--" method="post">
<table><tr>
<td><input style="width:450px;" class="text-input" type="text" name="beskrivelse_nu" value="<? print $mat->beskrivelse; ?>"></td>
<td><input style="width:60px;" class="text-input" type="text" name="enhed_nu" value="<? print $mat->enhed; ?>"></td>
<td><input style="width:30px;" class="text-input" type="text" name="stk_nu" value="<? print $mat->stk; ?>"></td>
<td><input style="width:205px;" class="text-input" type="text" name="vejl_eks_moms_nu" value="<? print number_format($mat->vejl_eks_moms,2,",","."); ?>"></td>
<td></td>
<td><img src="images/icons/slet.gif" width="16" alt="Slet" />
<input type="submit" value="Update">
</td>
<input hidden name="materiale_id" value="<? print $mat->materialer_id; ?>">
</tr></table>
</form>
<?
}
Second option is you create one form, but each field name is uniquer throughout the form.
Something like this:
while($mat = $materialer_query->fetch_object()){
?>
<tr>
<td><input style="width:450px;" class="text-input" type="text" name="faktura[<?=$mat->faktura_id?>][beskrivelse_nu]" value="<? print $mat->beskrivelse; ?>"></td>
<td><input style="width:60px;" class="text-input" type="text" name="faktura[<?=$mat->faktura_id?>][enhed_nu]" value="<? print $mat->enhed; ?>"></td>
<td><input style="width:30px;" class="text-input" type="text" name="faktura[<?=$mat->faktura_id?>][stk_nu]" value="<? print $mat->stk; ?>"></td>
<td><input style="width:205px;" class="text-input" type="text" name="faktura[<?=$mat->faktura_id?>][vejl_eks_moms_nu]" value="<? print number_format($mat->vejl_eks_moms,2,",","."); ?>"></td>
<td></td>
<td><img src="images/icons/slet.gif" width="16" alt="Slet" />
<input type="submit" value="Update">
</td>
<input hidden name="faktura[<?=$mat->faktura_id?>][materiale_id" value="<? print $mat->materialer_id; ?>">
</tr>
<?
}
This way your $_POST[faktura] will be an associative array with faktura-id keys and their updated value.
Scroll through the array and update the DB from each array.
Please keep in mind that qty of fields in a form is limited. So make sure not to have hundreds fields in the form :)

how to do i trigger a static method in a page that is called by jquery ajax

My problem is that i have a dropdown box that i load the content based on my selection.However in one of these pages that i get the content i have an static method that i need to call before showing the content.all the method does is to check if the that value was set in the session. If it was populate the field. Looks like the method does not fire if i load the content using jquery ajax.
Here is the html i am loading based on my selection:
<table>
<tr>
<td><label class="required">Enter Current Number:</label></td><td><input type="text" size=13 id="current_number" name="current_number" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "current_number"); ?>"></td>
</tr>
<tr><td colspan="2"><strong>Please Enter the Address associated to your current phone number</strong>
NOTE: You may only keep your current phone number if the address of your current phone number is in the same state as the address where you are signing up for CenturyLink service. </td></tr>
<tr>
<td width=20%><label class="required">Address Line 1:</label></td><td><input type="text" size=25 id="address_line1" name="address_line1" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "address_line1"); ?>" ></td>
</tr>
<tr>
<td>Address Line 2:</td><td><input type="text" size=25 id="address_line2" name="address_line2" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "address_line2"); ?>"></td>
</tr>
<tr>
<td><label class="required">Apartment:</label></td><td><input type="text" size=5 id="Apartment" name="Apartment" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "Apartment"); ?>"></td>
</tr>
<tr>
<td><label class="required">City:</label></td><td><input type="text" size=15 id="City" name="City" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "City"); ?>"></td>
</tr>
<tr>
<td><label class="required">State:</label></td><td><select id="State" name="State" >
<option value="">Select a State</option>
<?php foreach (Util::getStatesCodes() as $key => $value) {?>
<option value="<?php echo $key; ?>" <?php Util::getSessionValuebySessionName("phonePorting", "City")==$key)echo "selected";?>><?php echo $key; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td><label class="required">ZipCode:</label></td><td><input type="text" size=15 id="Zipcode" name="Zipcode" value="<?php echo Util::getSessionValuebySessionName("phonePorting", "Zipcode"); ?>" ></td>
</tr>
</table>
Here is the static method:
public static function getSessionValuebySessionName($ssessionName, $keyName){
if(!empty($_SESSION[$ssessionName][session_id()][0][$keyName]))
{
return $_SESSION[$ssessionName][session_id()][0][$keyName];
}
So pretty much what i want to do is to load the sessions values(in the text fields) if they are not empty in the html content when i make the selction in my dropdown. Hope it makes sense.
thank you.
problem resolved. I forgot to start the session in the constructor.....

Help passing _POST Form Data PHP

I apologize in advance, I am a PHP noob!
I have form with some hidden fields. I need the values to POST to "submit_rma.php" so that they're not missing from the db--I need $qty, $estmate_id and $rma_type.
The rest of the fields are just displaying data for the user and are readonly. Currently I only get value from the qty text field.
Is there any easier way to pass these values? URL is out of the question due to security issues.
<form method="post" action="submit_rma.php";>
<table>
<tr>
<td>
Quantity
</td>
<td>
<input type="text" name="qty" value="<?php echo $qty ?>" size="1"/><br/>
</td>
</tr>
<tr>
<td>
Part #
</td>
<td>
<input type="text" name="" value="<?php echo $model ?>" size="8" READONLY/><br/>
</td>
</tr>
<tr>
<td>
Description
</td>
<td>
<input type="text" name="" value="<?php echo $name_EN ?>" size="50" READONLY/><br/>
</td>
</tr>
<tr>
<td>
Paid Date
</td>
<td>
<input type="text" name="" value="<?php echo $sold_date ?>" size="6" READONLY/><br/>
</td>
</tr>
<tr>
<td>
Amount Each
</td>
<td>
<input type="text" name="" value="<?php echo $dealer_price ?>" size="8" READONLY/>
</td>
</tr>
</table>
<input type="hidden" name="estmate_id" value="<?php echo $estmate_id ?>">
<input type="hidden" name="rma_type" value="Short Shipped">
<input type="submit" name="submit";">
</form>
Maybe use a hidden <INPUT>:
<input type="hidden" name="qty" value="<?= $qty ?>">
This won't show anything to the user. If you're unfamiliar, <?= x ?> is effectively equivalent to: <?php echo x; ?>.
However, this is a security problem, as an attacker could craft a fake request and put a different value into the field (sidestepping your page and doing the request directly). You should try and get the value some other way, such as through running the INSERT on page generation, then using an UPDATE on the POST, or something like that.
Am I pointing out the obvious to say that you forgot NAME attributes for all of the text boxes after "qty"? The values won't persist beyond this page if the names aren't there :-)

Categories