Jump to content

Recommended Posts

I have these input fields.  How do i do a duplicate button?.. so when i press the button i get another stack of input fields(the code below) and if i press the button again i get another stack etc.

 

<p align='center'><input name='image' value='image' type='text'>  <br>

<input type='text' name='hyperlink'value='hyperlink'>  <br>

<input type='text' name='currency' value='currency'> <br>

<input type='text' name='name' value='name'> <br>

<input type='text' name='info' value='info'> <br>

<input type='text' name='keywords' value='keywords'> <br>

<input type='text' name='type' value='type'> <br>

<input type='submit' value='Submit'<br></p>

Link to comment
https://forums.phpfreaks.com/topic/169443-solved-duplicate-button/
Share on other sites

Put your stack of inputs inside a variable then in an array. Then every time you click the duplicate button, have some code to stick another copy of the input variable in the array. Then just loop through the array, echoing out the input stacks.

 

This will also keep your inputs in line when sticking them in a database (or whatever else you're doing with them), as the first set of inputs would be like $myvar[0], the second $myvar[1], etc. Just make sure that you change your variable names in your input form to fit the array format, ie name='hyperlink[]'.

 

Well I'm too tired to give code, but maybe you understand what I'm getting at?

i do, but i have trouble declaring a whole lot of code as a variable.

 

is this right?

 

$array="<p align='center'><input name='image' value='image' type='text'>  <br>

<input type='text' name='hyperlink'value='hyperlink'>  <br>

<input type='text' name='currency' value='currency'> <br>

<input type='text' name='name' value='name'> <br>

<input type='text' name='info' value='info'> <br>

<input type='text' name='keywords' value='keywords'> <br>

<input type='text' name='type' value='type'> <br>

<input type='submit' value='Submit'<br></p>";

 

:confused:

ok i tried and i get, where did i go wrong?

 

Parse error: syntax error, unexpected T_ELSE in /home/ustackc1/public_html/Admin.php on line 28

 

 

<?php

session_start();

 

if ($_SESSION['username'])

 

echo "Welcome, ".$_SESSION['username']."!<br><a href='Logout.php'>Log Out</a>";

 

  $stacks = array("

 

<form enctype='multipart/form-data' action='insert.php' method='post' name='changer'>

 

<p align='center'><input name='image' value='image' type='text'>  <br>

<input type='text' name='hyperlink'value='hyperlink'>  <br>

<input type='text' name='currency' value='currency'> <br>

<input type='text' name='name' value='name'> <br>

<input type='text' name='info' value='info'> <br>

<input type='text' name='keywords' value='keywords'> <br>

<input type='text' name='type' value='type'> <br>

<input type='submit' value='Submit'><br></p> ");

 

 

 

 

 

 

else

die("You must be logged in!");

 

 

 

?>

<?php

  <form enctype='multipart/form-data' action='Admin.php' method='post' name='changer'>

  <input type='submit' value='duplicate'>

$button = $_GET['duplicate'];

  if(!$button)

    array_push($stack, $stack);

  echo $stack;

 

?>

Well the syntax error is due to the lack of braces around your if/else statements. Try:

 

<?php
session_start();

if ($_SESSION['username']){

   echo "Welcome, ".$_SESSION['username']."!<br><a href='Logout.php'>Log Out</a>";
   
   $stacks = array("

      <form enctype='multipart/form-data' action='insert.php' method='post' name='changer'>

<p align='center'><input name='image' value='image' type='text'>   <br>
<input type='text' name='hyperlink'value='hyperlink'>  <br>
<input type='text' name='currency' value='currency'> <br>
<input type='text' name='name' value='name'> <br>
<input type='text' name='info' value='info'> <br>
<input type='text' name='keywords' value='keywords'> <br>
<input type='text' name='type' value='type'> <br>
<input type='submit' value='Submit'><br></p> ");




}else{
   die("You must be logged in!");
}



?>
<?php
  <form enctype='multipart/form-data' action='Admin.php' method='post' name='changer'>
  <input type='submit' value='duplicate'>
$button = $_GET['duplicate'];
   if(!$button)
     array_push($stack, $stack);
   echo $stack;

 

Though i'm afraid things aren't quite as simple as you've got there. You'll need to name all your input fields as arrays. Each time the "duplicate" button is clicked, you'll have to loop through each field and echo out the value if it exists -- otherwise you'll get your new input fields, but you'll lose all the data. Here's an old script where i did something similar:

 

<?php

$userid = $_SESSION['uid'];

$done = FALSE;

if(isset($_POST['makepayments'])){

$inserts = array();

foreach($_POST['category'] as $x=> $category){

	if(!empty($_POST['amount'][$x]) && $_POST['amount'][$x] > 0){

		$amount = mysql_real_escape_string($_POST['amount'][$x]);

		$date = mysql_real_escape_string($_POST['date'][$x]);

		$details = mysql_real_escape_string($_POST['details'][$x]);

		$inserts[] = "('".mysql_real_escape_string($category)."','$details','$amount','$date',$userid)";

	}

}

$insert = implode(',',$inserts);

if(count($inserts) > 0){

	$sql = "INSERT INTO items (cat_id,description,price,purchase_date,uid) VALUES $insert";

	mysql_query($sql) or trigger_error(mysql_error().'<br />Query Was: <br />'.$sql,E_USER_ERROR);

}

$done = TRUE;

$doneamount = count($inserts);

$_POST = array();



}

?>

<h3>Make Payment</h3>

<?php

if($done===TRUE){

echo $doneamount. ' payment(s) successfully added<br /><br />'."\n";

}

$sql = "SELECT COUNT(*) FROM categories WHERE uid=$userid";

$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

if(mysql_result($result,0)==0){

    echo 'Before you can add a payment, you need to add some categories. Go to <a href="index.php?page=admin">the administration page</a> to add some.';

}else{

?>

<form method="post">

Reload with

<select name="numfields" />

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

<option value="4">4</option>

<option value="5">5</option>

<option value="10">10</option>

</select>

field(s)

<input type="submit" name="reload" value="Reload" />

<br />

<br />

<table>

<tr>

<th>Details</th>

<th>Spent On</th>

<th>Date</th>

<th>Amount</th>

</tr>

<?php

if(isset($_POST['reload'])){

$numfields = (int) $_POST['numfields'];

}else{

$numfields = 1;

}

for($x=0;$x<$numfields;$x++){

echo "<tr>\n";

echo '<td><input type="text" name="details['.$x.']" value="';

echo (isset($_POST['details'][$x])) ? $_POST['details'][$x] : '';

echo '" /></td>'."\n";

echo '<td>'."\n";

echo '<select name="category['.$x.']">'."\n";

$sql = "SELECT id,name FROM categories WHERE uid=$userid ORDER BY id ASC";

$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

while(list($id,$name) = mysql_fetch_row($result)){

	$selected = '';

	if(isset($_POST['category'][$x]) && $_POST['category'][$x] == $id){

		$selected = 'selected="selected"';

	}

	echo '<option value="'.$id.'" '.$selected.'>'.$name.'</option>';

}

echo '</select>';

echo '</td>'."\n";

echo '<td>'."\n";

echo '<input type="text" name="date['.$x.']" value="';

echo (isset($_POST['date'][$x])) ? $_POST['date'][$x] : date('Y-m-d');

echo '" />'."\n";

echo '</td>'."\n";

echo '<td><input type="text" name="amount['.$x.']" value="';

echo (isset($_POST['amount'][$x])) ? $_POST['amount'][$x] : '';

echo '" /></td>'."\n";

echo '</tr>';	

}

?>



</table>

<br />

<input type="submit" name="makepayments" value="Make Payments" />

</form>

<?php

}

?>

 

Lastly, you could have this work a lot nicer with Javascript -- the page wouldn't have to reload each time you add fields. It's pretty easy to accomplish with the jQuery framework too.

i tried this array and its not showing the input fields.

 

$stacks[0] = array("

 

      <form enctype='multipart/form-data' action='insert.php' method='post' name='changer'>

 

<p align='center'><input name='image' value='image' type='text'>  <br>

<input type='text' name='hyperlink'value='hyperlink'>  <br>

<input type='text' name='currency' value='currency'> <br>

<input type='text' name='name' value='name'> <br>

<input type='text' name='info' value='info'> <br>

<input type='text' name='keywords' value='keywords'> <br>

<input type='text' name='type' value='type'> <br>

<input type='submit' value='Submit'><br></p> ");

 

  echo $stacks[0];

any idea why? it just says "Array".
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.