white space occur in textarea - javascript

I am trying to update query using php and database and display the textarea in a page. Here is my code,problem is i get the text area with some whitespace. Please help.
if (isset($_POST["events"])&&isset($_POST["description"]))
{
$id=$_POST["id"];
$title=$_POST["events"];
$description=$_POST["description"];
}
HTML
<form action="hotel1_galery_eventedit.php" method="post" class="col-sm-4" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $val->event_id;?>">
<div class="form-group has-info">
<label class="control-label" for="inputSuccess">Event title</label>
<input type="text" class="form-control" name="events" value="<?php echo $val->event_title;?>">
</div>
<div class="form-group has-info">
<label class="control-label" >Event Description</label>
<textarea name="description" class="form-control text-left" rows="3">
<?php echo $val->event_description;?>
</textarea>
</div>
</form>

Have you tried trim() function. use like this
<textarea name="description" class="form-control text-left" rows="3">
<?php echo trim($val->event_description);?>
</textarea>
hope this will help you.

use trim();
if (isset($_POST["events"])&&isset($_POST["description"]))
{
$id=$_POST["id"];
$title=trim($_POST["events"]);
$description= trim($_POST["description"]);
}

if (isset($_POST["events"])&&isset($_POST["description"]))
{
$id=mysql_real_escape_string(trim($_POST["id"]));
$title=mysql_real_escape_string(trim($_POST["events"]));
$description=mysql_real_escape_string(trim($_POST["description"]));
}
It's a secure method to protect our db from unexpected problems and you can also show data without any extra space...

Keep your textarea code in same line, the enter inside text area is causing this issue.
<textarea name="description" class="form-control text-left" rows="3"><?php echo $val->event_description;?></textarea>
The enter which you have added before echoing the value is the reason for this.

Use trim() with conditions, if value will be blank in text area then it will be treated as NULL or you can use ''
if(isset($_POST["events"])&&isset($_POST["description"])){
$id=$_POST["id"];
$title=$_POST["events"];
$description= ($_POST["description"] == '') ? NULL : trim($_POST["description"]);
}

Related

The checkbox function can only be used just on the first line in php

I have a modal and in this modal, I have a checkbox, The checkbox function can only be used on the first line table.
Code is like this, the checkbox function works but only on the first row of the table, henceforth the modal process doesn't work, I've tried adding
<?php echo $data['id_user']; ?>
to the ID but it still doesn't work.
<div class="form-group">
<label for="ubah_password">Ubah Password</label>
<div class="form-group">
<input type="checkbox" id="ubah_password" name="ubah_password" value="1">
<label class="prevent-select" for="ubah_password"> Ya, saya ingin mengubah password</label>
</div>
</div>
<div id="form_password" style="display: none;">
<div class="form-group">
<label for="editPassAdmin">Password Baru</label>
<input type="password" class="form-control form-control-sm" id="editPassAdmin" name="editPassAdmin" placeholder="Masukkan password baru">
</div>
<div class="form-group">
<label for="password_confirm">Konfirmasi Password</label>
<input type="password" class="form-control form-control-sm" id="password_confirm" name="password_confirm" placeholder="Masukkan kembali password baru">
</div>
</div>
<script>
var checkbox = document.getElementById("ubah_password");
var formPassword = document.getElementById("form_password");
checkbox.addEventListener("change", function() {
if (this.checked) {
formPassword.style.display = "block";
} else {
formPassword.style.display = "none";
}
});
</script>
The HTML ID attribute is designed to be unique, so even if you repeat it, you need to select them all, then individually in your JavaScript to load the modal.
Also, the name attributes should probably be unique enough for your form PHP code to tell which password you are changing.
For example, you could change the ID attributes to something like:
id="ubah_password[<?php echo $data['id_user']; ?>]"

How to save several input data in single MySql column?

How to save several input data in single MySql column?
I have 3 input fields, with same name (name="type"). Now I want to
save these 3 input fields datas in one, single Mysql column. How can I
solve this?
<div class="form-column col-md-4">
<div class="form-group col-md-12">
<input type="text" class="form-control" name="type" id="height" placeholder="height (cm)">
</div>
<div class="form-group col-md-12">
<input type="text" class="form-control" name="type" id="width" placeholder="width (cm)">
</div>
<div class="form-group col-md-12">
<input type="text" class="form-control" name="type" id="length" placeholder="length (cm)">
</div>
</div>
HTML
<form >
<div class="form-column col-md-4">
<div class="form-group col-md-12">
<input type="text" class="form-control" name="type[width]" id="height" placeholder="height (cm)">
</div>
<div class="form-group col-md-12">
<input type="text" class="form-control" name="type[height]" id="width" placeholder="width (cm)">
</div>
<div class="form-group col-md-12">
<input type="text" class="form-control" name="type[length]" id="length" placeholder="length (cm)">
</div>
Note when naming the variable in html use specifically name="type[width]". do not use the single or double quote inside with width like names
PHP
<pre>
<?php
$type = $_GET['type'];
print_r($type);
extract($type);
echo 'width:' . $width .'<br />';
echo 'height:' . $height .'<br />';
echo 'length:' . $length .'<br />';
//serialize before inserting
$mysql_value = serialize($type);
echo $mysql_value;
//retrive by unserialize
$unserialize_mysql_value = unserialize($mysql_value);
print_r($unserialize_mysql_value);
?></pre>
Documentation of extract function
I don't recommend it, but you could create an assoc array with the names and values and save it as a JSON string in the database using json_encode(). Then you can use json_decode() to get the array again and have access each field individually.
Change the name from name="type" to name="type[]" and the data will POST as an array.
When handling the request body, you can concatenate these however you'd like to be stored.
ex: $column_val = $request->type[0] . $request->type[1];
or: $column_val = implode('-', $request->type);
You can't use same name for multiple input, if you want to use same name then you can use name attribute like an array, something like:
name="type[]"
Now, you can use serialize() method in PHP to store all inputs in one single column something like:
$inputData = serialize($_POST['name']); // this will store your array as a string
After this, when you need to display this column then you can use unserialize() method like:
$fetchData = unserialize(your column name);
Here, $fetchData will return the original array, which you saved in your database.
Additional Point:
You can also store your input with associative array like:
name="type[width]" and name="type[height]" and name="type[length]"

