3

I have a website that I have very little control over.

The preset form has selected options I would like set different.

Example of the code is:

<form id="myForm" action="https://secure.domain.net/account/order" method="post">
<table><tbody><tr class="paperTR tr_class_paper"><td class="paperTD fiTitle fiItem">
<div>Paper</div>
<select id="paper" name="paper" onchange="EC_LithoCalc.updateForm()">
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
<option value="4">Option4</option>
</select>
</td></tr></tr></tbody></table>
</form>

I've tried many variants of the example: domain.com/page?paper=1

But nothing seems to work for me.

Everything I find is talking about javascript or jquery, but I don't have any knowledge of those. Though, I know that coding should start with a < tag and what I see just looks like what goes inside.

I'm so confused.

Can anyone out there help me?

I been on this all day and I would appreciate any help.

Thank you.

Lost305
  • 175
  • 1
  • 3
  • 7

4 Answers4

9

This is impossible (or nearly impossible) without using JavaScript code. Following is an example using jQuery (which is a JavaScript library):

Using jQuery, and new URL API, it can be done quite easily. You will have to include jQuery in your HTML. Here is a nice article about how to get it setup on your HTML.

So, to get the query parameter value, and then set it as a dropdown value, you can do something like this:

$(document).ready(function() {
  // Construct URL object using current browser URL
  var url = new URL(document.location);

  // Get query parameters object
  var params = url.searchParams;

  // Get value of paper
  var paper = params.get("paper");

  // Set it as the dropdown value
  $("#paper").val(paper);
});

So #paper matches the ID of the select element and then sets its selected value to what is received in the query parameter.

Related articles:


Update:

I see another library conflicting with jQuery as $ seems to be allocated to something else. To resolve this issue, you can try using jQuery's noconflict. The process to include jQuery in your HTML still remains the same. Just ensure that jQuery is loaded after the library which uses $ as its alias.

You only need to change the code like this:

// Relinquish control of `$` to previous library
jQuery.noConflict();

// Wrap jQuery code in IIFE
(function($) {
  $(document).ready(function() {
    // Construct URL object using current browser URL
    var url = new URL(document.location);

    // Get query parameters object
    var params = url.searchParams;

    // Get value of paper
    var paper = params.get("paper");

    // Set it as the dropdown value
    $("#paper").val(paper);
  });
})(jQuery);

The above simply wraps the previous code in an IIFE to prevent it from clashing with other libraries.

Elsiete
  • 126
  • 13
31piy
  • 23,323
  • 6
  • 47
  • 67
  • Thank you for this information. I read the jQuery info and implemented it. There seems to be a conflict when referencing the jQuery with the code that makes the form. I am able to test the jQuery "Hello, World", but the form disappears and it breaks up the styling of what seems to be jQuery elements. I am unsure if it's appropriate for me to place a link to the page here. Please advise. – Lost305 Aug 07 '17 at 12:54
  • @Lost305 -- yes please! Update your question with the necessary details, and the link. – 31piy Aug 07 '17 at 15:45
  • awesome... the url in question is : http://www.tightdesigns.net/products/businesscards I only have access to a backend form which allows me to only edit the HTML and the HEAD. The language is PHP, the form uses POST and I'm not sure what other information I should provide. – Lost305 Aug 07 '17 at 16:39
  • @Lost305 -- I couldn't find the issue you mentioned in your comment on the link. I suggest to create a new question with the necessary details, since it will become too broad to solve the bugs, and it is out of scope of this question. – 31piy Aug 08 '17 at 04:28
  • I apologize for any confusion. The initial question remains the same. All I need is to get the form on this page: http://www.tightdesigns.net/products/businesscards-digital to display the 2nd option in the Paper option. – Lost305 Aug 08 '17 at 12:38
  • @Lost305 -- There seems to be another library conflicting with jQuery. Please see my update. – 31piy Aug 08 '17 at 14:28
  • You are a genius!! Thank you so much for helping me out. I really appreciate it! Thank you to those who replied to my situation. – Lost305 Aug 09 '17 at 17:27
  • I feel like this should be something possible without JS. Perhaps HTML/browser support should get better for this sort of thing? Perhaps not a high priority... – Drewdavid May 29 '23 at 22:46
1

You're setting paper=1 as a query string parameter but that's not going to change the value of the select control. You'll need to extract the value of the query string parameter, and then set the select control accordingly.

For retrieving query string parameters in JavaScript, you can refer to the marked answer on this post How can I get query string values in JavaScript?

Once you have the value passed in the query string parameter, you can set the select control in JavaScript as follows:

document.getElementById('paper').value = paperValue;

Where paperValue is the variable holding the value passed to the page. Be sure to load/execute the function for setting the selected value when the document is ready.

