Appending Form Input Value to Action Url as Path

Appending form input value to action url as path

You can use onsubmit attribute and set the action inside a function for example:

<form id="your_form" onsubmit="yourFunction()">
<input type="text" name="keywords">
<input type="submit" value="Search">
</form>

function yourFunction(){
var action_src = "http://localhost/test/" + document.getElementsByName("keywords")[0].value;
var your_form = document.getElementById('your_form');
your_form.action = action_src ;
}

How to append HTML form data into the URL as path and not as a query string?

You can create javascript function or you can declare a backend function, and return a redirect to your request route

document.querySelector('form').addEventListener("submit", function (e) {
e.preventDefault()

let param = document.querySelector('input[name="myparam"]').value

window.location.href = 'https://mywebsite.com/mypage/' + param
})

html form input method get prevent name in url if value is not filled

You can use this plain JavaScript to remove the empty inputs from submitting.

<form method="GET" id="myForm">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>

<script>
Form = document.getElementById("myForm");
Form.addEventListener('submit',()=>{
if(Form.fname.value == ""){
Form.fname.name ="";
}

if(Form.lname.value == ""){
Form.lname.name ="";
}

Form.submit();
});

</script>

Is it possible to have an input from a form in a html template be added to the url in a django project?

Have your input form post to a new view that retrieves the field value and then redirects to a your wiki entry based on that value

html

<h2>Wiki</h2>
<form action="{% url 'wiki-lookup' %}" method="post">

views.py

def wiki-lookup(request):
#first we get the posted q term, or return 'notfound' if the form fails to provide a value
term = request.POST.get('q', 'notfound')
#....test term for validity here as appropriate
#next redirect to 'entry' passing the term as the name value for that URL
return redirect('entry', name = term)

urls.py

...
path('wiki-lookup/', views.wiki-lookup, name='wiki-lookup'),

Django is actually quite neat in that views don't have to be actual pages - they can just do some work to determine which page the user should actually end up at.



Related Topics



Leave a reply



Submit