How to $setdirty to a single input

I've got a little problem. I want to set to dirty a single input, I mean, because when I give a value automatically it stays in pristine class, doesn't change, and doesn't save the new value.
If I edit it, it works and change the class. I want to cancel that pristine class.
If anyone know please let me know.
<form class="form-horizontal" ng-repeat="studiant in studiants" name="form" id="form">
<input type="hidden" name="id" value="{{studiant.studiant_id}}" class="form-control" disabled>
<div class="form-group">
<label for="school" class="col-md-2 control-label">School</label>
<div class="col-md-1">
<input type="text" id="school" name="school" class="form-control" ng-init="studiant.school=studiant.studiant_school" ng-model="studiant.school">
</div>
</div>
<div class="form-group">
<label for="name" class="col-md-2 control-label">Student's Name</label>
<div class="col-md-10">
<input type="text" id="name" name="name" class="form-control" ng-init="studiant.name=studiant.studiant_name" ng-model="studiant.name">
</div>
</div>
</form>
And the script:
document.getElementbyId('name').value = "anything";
Or, if I doing wrong and I have to change the value with ng-model or something, please help me.
http://plnkr.co/edit/bVoljJqiK3CLB2xqZ6Zm?p=preview
You can see a working example there.
Make sure you have the name attr set for the form and the input.
HTML Example:
<button id="dirty-button" ng-click="addText(); myForm.myText.$setDirty()">Make Dirty</button>
<hr>
<form name="myForm">
<input name="myText" id="myTextInput" type="text" ng-model="myText" required>
</form>
<br/>
<span ng-show="myForm.myText.$dirty">it's dirty now</span>
A simple function:
$scope.addText = function() {
$scope.myText = "now I'm dirty";
}
$scope.$on('$viewContentLoaded'){
$scope.form.fieldName.$dirty = true;
}
When your view is loaded then angular calls viewContentLoaded event is called. After that you can set the field dirty. And also if you want to call some method ,that should be executed after the content is loaded than you should call that method inside this $scope.$on('$viewContentLoaded'){..}
This should do the job
angular.element(document.querySelector('form')).scope().formname.fieldname.$setDirty()

Simple login form with php/javascript

I'm working on a simple login in form that verfies one username and one password using php and javascript. I have a index.php a javascript file to check for errors and a results.php. I can't see anything that I'm doing wrong, but the page continues to submit Invalid login in no matter what is input. Looking for some hints if you guys can give me some. Thanks in advance. I have also looked at similar questions on here and my code pretty much matches the others.
results.php
<?php
$user="joesmith";
$password="abc123";
if(isset($_POST['submit'])){
if(($_POST['user'] == $user) && ($_POST['password'] == $password))
echo "You have successfully logged in";
}
else
{
echo "Invalid login! Try again.";
}
?>
index.php
<body>
<form class="basic-grey" action="results.php" method="post">
<div>
<label for="username" id="lUsernameLabel">* Username:</label>
<input type="text" name="username" id="iUsername" onblur="validateUserName()" onfocus="resetUserName()" />
<div id="dUsernameError" class="errorMessage"></div>
</div>
<div>
<label for="password" id="lPasswordLabel">* Password:</label>
<input type="password" name="password" id="iPassword" onblur="validatePassword()" onfocus="resetPassword()" />
<div id="dPasswordError" class="errorMessage"></div>
</div>
<div>
<input type="submit" class="button" value="Submit" />
</div>
your input has name="username" but you are looking for $_POST['user']
You are submitting username input with "username" but trying to check its validity with "user".
Change the part
($_POST['user'] == $user)
to
($_POST['username'] == $user)
it will start to work
Try $_POST['username'] instead of $_POST['user'].
(This would be a comment if I had the rep)

input onfocus not changing div style

I have two input forms. I want the second input form to appear when the user focuses on the first input form. I used a simple javascript "onfocus" within the input tags. This worked fine when it was changing the style of another input form. This however left an unwanted gap. I tried making it so the entire div group appeard onfocus but it had no effect.
I want to make the entire "form-group" div to appear when the user focuses on the first input form but for some reason, this is not working at all.
HTML
<div class="form-group<?php if(form_error('con_password') != ''){ echo " has-error"; } ?>">
<div class="col-md-10">
<input type="password" name="con_password" class="form-control input-lg" placeholder="Password Confirmation" onfocus="occupation.style.display='block'">
<span class="help-block"><?php echo form_error('con_password'); ?></span>
</div>
</div>
<div class="form-group<?php if(form_error('occupation') != ''){ echo " has-error"; } ?>" id="occupation">
<div class="col-md-10">
<input type="text" name="occupation" class="form-control input-lg" value="<?php echo set_value('occupation'); ?>" placeholder="Occupation" onfocus="hearus.style.display='block'">
<span class="help-block"><?php echo form_error('occupation'); ?></span>
</div>
</div>
CSS
#occupation{
display: none;
}
onfocus="occupation.style.display='block' should be onfocus="document.getElementById('id').style.display=block"
onfocus="document.getElementById('occupation').style.display='block'"
In your handlers, the code does not know what "occupation" (for ex.) is. You need to get a reference to it, then manipulate its style.

Categories

Resources