Jump to content

[SOLVED] PHP FORM SUBMIT


yjim

Recommended Posts

Question: Explain what kinds of code you expect to have for you input?

"multiple lines of code"

 

Here's a sample form:

 

<form action='handle.php' method='post'>
<input type='text' name='code[]'><br>
<input type='text' name='code[]'><br>
<input type='text' name='code[]'><br>
<input type='text' name='code[]'><br>
<input type='text' name='code[]'><br>
<input type='text' name='code[]'><br>
<input type='reset'><input type='submit'>
</form>

 

handle.php

<?php
if(isset($_POST['code']) && !empty($_POST['code'])){
//$code contains an array of all the lines of code
$time = timestamp();
$query = "INSERT INTO `users` ('code','time') VALUES ($code,$time)";
echo (mysql_query($query))? "true" : "false";
} else {
die('You did not submit any code!');
}
?>

Since pressing enter in a textarea adds the \n code, I would suggest the following:

 

form

<form action='handle.php' method='post'>
<textarea name='code'></textarea><br>
<input type='reset'><input type='submit'>
</form>

 

handle.php

<?php
if(isset($_POST['code']) && !empty($_POST['code'])){
//$code contains a string of all the lines of code
$code = explode("\n",$_POST['code']);
//$code is now an array of lines of code, line by line
$time = timestamp();
$query = "INSERT INTO `users` ('code','time') VALUES ($code,$time)";
echo (mysql_query($query))? "true" : "false";
} else {
die('You did not submit any code!');
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.