0

I'm trying to get data from an API and post it to a database, the form I have worked when you manually input data. but when you set the data from the API request - it updates on the page. Although shows blank when it posts.

I'm using

document.getElementById('Title').value = item.volumeInfo.title;

to get the "Value" in an Input.

and

    <div class="form-group">
            <label for="name">Authors</label>
            <input class="form-control" type="text" value="" id="Author" ng-model="book.Author" required="required"/>
        </div>

to attach to form.

<button class="btn btn-primary" ng-disabled="AddNewForm.$invalid ||
isUnchanged(book)" id="add-new-btn"
ng-click="New_Book(book)">Add</button>

Why is it submitting as blank?

6
  • What is the type of your input ? Commented Apr 17, 2018 at 15:57
  • stackoverflow.com/help/mcve Commented Apr 17, 2018 at 15:57
  • 1
    My Input in just "text" Commented Apr 17, 2018 at 16:00
  • Please, post something that we can work with. It is impossible in the current state. Commented Apr 17, 2018 at 16:02
  • 1
    How do you submit the "form"? What do you do before submitting? Any event listeners? Commented Apr 17, 2018 at 16:09

2 Answers 2

1

Your form elements submit their value via their “name” attribute... does you input element generate a name attribute? If not, this is why it won’t work.

Eg

<input name=“foo” value=“bar”/>

Submits as:

?foo=bar
Sign up to request clarification or add additional context in comments.

3 Comments

Really not sure what you mean, sorry! can you explain it differently?
If you are trying to submit your form via HTTP or serializing the content you will need a name attribute as that is what the browser uses for each key/value pair. The browser does NOT use the id attribute.
Ahh I see! Although the form does post to the database If i manually type in the data (Its angular to pass data from the database) not sure how you would use the name attribute.
0

Your question isn't clear enough for me. But I've tried to make a quick example and make it as general as possible to let you work with your form inputs!

Here is a Live Preview

HTML

<form action="post" id="myForm">
   <input type="text" id="title">
   <input type="submit" value="Click To Update">
</form>

<h2 id="result"></h2>

JavaScript

// Select our main elements (form, text input, result heading)
const form = document.getElementById("myForm");
const titleInput = document.getElementById("title");
const result = document.getElementById("result");

form.onsubmit = function(e) {

  // Don't refresh the page
  e.preventDefault();

  // Set the input value into the result heading
  result.innerHTML = titleInput.value;
}

This solution allows you to write anything in your input, and after submitting your form. You can use that data elsewhere.

3 Comments

I think I may have phrased the question badly, I enter a book name in a search button, it uses the google books api to populate the form with the js value function. so the data is there in the page, but when I submit it, its blank. Although if I manually type into the form, it posts fine.
Good. Are you using an Event Listener when you are passing the data? For example; I used onsubmit event to pass the titleInput value to the result heading.
I have "ng-click="New_Book(book)" on my form that posts it into Angular, if thats what you mean?

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.