Jump to content

Recommended Posts

I have a html form which generates input fields using javascript at the request of the user (the user presses a button and a new field appears).

I am using a php code to grab all of these fields similar to the below:

 

foreach ($_POST as $key => $value) {
$$key = addslashes(trim($value));
}

 

I want to save all data from these fields to a text file. I know how to save a string to a text file, but I'm not sure how to save this to one.

 

Could Somebody Please Help?

(if it helps, all the generated form fields have random numbers for there names.)

 

The Full Code Below:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>untitled</title>
<style type="text/css">



</style>
</head>
<body>
<?php
if (isset($_POST['save_edit'])) {

echo "<form method='post' action='$PHP_SELF'>"
	."<table cellpadding='3' cellspacing='0' border='1'>"
	."<tr><th align='left'>Item URL:</th>	<th align='left'>Item Name:</th></tr>";

foreach ($_POST as $key => $value) {
	$$key = addslashes(trim($value));

	if($key != 'save_edit'){//Take Out Submit Button Value
		$randomenumber = rand(0,1009000);
		echo "<tr><td><input type='hidden' name='".$randomenumber."URL' value='<".$randomenumber." Starts>' /><input type='' name='url_".$randomenumber."' value='".$_POST["$key"]."' /></td>	<td><input type='hidden' name='".$randomenumber."SPLIT' value='<".$randomenumber." SPLIT>' /><input type='' name='name_".$randomenumber."' value='Item Name' /><input type='hidden' name='".$randomenumber."NAME' value='<".$randomenumber." Ends>' /></td>";
	}
}

echo "<tr><td> </td>	<td align='right'><input type='submit' name='save_edit2' value='Save' /></td></tr>"
	."</table>"
	."</form>";
}elseif (isset($_POST['save_edit2'])){

foreach ($_POST as $key => $value) {
	$$key = addslashes(trim($value));

	if($key != 'save_edit2'){//Take Out Submit Button Value
		$randomenumber = rand(0,1009000);
		$key = "".$_POST["$key"]."<br/>";
		$key = str_replace("<br/>","",$key);
		$key = preg_replace("!<.*?Starts>!s", "<option value='", $key);
		$key = preg_replace("!<.*?SPLIT>!s", "'>", $key);
		$key = preg_replace("!<.*?Ends>!s", "</option>", $key);

		$myFile = "test.txt";
		$fh = fopen($myFile, 'w') or die("can't open file");
		fwrite($fh, $key);
		fclose($fh);
	}
}
}else{
?>
<form id="the_form" name="the_form" method="post" action="<?php echo $PHP_SELF;?>" style="width:100px;">
<input type="button" value="Add Menu Item" onclick="insertField()" /><input type='submit' name='save_edit' value='Submit' />

<script type="text/javascript" language="javascript">
function get_random()
{
    var ranNum= Math.floor(Math.random()*5000000000000);
    return ranNum;
}


var the_field, the_form = document.getElementById('the_form');

function insertField()
{
the_field = document.createElement('INPUT');
the_form.insertBefore(the_field, document.getElementsByTagName('INPUT').item(1));
the_field.setAttribute('name', get_random());
the_field.setAttribute('size', '30');
the_field.setAttribute('value', 'http://webaddress.com');
}
</script>

</form>
<?php
}
?>

</body>
</html>

 

What Am I Doing Wrong?

Link to comment
https://forums.phpfreaks.com/topic/66595-solved-i-need-help/
Share on other sites

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.