0

I was following the accepted solution to this question : HTML Dropdown list with user input field however I am unable to get it to work. I updated the jquery link, modified it to what i wanted but it doesn't insert any value and I was hoping someone could help me with that.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

Testing This:
<script language="javascript">
$(document).ready(function(){

    $("#addLocation").click(function(){
    $("#locationContainer").append('<option value="' + $("#newLocation").val() + '">' + $("#newLocation").val() + '</option>');
    });

});
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<select name="imprint_location" id="locationContainer">
    <option value="None" selected>None</option>
    <option value="Front Right Breast">Front Right Breast</option>
    <option value="Front Left Breast">Front Left Breast</option>
    <option value="Full Back">Full Back</option>
    <option value="Half Back">Half Back</option>
    <option value="Barrell">Barrell</option>
    <option value="Clip">Clip</option>
</select>

    <input type="text" id="newLocation"/>
    <input type="button" id="addLocation" value="Insert"/>
</body>
</html>

Get value to save to file: Would something like this work? Found on another question.

<?php
if(isset($_GET["filename"]) == true && isset($_GET["log"]) == true){
    $fp = fopen($_GET["filename"], "a+");

    if($fp !== null){
        fputs($fp, $_GET["log"] . "\r\n");
        fclose($fp);
    }
}
?>

But how do i get the javascript value into this?

Thanks, MsKazza

2
  • 1
    Do you get any error in console? Also, put jQuery code below jQuery link (script src) Commented Sep 14, 2016 at 12:47
  • 2
    Because you load jQuery after your script that uses jQuery. It is like trying to eat a pizza before it is made. Open up your developer console, I am sure you have a nice error message. Commented Sep 14, 2016 at 12:57

1 Answer 1

1

Add the jquery src before your javascript codes

Sign up to request clarification or add additional context in comments.

5 Comments

Thats great it works now, however i was under the impression that it was supposed to save that input for next time, no? i.e. if the page is refreshed or next time you go to the page should that input not now be an option on the dropdown?
You should save the inputs if you want to show next time
Save them into database or file (xml or json type)
i'm sorry, yes i figured that lol i didn't ask properly. How can i save from the javascript input field into a file? i assume i somehow take the value #newLocation and use a write command to add to a file. please see edit above
You can call the code with Ajax and save it into file. Search "jquery ajax". But you dont need php. You can do this with javascript.

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.