Hi I worte a js client side and rendered that server side aswell when i user filling the form the Username field need to be simultaneously filled based on Email input before submiting the form.
My query is in the Email field and Username field the values "UserName"
and "Email" are appearing by Default and also i cant edit the email field the forms is able to submit with valuse UserName and Email which is not i am looking.
the below code is my client side JS
<asp:TextBox ID="Email" runat="server" onkeyup="change();"></asp:TextBox>
function change(){
document.getElementById('ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_UserName').value = 'UserName';
document.getElementById('ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_Email').value = 'Email';
UserName.value=Email.value;
}
The below is my C# which i used stringbuilder
protected override void RenderContents(HtmlTextWriter writer)
{
System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
sb2.AppendLine(#"<script language='javascript'>");
sb2.AppendLine(#"function change(){");
sb2.AppendLine(#"var Email= document.getElementById('#ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_Email');");
sb2.AppendLine(#"var UserName= document.getElementById('#ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_UserName');");
sb2.AppendLine(#"UserName.value=Email.value;");
sb2.AppendLine(#" }");
sb2.Append(#"</script>");
if (!Page.ClientScript.IsStartupScriptRegistered("JSScript"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb2.ToString());
}
The reason i added my code into C# stringbuilder is i am unable to get the java code on clientside when checked using f12 i am not able to see any of the java code which i wrote on aspx page so i used string builder to get that code.
Thanks
I managed to fix the issue.
I have used the below code
function change() {
var Email = document.getElementById('ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_Email');
var UserName = document.getElementById('ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_UserName');
UserName.value = Email.value;
}
System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
sb2.AppendLine(#"<script language='javascript'>");
sb2.AppendLine(#"function change(){");
sb2.AppendLine(#"var Email = document.getElementById('ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_Email');");
sb2.AppendLine(#"var UserName = document.getElementById('ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_UserName');");
sb2.AppendLine(#"UserName.value=Email.value;");
sb2.AppendLine(#" }");
sb2.AppendLine(#"</script>");
if (!Page.ClientScript.IsStartupScriptRegistered("JSScript"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb2.ToString());
}
Thanks
Related
I am trying to build login pages with Gin-gonic, but I got trouble in redirecting browser.
main.go
router.GET("/login", getLoginPage)
router.POST("/login", authentication.Login)
router.GET("/dashboard", showMainPage)
If user press the button in html
login.html
<input a href="javascript:void(0);" class="btn btn-primary btn-user btn-block" id="loginButton" value="Login"></input>
Then, Javascript function will be working. The reason why I coded like this is that, I learned that UserID and Password should be hashed before sending to server. (Due to some sort of security problems like tapping, as far as I know)
login.js
document.getElementById("loginButton").addEventListener("click", tryLogin, false);
// Get user inputs like ID and PW
var request = new XMLHttpRequest();
request.open("POST", "/login");
request.send(formData);
And now, router.POST("/login", authentication.Login) will work.
auth.go
func Login(c *gin.Context) {
id := c.PostForm("id")
pw := c.PostForm("pw")
// Hash password again, Check Validation and Find user in database
// If all inputs are correct, Logged in.
c.Header("Content-Type", "text/html")
c.HTML(http.StatusOK, "dashboard.html", gin.H{
"title": "title of my page",
"username": "I want to send some data like this",
"usernickname": "TyeolRik",
}) // But the thing is c.HTML not directing as I want. but it returns well checked in Postman.
}
But c.HTML doesn't showing rendered HTML screen in browser (Chrome and Edge checked) and also, c.Redirect() doesn't work neither.
auth.go
func Login(c *gin.Context) {
id := c.PostForm("id")
pw := c.PostForm("pw")
// Hash password again, Check Validation and Find user in database
c.Redirect(http.StatusMovedPermanently, "/dashboard")
}
How can I redirect HTML easily? I temporarily used window.location.href = '/dashboard'; in Javascript. But It cannot be rendered like using gin.H{something}
Is there any tips for me?
Thanks to #mkopriva, I found out that XMLHttpRequest() in javascript doesn't work with *gin.Context.HTML() of gin.gonic
Browser doesn't redirect with XMLHttpRequest() as server returns.
Final code (works well)
login.js
function tryLogin() {
const email = document.getElementById("loginFormEmail").value;
const pw = document.getElementById("loginFormPassword").value;
var hashedPW = sha3_512(pw);
var hashedForm = document.createElement("form");
hashedForm.setAttribute("charset", "UTF-8");
hashedForm.setAttribute("Content-Type", "multipart/form-data");
hashedForm.setAttribute("method", "POST"); // Post
hashedForm.setAttribute("action", "/login"); // URL destination
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "email");
hiddenField.setAttribute("value", email);
hashedForm.appendChild(hiddenField);
hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "password");
hiddenField.setAttribute("value", hashedPW);
hashedForm.appendChild(hiddenField);
document.body.appendChild(hashedForm);
hashedForm.submit();
}
When server validate POST-ed login, then
auth.go
// Some validation
// If ID and PW is right,
c.Redirect(http.StatusMovedPermanently, "/dashboard")
This works.
I wanted to ask how can i get the values of the Javascript Input and store it into a php value so i can post this data into Sqlite3. Im receiving user inputs from the Javascript Prompts. Is there another way to accomplish this also. Any help would be greatly appreciated.
function myFunc(){
var code = prompt("Please enter authorized code twice for security purposes: ");
var email = prompt("Please enter email twice to continue: ");
if(code==""||code==null||code!="1234"){
//Handle Error
window.location.href="error.html";
}
}
document.onreadystatechange = () => {
document.addEventListener('readystatechange', event => {
if (event.target.readyState === "complete") {
myFunc();
}
});
}
Using jquery you can use the $.post method:
function myFunc() {
var code = prompt("Please enter authorized code twice for security purposes: ");
var email = prompt("Please enter email twice to continue: ");
var url = "phpToGetInputs.php";
var data = {
code: code,
email: email
}
$.post(url, data); // "send the data to the php file specified in url"
// code...
}
document.onreadystatechange = () => {
// code...
}
Then, in your PHP file (that you specified as the url)
phpToGetInputs.php:
<?php
if(isset($_POST['email'])) {
$email = $_POST['email']; // get the email input (posted in data variable)
$code = $_POST['code']; // get the code input (posted in data variable)
// do code that requires email and code inputs
}
?>
Use a jQuery post request to send the variable from javascript to php.
$.post([url], { "data" : text });
Look at this website for more information: https://api.jquery.com/jquery.post/
I have this HTML form and try to validate at client side.
This is the validation function at HTML form.
function registerValidate() {
var have_error = "No";
var email = $('#userEmail').val();
var pwd = $('#password').val();
var pwdr = $('#passwordr').val();
var email_re = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*#([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
if (!email.match(email_re)) {
$('#email-error').html('Please enter a valid email');
have_error = "Yes";
}else{
$('#email-error').html('');
}
}
When I click "Create My Account", all texts filled inside the text inputs disappeared and doesn't show the error message, even it should show error message.
What could be wrong?
When do you call this function? On form submit? Maybe you form have been submitted and all data field cleared.
Could you provide source code?
I'm creating a website in ASP.NET (Framework 4.0).
On this site I need to automatically want to Bind city in dropdownlist based on the user's location.
Can I get name of user's city based on the IP address of the user ?
Code for dropdownlist
public void BindCity()
{
try
{
// ddlCities.DataSource = obj.GetCity();
DataSet ds = obj.GetCity();
ddlCities.DataSource = ds;
ddlCities.DataTextField = "CityName";
ddlCities.DataValueField = "CityId";
ddlCities.DataBind();
ddlCities.Items.Insert(0, "Select City");
}
catch (ArgumentException ex)
{
//Label1.Text = "Please Select Country";
//ScriptManager.RegisterStartupScript(Page, Page.GetType(), "warning", "$('#warning').modal();", true);
}
}
No, there is no way to do this using just the Framework. You need to query some type of whois service for that type of information.
I've been trying to pass an ASP.NET variable to a javascript function without any luck so far.
What I have is:
A master page that is using several .js files under folder root/js
I'm trying to create a public variable that contains the username of a person and would like to send it to a function that is inside one of the js files mentioned above.
public string username;
...
username = User.Identity.Name.ToString();
my js is:
$(document).ready(function {
var username = "<%= username %>";
var element = document.getElementById("userid");
element.innerHTML = username; });
After executing this I get <%= username %> and not the actual value.
I tried a second approach:
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "customScript", "AssignValue('" + username + "');", true);
but I get function (e,t){return new x.fn.init(e,t,r)} as a result... I don't know what to do.
Any ideas will be highly appreciated!
Thank you!
// aspx
<asp:HiddenField runat="server" ID="hfUserName" />
// page_load:
hfUserName.Value = username;
// js
$(function() {
$("#userid").text($("input[id$='hfUserName']").val());
});