• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

need help adding a javascript var to a form url

Contributor ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

Hi all,

I need help adding a javascript var to a form url.

So the form url is like:

"url": "https://mypage/info/230",

but I want to replace the number 230 with var MyNum like:

var MyNum = 230;

"url": "https://localhost/mypage/info/" + MyNum + "\"",

but it doesn't seem to work

Is there a better way to write it?

var settings = {

  "async": true,

  "url": "https://localhost/mypage/info/230",

  "method": "Post",

  more stuff.....

}

Views

3.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 02, 2017 Dec 02, 2017

Copy link to clipboard

Copied

Hi - I could still use some help with this - thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

You can just change the .action property in JavaScript. Short example:

Example HTML:

<form id="myForm">

<input type="text" id="theInput" value="/script.php">

<button type="button" id="changeForm">Update Action</button>

</form>

Example JS:

(function(){

document.getElementById('changeForm').onclick = function(e) {

    document.getElementById('myForm').action = document.getElementById('theInput').value;

  }

})();

JSFiddle: JSfiddle

This is just assigning a click handler to the button that adjusts the .action property of the form "myForm" to whatever you put in the input text field.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

Can you not set it as below:

<script>

var MyNum = 230;

var url = "https://localhost/mypage/info/"+MyNum;

var settings = {

"async": true,

"url": url,

"method": "Post"

};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 03, 2018 Jan 03, 2018

Copy link to clipboard

Copied

Can you explain what exactly does not work? Maybe we can pick up from there.

Ia there an error message?

What is the behavior and what is the expected behavior?

Thabk you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 11, 2018 Jan 11, 2018

Copy link to clipboard

Copied

LATEST

Watch out for typos like setting the object value how you are.

e.g.: "url": "https://localhost/mypage/info/" + MyNum + "\"",

That is setting the property .url to: https://localhost/mypage/info/MyNum"

Note the double quote at the end because you specified to end it with "\"". It doesn't take much to break things like that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines