Jump to content

how to keep Line return, tabs etc. in text box


brosskgm

Recommended Posts

I created a very simple text box database that I could start putting all the different code etc I have found and used.

 

Problem when I paste the code in a text box, it takes all the spaces, tabs, and line returns out and stores it in a jumbled mess.

 

Is there a way to tell it to leave it as, including all the ",'$% etc...?

 

Kind of like here when we type or paste code in. The code with the SUBDIR is read by the section below it.

 


} else if(SUBDIR == 'phpmysql') {
   $fields[] = array('name' => 'work_date', 'type' => 'date', 'len' => 15);
   $fields[] = array('name' => 'description', 'type' => 'varchar', 'len' => 50);
   $fields[] = array('name' => 'notes', 'type' => 'text', 'len' => 100);

//////// add.php

<?php

require_once('../check_logged_in.php');
require_once('../utils.php');


if(!defined('ACTION')) { define('ACTION', 'ADD'); }


// print what's needed for using calendars, both right now and when script exits
CalendarSetup();

$form_action = (ACTION == 'ADD') ? 'added.php' : 'updated.php';

echo "\n\n" . '<script type="text/javascript" src="' .CONFIG_BASE_DIR. '/utils.js"></script>' . "\n";

?>

<form id="FormName" name="FormName" enctype="multipart/form-data" method="post" action="<?php echo $form_action; ?>" onsubmit="return <?php echo CreateOnSubmitString(); ?>;">

<table width="448" border="0" cellspacing="2" cellpadding="0">

<?php

// if updating, get $id and put the values in $data
$data = NULL;
if(ACTION == 'UPDATE') {
   require_once("../db.php");

   SetKeys(array('id'), $_GET);

   $query = "SELECT * FROM `".SUBDIR."` WHERE id='" .mysql_real_escape_string($_GET['id']). "' AND username='{$_SESSION['username']}' ";
   $result = mysql_query($query) or die("UPDATE: query failed");
   $data = mysql_num_rows($result) ? mysql_fetch_assoc($result) : 
      MyArrayFillKeys($fields, NULL);
}

$string_templates = array();

// use textbox
$string_templates['varchar'] = '<tr><td width = "150"><div align="right"><label for="!!FIELDNAME">!!CAPFIELDNAME</label></div></td>
<td><input type="text" id="!!FIELDNAME" name="!!FIELDNAME" size="25" maxlength="!!FIELDLEN" value="!!FIELDVALUE"></td></tr>' . "\n\n";

// use textbox + calendar
$img_html = '<img src="' .CONFIG_BASE_DIR. '/_date_picker/images/calendar.png" border="0">';
$calendar = '<a href="#" onclick="cdp.showCalendar(this, \'!!FIELDNAME\'); return false;">' .$img_html. '</a>';
$string_templates['date'] = str_replace('</td></tr>', "\n$calendar</td></tr>", 
   $string_templates['varchar']);

// use textarea
$string_templates['text'] = '<tr><td width = "150"><div align="right"><label for="!!FIELDNAME">!!CAPFIELDNAME</label></div></td>
<td><textarea id="!!FIELDNAME" name="!!FIELDNAME" rows="4" cols="40">!!FIELDVALUE</textarea></td></tr>' . "\n\n";

// Use file upload. The photo_upload field is handled separately in Fields2String(), and it's the only current file_upload.
// All the others will be treated like text.
$string_templates['file_upload'] = $string_templates['varchar'];

//output what's needed for each field
echo Fields2String($string_templates, $data);

if(ACTION == 'UPDATE') {
   $hidden =  "\n" . '<input type="hidden" name="id" value="' .htmlspecialchars($data['id']). '">' . "\n";
   $button_value = 'Update';
   
} else { // ACTION == 'ADD'
   $hidden = '';
   $button_value = 'Add';
}

echo <<<EOS
<tr><td width="150"></td><td>
$hidden
<input type="submit" name="submitButtonName" value="$button_value"></td>

</tr></table></form>
EOS;

?>



 

 

 

Link to comment
Share on other sites

There is only a couple files that handle all the databases

so I thought it was add. Here is added.php

 

 


<?php

require_once('../check_logged_in.php');
require_once('../utils.php');
require_once("../db.php");


if(!defined('ACTION')) { define('ACTION', 'ADDED'); }


echo '<a href="index.php">Back to List</a><br>' . "\n";

// Prep the fields for db use. When this loop is done, variables will be created for each field
// with the same name as the field (ie $id, $date_added, etc).
// Prep the fields for db use. Data will be put in $data.
$data = array();
foreach($fields as $index => $field_info) {
   $field = $field_info['name'];
   $type = $field_info['type'];
   
   $d = isset($_POST[$field]) ? $_POST[$field] : '';
   
   if($type == 'date') { // Handle all date fields
      if(!IsValidDate($d)) {
         die("${field}[$d] must look like 1/1/1999 or 01/01/1999 (no extra whitespace)");
      }
      
      // will do [9/8/1976] => [1976-9-8], then call mysql_real_escape_string()
      $d = MakeDBDate($d);
      
   } else if($field == 'photo_upload') { // Handle photo_upload
   
      // This will die if get invalid file or encounter any other issue. It will also create the
      // thumbnail. It returns the value to be stored in the db (it calls mysql_real_escape_string)
      $d = HandlePhotoUpload($field, $data);
   
   } else { // Handle all non-date non-photo_upload fields
      $d = mysql_real_escape_string($d);
   }
   
   $data[$field] = $d;
}

// now perform the query
if(ACTION == 'ADDED') { //add
   $query = "INSERT INTO `".SUBDIR."` (id, username, ".QueryData('names', $field_names, $data).") ".
      "VALUES ('', '{$_SESSION["username"]}', " .QueryData('values', $field_names, $data). ")";
   $results = mysql_query($query);

   if ($results) { echo "Details added."; }
   else { die("error adding details"); }
   
} else { //update
   $query = "UPDATE `".SUBDIR."` SET " .QueryData('set', $field_names, $data). 
      " WHERE id='{$data['id']}' AND username='{$_SESSION['username']}' ";
   $results = mysql_query($query);
   
   if ($results) { echo "Update successful."; }
   else { die("error updating details"); }
}

mysql_close();

?>

 

Link to comment
Share on other sites

This was written for me to replace the set of pages I had to create for each database. The new setup allows me to create a database in about 10 minutes and the add.php and add.php creates the form for me from the way it's listed under SUBDIR.

 

I can create a database in about 10 minutes. Has been working flawless except that I can post code in a text box.

 

Gets the form information.

 

The page is to long. It errors out when I post it, so this is part of the section that uses it.

 


// QueryData('names', $field_names); // date_worked
// QueryData('values', $field_names, $data); // '1/1/1999'
// QueryData('set', $field_names, $_POST); // date_worked = '1/1/1999'
function QueryData($type, &$field_names, &$data) {
   $output_array = array();
   foreach($field_names as $field) {
      if($field == 'id') { continue; } // skip id
      
      $field_value = isset($data[$field]) ? $data[$field] : '';
      
      if($type == 'names') { $output_array[] = $field; } // date_worked
      else if($type == 'values') { $output_array[] = "'$field_value'"; } // '1/1/1999'
      else if($type == 'set') { $output_array[] = "$field = '$field_value'"; } // date_worked = '1/1/1999'
   }
   
   return join(', ', $output_array);
}

// DeletePhotos($id); // needs $id escaped
// if SUBDIR has them, delete photo_upload and associated thumbnail for this id and $_SESSION['username']
// Assumes already connected to db, and $id has been escaped for db.
function DeletePhotos($id) {
   global $fields;
   
   $field = 'photo_upload';
   
   if(SUBDIR != 'contacts') { return; } // this SUBDIR doesn't have a photo_upload field
   
   $query = "SELECT * FROM `".SUBDIR."` WHERE id='$id' AND username='{$_SESSION['username']}'";
   $result = mysql_query($query) or die("DeletePhotos: query failed<br>\n");
   if(mysql_num_rows($result) == 1) { // if you got the row
      $data = mysql_fetch_assoc($result);
      $img_name = $data[$field];
      
      $fname = HOME ."/". CONFIG_UPLOAD_DIR . "/{$_SESSION['username']}.$img_name"; // regular
      @unlink($fname); // delete the file. It's OK if $img_name is ''
      
   } else { die("No such record exists"); }
}


Link to comment
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.