0

I want to add html parameters to the url with onsubmit. I have 2 forms(1 GET, 1 POST), want to use 1 button to submit them both(Using the POST form button) When the submit button is pressed use onSubmit to call a javascript function, where parameters are "appended" to the url.

I was thinking about doing something like this:

function onSubmitForm() {
    url = "localhost:8080/test/index.php?Title=document.getElementsByName('title')[0].value";
    //I don't know how to actually call the url.
}

EDIT

I got what I wanted: Appending form input value to action url as path

2 Answers 2

2

First, you have to concatenate the string's static and dynamic parts. Then, you can redirect.

function onSubmitForm() {
  window.location = "localhost:8080/test/index.php?Title=" + 
    document.querySelector('title').textContent;
}

NOTES:

  • Only form fields have a .value property. If you are trying to get the text within an element, you can use .textContent.
  • .getElementsByName() scans the entire document and makes a collection of all the matching elements. That's wasteful when you know you want only the first one and, in the case of <title>, there is only ever going to be one of those in a document anyway. Use .querySelector() to locate just the first matching element.
Sign up to request clarification or add additional context in comments.

2 Comments

Can't seem to get it working. I had the code done, It was 3 lines of code yet I somehow managed to delete it. It was a stackoverflow post that I got it from and now that I've looked everyone is suggesting window.location which isn't working in my case and I unfortunately can't get it working. Thanks anyway tho.
@CrimzonWeb You need to make sure that this code runs after the entire HTML document has been parsed. As such, place this code into a <script> element and insert that element just before the closing body tag (</body>). As long as your <title> element has a value, this will work.
1

ES6

NOTES :

  • Don't forget to use Babel for converting your code in ES5.

I purpose to you this solution :

function onSubmitForm() {
  window.location = `localhost:8080/test/index.php? Title=${document.querySelector('title').textContent}`
}

This way of doing with backtick is even simpler than in ES5, let me explain, before we had to concatenate with the sign + ourVariable + the continuation of our string of character.

Here we have more of that. And we can write on several lines as well. Then ${} is used to pass a variable inside

Documentation if you want : Literal template string

5 Comments

This is not an answer. It doesn't explain what is being done and why.
You explain why on your response. I thinking it's important to know the good pratice with ES6 :)
What if my answer gets removed? Then, how helpful is your answer?
This is a little better, but it would be best to include a link to the documentation on ES6 templated strings. Answers should always be as complete as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.