Forum Moderators: coopster

Message Too Old, No Replies

Generate a new row <tr> when click to link

generate new row

         

gabo

11:01 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



Hello!
I want create a webapp for creating invoices and now I've a problem. In default when I open the new invoice I've one row for one item. Next to the this row I need link with "add new item" witch will generate new row and after put each row in MySQL.
If every <input> have same id can I insert them to the databese?
I tried making a some script and experiment with this, but without any results.

e.g.:

 [pre]
<?
function x() {
if ($click == 1) {
echo "<br /><table><tr>NewRow</tr></table>";
}
}
?>
<body>
<a href="<? $click = 1; ?>">#*$!x</a>
<? x(); ?>
</body>
[/pre]

Thank you very much for every response.

G.

webfoo

12:02 am on Jun 12, 2008 (gmt 0)

10+ Year Member



Welcome to webmasterworld, Gabo!

It seems that you are confusing JavaScript and PHP - PHP is a server side lanugage and there is no "onClick" event handlers as there are in JavaScript - a client-side scripting language.

First, make sure your server supports PHP. Upload your files onto the server. PHP is run on the server, not your local machine (like JavaScript).

Your function x is valid, but will never be used, because this line is invalid:

<a href="<? $click = 1; ?>">#*$!x</a>
This line would just display a broken link. You seem to expect the clicking of this link to set the variable click to equal 1. That's not how PHP works.

In PHP, we have to submit a new request to the server every time we want something to happen. Try something like this:

<table>
<?php
// writes the first row no matter what.
echo "<tr><td>This is your first row.</td></tr>";

// if $click is not set, end the table and display a link. 
if (!isset($_REQUEST['click'])) {
echo "</table>";
echo "<a href='?click=1'>Add new Row</a>";
}
// if $click is set, display the second row, then end the table.
else {
echo "<tr><td>This is your second row.</td></tr>";
echo "</table>";
}
?>

If every <input> have same id can I insert them to the databese? 

No. You should not give more than one thing the same id. The point of id's is to uniquely distinguish elements.

I hope this helped you, please reply with questions/comments/concerns.

gabo

5:54 am on Jun 12, 2008 (gmt 0)

10+ Year Member



Hello!
Thank you for your response Webfoo.
Can I have two more question, please?
If I need
 echo "<a href='?click=1'>Add new Row</a>"; 
in each new row too is possible do that, beacause I think this will be invalid
[pre]
...
else {
echo "<tr><td>This is your second row.</td></tr>";
[b] echo "<a href='?click=1'>Add new Row</a>"; [/b]
echo "</table>";
}
[/pre]

About <input>s:
I can generate IDs with index e.g. "$i++". If I fill the form and afterward I click "Add new Row", I will lost every filled data, right? I can put these data to temporary MySQL table or Cookie and prevent loose data. Is this solution good?

Thank you.

G.

eelixduppy

3:19 pm on Jun 12, 2008 (gmt 0)



>> I can put these data to temporary MySQL table or Cookie and prevent loose data. Is this solution good?

Cookies [php.net] or sessions [php.net] are the way to go.

gabo

2:30 pm on Jun 15, 2008 (gmt 0)

10+ Year Member



Hello!
Thank you for your helps.
Hm... The cookies and sessions are the things what I didn't use before, so I hope I learn it.
I resolve the Add new Row problem as follows:
[pre]
<table>
<?php
[i]//If the defRepeat isn't set the value is 1 (1 row)[/i]
if (!isset ($defRepeat)) {
$defRepeat = 1;
}
for ($repeat = 1; $repeat <= $defRepeat; $repeat++) {
echo "<tr><td> Row content</td>"; [i]// The contet of the row[/i]
[i]// the link is displayed just on the last row[/i]
if ($repeat == $defRepeat) {
[i]// The link will add allways one more row [b]$defRepeat+1[/b][/i]
echo ("<td><a href=\"?defRepeat=".($defRepeat + 1)."\">Add New Row</a>");
[i]// The remove link will show if there are more then one row[/i]
if ($defRepeat > 1) {
echo ("<br /><a href=\"?defRepeat=".($defRepeat - 1)."\">Remove Row</a>");
}
echo ("</td>");
}
}
?>
</table>
[/pre]

I'm sorry, my english is not good. I hope if somebody will look for something like this code, he will understand.
Thank you guys. Maybe I'll come back with cookies. :)

G.

gabo

10:08 pm on Jun 15, 2008 (gmt 0)

10+ Year Member



Is possible save the form data to the session or cookie without submit form?
I need keep the data when I click to the Add New Row and fill them back to the form when the page is updated with new the row.

Thanks.

G.

eelixduppy

3:09 am on Jun 16, 2008 (gmt 0)



>> Is possible save the form data to the session or cookie without submit form?

One way or another you are going to have to use javascript to do this without a form submit. You can either set the cookie manually with javascript or you can use an asynchronous call to a server-side script to save certain data into sessions or cookies.

gabo

1:55 pm on Jun 17, 2008 (gmt 0)

10+ Year Member



Hello!
I'm cretes the cookies and get it back to the form. Now I've problem with the non-ASCII characters.
I'm set cookies with java script:
[pre]
<?
$oneHour = (mktime(date('h')+1));
$oneHour = date('r',$oneHour);

function restoreValue($thisID, $defValue) {
if (isset ($_COOKIE[$thisID])) {
echo $_COOKIE[$thisID];
} else {
echo $defValue;
}
}

?>
<script type="text/javascript">
function setcookie(Cname, Cvalue) {
var expire = "<?php echo $oneHour; ?>";
document.cookie = Cname+"="+Cvalue+"; expires="+expire+"; path=/";
}
</script>

<form>
<input name="Description" id="Description" type="text" value="<?php restoreValue('Popis','The description field');?>" onchange="setcookie(this.id, this.value);"/>
[/pre]

How can I fix this thing?
The page is UTF-8:

[pre]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
[/pre]

Thanks.
G.

coopster

1:10 pm on Jun 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If non-ASCII characters are being entered into the form which your JavaScript is then putting into the cookie, you are going to have to manage that in your JavaScript by editing the new value being entered prior to using JavaScript to change the cookie value.