Get rid of multiline PHP variables

I tried out a form to collect sentences. Works well, BUT: 1 student pressed enter in the textarea number 18
This pushed all his answers down 1 line and his score was very bad.

<textarea cols="26" rows="1" maxlength="26" placeholder="Write your answer here." name="G18"></textarea>

Apparently, setting rows=“1” does not prevent the use of ‘enter’ in a textarea like I hoped.

I’m wondering if it is possible to look at each php variable before they get sent and get rid of new line characters?

This code, kindly suggested to me here puts an X in empty variables

if($studentnr == '') {$studentnr = 'X';}

for ($i=1; $i <= 35; $i++) {

    if (${"q$i"} == '') ${"q$i"} = 'X'; 
}

Maybe something similar can get rid of newline characters? Thanks for any tips or links!

Edit: I tried this, it seems to work. Can it be tidied up a bit?

for ($i=1; $i <= 35; $i++) {
	$str = ${"q$i"};
	$order   = array("\r\n", "\n", "\r");
	
    $nonewlines = str_replace($order, '', $str);
    ${"q$i"} = $nonewlines;
    }

Why? Just use trim() and strtr() before using the variable. And use arrays, it’s much cleaner then your variable juggling.

https://www.php.net/manual/en/language.types.array.php

1 Like

How would I do that?

Until the student writes something, I don’t have a variable.

You mean before the student clicks ‘send’ do something?

<?php

$texts = [
    "this",
    " is a test ",
    "\n with newline",
    "\n everywhere \n",
    "\r and carriage return \r\r\r",
    "\r and \n some \r\n mixed \n \r stuff \r\n\n\r",
];

$normalized = array_map(function($text){
    $trimed = trim($text);
    $replaced = strtr($trimed, ["\n" => '', "\r" => '']);
    return $replaced;
}, $texts);

var_dump($normalized);

/**
array(6) {
  [0]=>
  string(4) "this"
  [1]=>
  string(9) "is a test"
  [2]=>
  string(12) "with newline"
  [3]=>
  string(10) "everywhere"
  [4]=>
  string(19) "and carriage return"
  [5]=>
  string(24) "and  some  mixed   stuff"
}

Please forgive my obtuseness, but I really know nothing about php. I’m not sure where I should put the code you suggest, but I’d like to try it.

My <form> has this:

<form method="post" action="php/thankyou18BEweek17.php" enctype="text/plain">

So when they press send, it invokes thankyou18BEweek17.php

Your code should go at the top of my thankyou.php, before the variables are assigned??

<?php
// your code here??

$studentnr = $_POST['sn'];
$q1 = $_POST['1'];
$q2 = $_POST['2'];
$q3 = $_POST['3'];
$q4 = $_POST['4'];

You can use the code whenever you think you need it, it just strips out any linebreaks. But you have to provide an array with all the texts.

1 Like

Perhaps JavaScript would be an easier solution for your scenario?

This script could be included at the bottom of the same page that you have your textareas in. It adds an event listener to all of the textarea elements on the page. If one of the listeners detects an ‘enter’ key press, it blocks the default behavior and pops up an alert.

<script>
	var listOfTextareas = document.getElementsByTagName("textarea");
	Array.from(listOfTextareas).forEach(applyListenerToTextarea);

	function applyListenerToTextarea(textareaElement)
	{				
		textareaElement.addEventListener('keypress', function (e) {
				if (e.keyCode == 13 && !e.shiftKey)
				{
					// prevent default behavior
					e.preventDefault();
					alert("The 'enter' key is not allowed.");
					return false;
				}
			});
			
			
	}
</script>

Example of working code:
https://jsfiddle.net/Kasil/ncy1q5Ls/13/

1 Like

Great, I will definitely try that! Thanks a lot!

1 Like

Just got round to trying it! Works like a dream!

Thank you very much!

I managed to bend some php to get rid of the newline characters, but “Prevention is better than cure!”

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service