Insert textbox value based on logged in user? - javascript

Good day
I would like to do the following. I am using Joomla and RSForm Pro component.
I'd like to fill a textbox with a certain value based on who is logged in.
Something like this:
<?php
$user = JFactory::getUser();
if($user->id==42) { ?>
<script type="text/javascript">
$(document).ready(function(){
$("#Email1").value = "testmail#test.com";
});
</script>
}
?>
However, in RSForm pro there is 3 different PHP "areas". They are:
Script called on form display (HTML code of the form)
Script called on form process (POST Data)
Script called after form has been processed (Thank you Message)
They also mention to not include the "php" opening and closing tags, so I don't understand how I'm supposed to use the php tags when I want other scripts such as Javascript involved.
Basically, it must just set the value for 5 fields, textboxes #Email1 to #Email5, depending on the user which is logged in. When the form is submitted, the users will still receive the values.

RSForm!Pro has a hidden feature where you can assign dynamic values to textboxes. Essentially, you can add any PHP code to the field's default value and it will get executed, as long as it has the string inside it, and as long it doesn't have the PHP closing and ending tags. Check this comprehensive post on how to assign dynamic values to fields in RSForm!Pro.

Related

How to trigger javascript pop-up box by if-condition within the HTML code?

I created an html site where the user can input a code in a text input and then this code is being inserted in a MYSQL database if certain conditions are met. In case of a successfull database entry I would like to notify the user with a little pop up message, which displays something like "Success" and also stating some data from the table row, the code was inserted into.
I found a nice looking pop up message on the following page, which I would like to use: https://www.gitto.tech/posts/animated-modal-box-using-html-css-and-javascript/.
However, in the implementation from the page, the pop-up is triggered by the click of a button:
document.getElementById("open-popup-btn").addEventListener("click",function(){
document.getElementsByClassName("popup")[0].classList.add("active");
});
document.getElementById("dismiss-popup-btn").addEventListener("click",function(){
document.getElementsByClassName("popup")[0].classList.remove("active");
});
I would like to execute the pop-up based on an if-condition, in which I check whether an php variable is already set:
<?php
if (isset($group)) {
?> ......HTML code.....
So, can anybody tell me how to successfully remove that "onClick" function of the pop-up?
Thanks in advance!
I understand that you reload the page after said data was inserted into database?
If so, what you can do to show pop-up initially, is to simply add class active to your popup (element with class popup, like that:
<div class="popup center active">
...
</div>
BUT
in order to be able to close the popup, you still will have to attach the second part of the provided script (you can simply insert it within script tags inside php if statement (at the end of HTML body element), like:
<script>
document.getElementById("dismiss-popup-btn").addEventListener("click",function(){
document.getElementsByClassName("popup")[0].classList.remove("active");
});
</script>
note: It doesn't change the fact that, as said above - it would be better to handle this with XHR (e.g. via ajax)

Dependable textbox autocomplete method

page imageThis Images shows my page where i am facing a problem, i want my 'company' textbox get autocomplete by value fetched from database when i input a value in 'code'.
in a seperate page mysql queries will run to get company where code='*'
Both 'code' and 'company' are present in my databasedatabase image
Tried lots of techniques but failed to achieve anything.
Please help!!
Give us some code please.
You want to submit form and then, on new page - enter a value to new form with second textbox?
As you probably know - if you want to get data from database you must use classic SQL query in PHP file.
But - if you want do dynamicaly autocomplete second textbox without submit - you can use jQuery:
$('#your_textbox').on('input', function() {
/* This will be fired every time, when textbox's value changes. */
$('#your_second_textbox').val($('#yout_textbox').val(''));
} );

Display hidden div after successful form upload

I'm using a shortcode as part of my membership setup which allows users to upload files directly to their contact record in my CRM. This displays a multi-upload form which I have then styled:
Once the files are uploaded the page refreshes and a success message appears. On refresh I also need to display a hidden div (#hiddendiv) underneath the success message.
From what I've read there is a JS/PHP combination that could potentially achieve this (such as this example - although this is quite vague and difficult to adapt).
Can anyone assist me/point me in the right direction to achieve this?
Since your form page is refreshing then you obviously not using AJAX, therefore instead of putting the div as html what you can do is echo a assign a variable initially it should be empty then, then echo that variable where you wanna show the div, this will echo empty message at the begging... then upon successfully validating your form and on success then assign the content of that div to the empty variable once the page loads its gonna show the div content u just assign
<?php
$hiddenDiv = "";
if(isset($_POST['submitButton'])){
//validate what you need to validate on
//On success assign value to hidden div
$hiddenDiv="<div class=\"whatever\">What ever dv content you need to display</div>";
}
?>
<!-- the place you wanna show the div -->
<?= $hiddenDiv?>
You could use the jquery switch class. This will allow you to do CSS transitions to make the appearance of the element smoother. This could be part of 'a' ajax response.
You could also just do something as simple as
$('#hiddendiv').css({"display":"inline"})
EDIT ** changed the to 'a' in reference to ajax.
Try like below:
<?php
if(isset($_POST['whatever'])
{
$display='';
}
else
{
$display='none';
}
?>
<div style='display:<?=$display?>'></div>

php, scroll page to specified html control on form

First question after starting with netbeans and wamp server 2 days ago. Only have some desktop vb background, so my jargon may be off. In vb I would want to set the focus on a control.
I have a long, as in a lot of scrolling, webpage form in html that displays the way I want it. There are about 300 controls, almost all checkboxes.
When the Submit button is clicked at the bottom of the form I want to find any incomplete data, scroll the page to that control and give the user a message so I don't insert unfinished records.
How do I do that?
I think you need to validate data with javascript and with a function to do scroll. Check this:
scroll to div ajax/javascript
In the form, add the 'required' attribute to each input tag.
EX:
<input type="text" required>
When the form is submitted, the first input tag with the 'required' attribute will be scrolled to, and the user will be notified.
What you are looking for is called validation. You should validate data on the server with PHP to make sure that the data meets your defined criteria (since the data could come from a manipulated form and users can deactivate javascript). You should also look into escaping data, for example with mysqli_real_escape_string to prevent SQL injection (an attack where a malicious user enters SQL statements in your form to alter/expose data in your database). And you should sanitize your data.
But that is all server-side. What you also want is to have a good user experience and not even send data to the server unless is is valid. This is done with javascript on the client side. The upside is: Immediate feedback without a page load. But it has little to do with security since it's easy to manipulate that or turn javascript off.
I would recommend using jQuery and then a plugin like jQuery Validation. And for the scrolling part you could use jQuery scrollto. You could combine those two with a callback from jquery validation. For example (just a quick and dirty example):
$(".form-selector").validate({
invalidHandler: function(event, validator) {
// 'this' refers to the form
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? 'You missed 1 field. It has been highlighted'
: 'You missed ' + errors + ' fields. They have been highlighted';
$("div.error span").html(message);
$("div.error").show();
// Now scroll to the first error message
var first_element = validator.errorList[0].element;
$(window).scrollTo(first_element);
} else {
$("div.error").hide();
}
}
});
But the best thing would be to start with a jQuery tutorial and then dig into that.

Input Validation & Return with either javascript or asp

I need to know how to make a order look up validation.
For example if order number 2005 is entered into the field,
i want it to pull a specific html page.
If entered number is anything other than 2005 ,it will pull another html page.
Will be great if u guys can show for asp or java script with the .document write innerhtml for java script
how to do do this with javascript or asp?
There are a few ways you can go about this. You can use JQuery or Javascript to make an AJAX request to validate the OrderNumber first. If Valid, that field gets posted back to the Form which envokes the response object to go to the page desired.
dim strOrderNumber
strOrderNumber = request("txtOrderNumber") ' Or whatever field name you gave this object
' Page gets posted and picks up here
if len(strOrderNumber) > 0 then
' Be sure to close any opened DB connections and destroy any objects that were created
response.redirect("pagename_" & strOrderNumber & ".asp")
end if

Categories

Resources