Passing current page URL via hidden input field within form - javascript

I'm trying to figure out how to pass the URL of a current page via a hidden field so that I can redirect back to that page after the form's input has been handled. I've tried using javascript:location.href, however it looks as though it'll pass that as a literal string.
<input type="url" id="location" name="location" value="javascript:location.href" hidden />
When viewing the page source, I can see that the value of this input box is "javascript:location.href" rather than the page's URL. Any ideas here? Thanks in advance!

You can access the element in Javascript and change the value there
document.getElementById('location').value = location.href;
Example:
http://jsfiddle.net/6zxD5/

I don't think it works that way. You could just use the onload callback to insert it when the page is completely loaded:
<body onload="document.getElementById('location').value = document.location.href">

If your document containing the form is already a PHP file , you can do
$yourPath = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
And in your html you would do
echo '<input type="url" id="location" name="location" value="'.$yourPath.'" hidden />';

You can use JavaScript to set the value of the hidden field:
document.getElementById('location').value = window.location.href;
Example:
https://jsfiddle.net/moogs/mhjh0je3/
Since you also tagged PHP, here's the PHP version:
<input type="hidden" id="location" name="location" value="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" />

Related

call js inside php not worked

I have issue with script js inside php so i want ipnut get the somme of the value return by function js in dispaly in input with condition in php inside the input in below my script php:
function getSum(){
let NotePresence= document.getElementById("NotePresence").value;
let NoteValidation= document.getElementById("NoteValidation").value;
let NoteEvaluation= document.getElementById("NoteEvaluation").value;
let t=NotePresence+NoteValidation+NoteEvaluation;
return t;
}
calling in php:
<div class="form-group" style="display: flex">
<input readonly="" style='width: auto' type="text"
name="NoteFinale" id="NoteFinale"
class="form-control" maxlength="2"
value="<?php echo '<script type="text/javascript">getSum();</script>';?>
size="4" />
<label for=""> /80</label>
</div>
Thanks in advance
You can't put a script element inside an attribute value.
If you want to assign a value to the attribute from JS, then:
Put the script somewhere after the element in the DOM
Find the element with DOM methods (e.g. document.getElementById)
Assign the value to the element with DOM
Note, however, that since you should know the initial values for the three elements you are reading from, you should be able to eliminate JS entirely and just do this with PHP.
$NotePresence = 50;
$NoteValidation = 100;
$NoteEvaluation = 150;
<input name="NotePresence" value="<?=htmlspecialchars($NotePresence)">
<input name="NoteValidation" value="<?=htmlspecialchars($NoteValidation)">
<input name="NotePresence" value="<?=htmlspecialchars($NotePresence)">
<input name="NoteFinale" value="<?=htmlspecialchars($NotePresence + $NoteValidation = $NoteEvaluation)">
That is, assuming you want to combine them when the document loads.
If you want to do it later, then…
In JavaScript you would need to move the code into a function that you call when a suitable event (e.g. change on an input or submit on a form occurs).
In PHP you would need to move the code to the place the data is submitted to and combine the data from $_POST.

pass variable between two .html pages

I basically have a search box, I am trying to get the value inserted and use on a separate /results.htm screen via the submit button. I've been able to get and process the results within the same page; but I am trying to do this after redirecting to a new page
/search.html
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var ZipSearch = jQuery("#Zipcode").val();
});
});
</script>
<div class="search_bar">
<form action=""><input type="text" name="Zipcode" id="Zipcode" value="" maxlength="5" placeholder="Zipcode">
<input type="button" name="submit" id="submit" value="Submit"/></form>
</div>
Want to keep input value content from /search.htm within a variable on next page, separate, /results.htm
How can I keep the user input value and use on my separate 'results' page?
Remove all the JavaScript from search.htm. There's no point in writing custom software to do things HTML has built-in.
Set the action of the form to the URL of the second page (action="/results.htm").
Change the button to a submit button (type="submit")
Read the data from the query string (location.search in JavaScript or $_GET in PHP).