M3talM0nk3y
  • 1,382
  • 14
  • 22
  • Thank you for your comment Maximo, but I have no idea at what I'm looking at or how to implement it. Can you please elaborate as I have no knowledge of javascript? – Lost305 Aug 05 '17 at 04:24
  • I placed the marked answer in a note, plus the code you wrote and named it script.js. I tried the following urls ?paper=251 ?paperValue=251 ?value=251 but nothing works. – Lost305 Aug 07 '17 at 13:15
  • Also, it shows this... Uncaught ReferenceError: paperValue is not defined – Lost305 Aug 07 '17 at 13:24
  • @Lost305 I made a quick example showing how to get this to work; however, I do recommend you read up on JavaScript so you understand how it works. The example I put together consist of a simple select element with four options; if the page is loaded without a query string parameter or without a parameter you expect, the default selection (Option1) is selected. If the paper parameter is passed in the URL, then the select option is changed based on the value of the paper parameter. You can grab the code from this fiddle I created. https://jsfiddle.net/8zj34pkb/1/ – M3talM0nk3y Aug 07 '17 at 14:08
  • thank you for this example.. I read up on the basics of jquery provided by 31Piy, so I understand it a little bit of what's needed for this to work as I got the "HELLO WORLD" working on the page. What I'm confused about in the example you provided is that the option is already selected in the html. Another issue is that I cannot edit the html displaying the form to change the Select ID from paper to paperSelector as in your example. – Lost305 Aug 07 '17 at 15:19
1

You have given an extra </tr> which should be removed.

You need to submit that form using submit button. Your full code should be like this

<form id="myForm" action="https://secure.domain.net/account/order" method="post">
  <table>
   <tbody>
    <tr class="paperTR tr_class_paper">
     <td class="paperTD fiTitle fiItem">
      <div>Paper</div>
      <select id="paper" name="paper" onchange="EC_LithoCalc.updateForm()">
       <option value="1">Option1</option>
       <option value="2">Option2</option>
       <option value="3">Option3</option>
       <option value="4">Option4</option>
      </select>
     </td>
    </tr>
   </tbody>
  </table>
  <input type="submit" value="Submit">
</form>
Syed Sarek
  • 373
  • 2
  • 12
1

If you aren't using any server language like ASP or PHP, the only way to make this work is either with Javascript or jQuery. It can be set to run after the page is fully loaded.

<script>
    if (document.readyState == 'complete') {
        var item = document.getElementById('paper').value;
        var items = document.getElementById('paper').options;
        for (var x = 0; x < items.length; x++) {
            if (items[x].value == item) {
                items[x].setAttribute('selected', true);
            }
        }
    } else {
        items[x].removeAttributes('selected');
    }
</script>

UPDATE 1:

It seems you really intended on just having a option selected by default, so just add the ´selected´ parameter in the desired option tag.

<option value="default" selected="selected">Default value</option>

If any other option tag had the same parameter, remove it from them.

Tiramonium
  • 557
  • 5
  • 15
  • Thank you, I placed the code into the webpage. When I call domain.com?paper=251 it doesn't change the form. What am I doing wrong? – Lost305 Aug 07 '17 at 13:27
  • Your form uses Post method, not Get. Also, changing the selected value in a select tag only works in an Edit View/Page, since it requires a database or other source of data to know what option was recorded at the time. Long story short, this will only work when/if you have a server language in use. If this is a simple HTML page, just add `selected="selected"` in the option tag you want to be selected by default. Or change the action address in the form tag to this page itself and add the script I put in the answer. – Tiramonium Aug 07 '17 at 15:04
  • 1
    I'm not sure how to determine if the website uses POST and GET, but since there's a Database, I will assume it is GET. The website uses PHP, though I do not have access to the PHP files or DB. I'm just provided a backend form to make changes to the HTML including the HEAD. – Lost305 Aug 07 '17 at 15:25
  • Your `method` parameter in your `
    ` tag will determine wheter the page uses GET or POST. In case you know little to nothing regarding PHP and SQL, go learn it when you can. If that's the case, just rewrite the form tag like this:
    And place a submit button in it: Plus add the script I wrote in the answer to the end of your `` tag.
    – Tiramonium Aug 07 '17 at 15:46
  • Thank you for that information. I looked at the code and the form has the POST method. I am not able to edit the form. Since I cannot edit the form, I would like to have a preselected option other than the default by manner of url with parameters. – Lost305 Aug 07 '17 at 16:34
  • Default option – Tiramonium Aug 07 '17 at 16:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151292/discussion-between-tiramonium-and-lost305). – Tiramonium Aug 07 '17 at 16:42
  • I'm not given access to edit the form code. This is why I would like to link to the page that contains the form with a URL with Parameters that would select the Option I need to display as the default. – Lost305 Aug 07 '17 at 16:43