Passing on page elements to PHP(database insertion) - javascript

I have a member profile page which displays an username in regular text,(h1). I can capture the username using Javascript using this code
<script type="text/javascript">
var userID = document.getElementsByClassName("display-name")[0].innerHTML;
</script>
Q: What I want to know is, is there a way to get that variable and sent it back to my database? And add the username if it's an unique username.
P.S.
- I'm running a Wordpress site, database is (phpMyAdmin).
- I've read the $_POST methods for javascript/php transfers. But I don't see how I can apply it here, because there is no form to submit anything in.
- The username gets generated by itself (using a plugin) that's why I'm doing it this way.

You probably want to use an AJAX request and send it back to your server, without having to have a usual form/submit. Check this jQuery Ajax POST example with PHP

Related

Is there a way to get the form data easily with JavaScript?

I was looking on how to get values of form data that are passed with POST requests and found this answer.
I know you you can get GET query parameters quite easily in JavaScript doing window.location.search but is there a way to do similar for POST request or document.forms is my only option?
To expand on what RUJordan said:
When you do a POST, the information is sent to the server using an entirely different method and does not show up in the URL or anywhere else that JavaScript can get access to it.
The server, and only the server, can see it.
Now, it is possible to take some of the form data and fill JavaScript variables and/or hidden form fields so the server can pass data back down to the client.
If you need more help, you'd be better off opening another question explaining exactly what problem you are trying to solve.
Do you want javascript to see the data was POSTed to load the current page?
JavaScript does not have access to the request body (where the POST content is) that loaded the page. If you want to be able to interact with the POSTed parameters, the server that received the request would need to respond with the necessary data written back out on the page where javascript can find it. This would be done after the form was submitted, as part of the response to that POST request.
Or do you want to know what your page could POST form the forms that are on it?
Inspecting document.forms will let you see what could be POSTed later if those forms were submitted. This would be done before the form was submitted, without a request being made.

Simple page authentication

I'm trying to hide access to a certain page on a rails app without using something like devise or sorcery. I want to create a password inside the actual app code, so when you access the page it just shows an input, and if the password is entered is correct, it will show the page. Could I get this done with Javascript?
Just to be clear, I'm not in favor of doing authentication on the front-end since it is very very insecure. But if you don't need that level of security you can use this tool:
http://www.javascriptkit.com/epassword/index.htm
That would generate an encrypted password checker which would work to protect your page.
You could do an AJAX request to check if the password is right, then when the AJAX response finishes, call a callback that shows the div where the page is.

Javascript validation for duplicate username

I have created 2 pages in asp.net login and signup.
Now I want to check whether the username entered is already present before inserting data into database on signup page. I want to do this validation using javascript.
I tried doing it using compare validator but it did not worked.
Here are some way to do this.
You send all the username to the javascript when the page load and validate against the username list.
You make an AJAX query to your server to validate the username from the client side.
When the user submit the signup form, you validate the username received against your database and send an error if you find it.
You can use ajax to call a server side method that verify if the username already exist. This can be done very easy with JQuery. Look at the documentation here: http://api.jquery.com/jQuery.ajax/

how to get return value from server after POSTing from a html form

I have a form :
<FORM name="loginForm" ACTION="https://www.example.com/cgi-bin/login.pl" METHOD="post">
The form submits a username and password to my perl script on the server which verifies the password is correct. However since my page login.html is a static page is it possible to have the Post return the value to me somehow in a javascript that will display to the user an error message if the password did not match for example, without having to go to a different page.
This can obviously be done by the server, serving a new page, but that's not what im looking for. Any ideas?
You want to use XMLHttpRequest to post the form contents from JavaScript.
If you're using jQuery, there is an example on the jQuery.post() page:
$.post("test.php", $("#testform").serialize()); // Assumes form id is "testform"
See the bottom example on the page ("Example: Post a form using ajax and put results in a div") to get more complete code for how to get the return value form the server.
There're many solutions for your problem. A simple one is in case of login failure let your login.pl returns an html containing a "redirect to login.html" element and with parameters indicating the error, the html looks like:
<META HTTP-EQUIV="Refresh" CONTENT="1; URL=login.html">
Your login.html should contains logic (in Javascript) to check parameter for error and display it.

How to get value of hiddenfield in another form

iIhave value in hdnField in form1.aspx . I assign a value to hdnfield in javascript .I want to get that value in aspx.vb in another form, form2.aspx. How can I accomplish this?
If your Form1.aspx submits to Form2.aspx, then you have atleast a few ways to access the value of form fields (including Hidden fields):
The Request.Form property exposes a NameValueCollection containing all submitted form field names as Keys and their values as Values. You could use the syntax Request.Form["fieldName"] to access the value.
If this is ASP.NET 2+ and you used the Cross-page posting technique, you will be able to access field values in the previous page using the PreviousPage property of the Page.
If you use Server.Transfer, you can access values using the Current HttpContext.
If you need more info, you should take a look at Passing values between pages in ASP.NET.
I think your concept of session is wrong. Session is a server-side object, and javascript runs on the client, so you can not directly assign that value to a session. You can, instead, use some AJAX to send it to the server and then add code in the server so the value is assigned.
Hm... you have to think about the difference between serverside and clientside first...
You cant directly access changes you made in the client on the serverside, because you once send a request to server, as a response you get the site diplayed in your browser. As soon as you recieve the request, the server is finished and cant access the site anymore. Its like a letter you send away. As soon as you put it into the mailbox, you cant make changes anymore.
But you could post a new request to the server and add POST or GET peremeters. These can be accessed by the server. The way you send the request doesnt matter... you can send a request using AJAX or simply reload the page.

Categories

Resources