value from ajax jquery json call in input hidden

I get data.my_id from an AJAX call. I want to place it inside a hidden input named my_id, but I am not able to do it. Is there a special way to succeed with this sort of input? Is it normal not to see the AJAX return in the sources of the page? Thanks and sorry for my school English.
// dat.my_id is working in an alert
$(#my_id).html(data.my_id);
<input type="hidden" name="my_id" id="my_id" value="<?= $my_id; ?>" />
You need to keep value of hidden empty initially.
<input type="hidden" name="my_id" id="my_id" value="" />
Then on ajax response you can set the value as below:
$("#my_id").val(data.my_id);
To pass a value to your input you can use
$('#my_id').val(data.my_id);
Some examples

Get variable by user input and use the variable in form URL

I have a problem with using variables in URL's. The reason I need to do this in JavaScript is because I can't use PHP (Phonegap).
<form action="https://website.com/login.dll?Logon" method="post" id="logonForm">
<input type="hidden" id="curl" name="curl" value="Z2FZ2Fexample.phpZ3FZ26idZ3D<?php echo $_SESSION['user_email'];?>Z26typeZ3D0" />
This works, but now I am using Phonegap so I can't use PHP, So I need to get a variable set by the user and then use that variable in the URL (instead of the php echo) I guess. I'll still be using the same form.
So I need the user to enter the info I need and pass that to a variable,
<input id="data" type="text" name="data" placeholder="Placeholder" required />
What is the best way of doing this in Javascript?
Add another attribute to the form
onSubmit = 'setCurlValue();'
Add code for setCurlValue
function setCurlValue() {
$('#curl').val('Z2FZ2Fexample.phpZ3FZ26idZ3D'
+ $('#data').val()
+ ';?>Z26typeZ3D0');
}

Make form redirect users after posting

I need to change a form so that instead of reloading the page after submitting it, it redirects the user to another page.
This is the form I'm talking about:
<form class="questionform" name="questionform-0" id="questionform-0">
<textarea class="question-box" cols="12" rows="5" id="question-box-' . $questionformid . '" name="title" type="text" maxlength="200" size="28"></textarea>
<input type="text" class="ubicacion" value="" name="question">
<input type="button" name="ask" value="Publicar" onclick="askquestion('questionform-0'); window.location.reload(true);">
I want to remove window.location.reload and change it for something that redirects users to the page their comment will appear.
The problem is that it's not simply a static. So I have no idea how to do it. The URL I want to send users to is:
www.chusmix.com/s?=(content of the second field)
How do I do it? Anyway thanks for any info or whatever that points me on the right direction. Thanks
There is no need to use javascript for this purpose. You only need to set the action attribute of the form tag. It tells where the form information will be sent. So in your case it will be:
<form action="http://www.chusmix.com/s">
Also if you want to send the variables through the URL like: http://www.chusmix.com/s?variable=someValue
You need to set the method attribute as get so it will look like this:
<form action="http://www.chusmix.com/s" method="get">
If you don't want the data sent to be visible set the method to post, note that there are different advantages for each method so i recommend you read more about this if this form is an vital part of your webpage.
The variable names that appear in the url http://domain.com?**variable**= will depend on the inputs name <input type="text" name="**variable**" />
For more information on how forms work you can go to:
http://www.w3schools.com/TAGS/tag_form.asp
http://www.tizag.com/phpT/forms.php
You can ad an action:
<form action="redirection_url.php" method="POST" class="questionform" name="questionform-0" id="questionform-0">
I think you can use both absolute and relative url. Also note that I've added
method="POST" - which defines how the data from the form will be sent, as you already send some data with GET method (that's the stuff after ? in your url) - so this should work pretty well.
If you cannot use the action attribute in the <form> tag, you may redirect the user using window.location (you will probably want to do this inside the askquestion method, not in the onclick attribute).
window.location = "http://www.chusmix.com/s?=" + inputValue;

Categories