Sending SCROLLTOP javascript via Submit - php

I'm trying to submit a form after updating the value of a hidden field from a javascript variable.
This is the code:
<form name=form1 id=form1 method=get action=gestionale.php>
...
...
<input type=hidden name=scrolltop id=scrolltop value=''>
<input type=button name=update value=Update onClick=vai('form1');>
</form>
<script>
function vai(formid) {
document.getElementById('scrolltop').value=document.getElementById('offerte').scrollTop;
document.getElementById(formid).submit();
}
</script>
The form submit works correctly but $_GET[scrolltop] after form submission, is empty even if it was filled with javascript. And testing it with alert before submit shows the correct value.
Anyone knows why?
Thanks a lot.

In your script...
The line document.getElementById('scrolltop').value = document.getElementById('offerte').scrollTop;
What exactly do you intend to do with the last bit (('offerte').scrollTop)?
As far as I understand, you want to return a text value, however setting the value to this will not return anything.
You will need to parse the query string ($_GET variable) and assign that to a variable which you will then pass as the value for the hidden field.
If I'm being completely in the dark here, I'm sorry. You should include a bit more of your code or additional information if my answer seems far fetched.
Anyway, I hope this helps!

I found this:
function submit_form(formid) {
var hidden = document.createElement("input");
hidden.type = "hidden";
hidden.name = "theName";
hidden.value = document.getElementById('offerte').scrollTop;
var f = document.getElementById("form2");
f.appendChild(hidden);
f.submit();
}
I submit the form using a javascript function instead of submit button.
Here I can set a hidden filed to a value I need in my action page.

Related

Call js function with image Button with php without submit

Im coding php and i want to use image-button to call function.
$del= "<input type='image' src='delete.png' name='delete_cat' value='{$cat_name}' onclick='delete_exe(this.form,this.name,this.value)' >";
This button is into a submit form
<form method="post" action="caffe_menu_category_post.php">
/form>
My problem is that when js function(delete_exe()) call and execute, then have auto-submit.
I don't want that. Is there any solution or alternative code?
Can i set submit off for my button?
Thanks in advance.
ps in my code i submit form with submit input
input type = "submit" value = "Submit Your Entries">
Use
return false;
in the end of your delete_exe(); function. That will cancel submitting.
Ok. i find the solution. I change my post form
form name="myForm" method="post" action="caffe_menu_category_post.php" onsubmit="return validateForm()">
And use javascripts
var val=true;
function delete_exe(sel,kind,name){
//Set val false to stop submit
val=false;
}
function validateForm(){
if(!val){
//Stop submit
return false;
}
}
So the auto-submit stop.
On the oher hand when click submit button i call js function to set val=true;
i don't know if these is the best solution, but it's work for me!

Want to set value to input type text and then post the form to php from javascript

id doesn't want to put the value in to the document.getElementById("UniLoc").value = val; and it doesn't want to submit the form how to fix this ?
This is how I call the function JS nas some value "asd56"
echo "
<script type="text/javascript">
repostD("'.$JS.'");
</script>";
here is my function inside
<script>
function repostD(val)
{
//document.getElementById("UniLoc").value = val;
document.getElementById('UniLoc').innerHTML = val;
//document.UlRL.UniLoc.value = val;
document.forms["UlRL"].submit();
}
</script>
and here is my form in html
<form name="UlRL" id="UlRL" action="in.php" method="post">
<input type="text" id="UniLoc" name="UniLoc" value="<?php echo $UniLoc;?>" />
Use
document.getElementById('UniLoc').value=val;
If there is nothing else wrong with your full form, it should submit. If you haven't closed your form tag, then close it.
</form>
try doint that in php all instead of in function make an if clause down the code that will execute the action of the posting of the form and at the beggining of the php try this
if($JS!="" )
{
$UniLoc=$JS;
$JS="something that will go throught the other if down at the code";
}
else {
$UniLoc=$_POST["UniLoc"];
}
I cant really tell from your question what is wrong, but looking at your code:
document.getElementById('UniLoc').innerHTML=val;
you are trying to set the value of the input with id 'UniLoc', but you are using the wrong syntax. Instead of .innerHTML, use .value
document.getElementById('UniLoc').value=val;

Jquery - How to get current input value from a repeating from

I have a comment system in which i want to add delete option, for this i have implemented a POST form in each comment which posts comment-id to delete.php, it is working in php, but not in jquery.
i.e in order to delete comment a comment id must be posted to delete.php file which handles deletion of comment from database.
i am trying to fetch that comment-id from input value to post with jquery like this but it gives me the first comment-id value not the selected value.
Jquery
$('form[name=comments]').submit(function(){
var comment_delete = $("input[name=comment-delete]").val();
//$.post('../../delete.php', {value1:comment_delete}, function(data){alert('deleted')});
alert(comment_delete);
return false;
});
repeating form is like this
<form name="comments" action="../../delete.php" method="post">
<input name="comment-delete" type="hidden" value="<?php echo $list['comment-id']; ?>" />
<input value="Delete" type="submit" />
</form>
if i use .each() or .map() it gives me all the comment-id values.
Please see and suggest any possible way to do this.
Thanks.
To find the relevant input, that is the one of the form you submit, you could use this :
$('form[name=comments]').submit(function(){
var comment_delete = $(this).find("input[name=comment-delete]");
BTW, I'm not totally sure of what you do but you might be missing a .val() to get the value of the input.
You have the same name on each hidden input, naturally you get all those inputs as you have not targeted the correct form when doing:
$("input[name=comment-delete]");
"this" whould point to the form inside your submit function. Try this.
$('form[name=comments]').submit(function(){
var comment_delete = $(this).find("input[name=comment-delete]");
//$.post('../../delete.php', {value1:comment_delete}, function(data){alert('deleted')});
alert(comment_delete);
return false;
});
As dystroy said, you are probably missing .val().
var commentId = $(this).find("input[name=comment-delete]").val();
try this
$('form[name=comments]').submit(function(){
var comment_delete = $("input[name=comment-delete]", this);
//$.post('../../delete.php', {value1:comment_delete}, function(data){alert('deleted')});
alert(comment_delete);
return false;
});
this refers to the form being submitted (more generally, to the event source).
$(...) accepts a second parameter, which is then used as a context for the selector. $(selector, context) is equivalent to $(context).find(selector)

Submit via Enter (specific Submit Submit Name)

This submitenter function is floating around the internet, apparently it is from O'Reilly's. Good job, however, I need it to POST a specific submit value. (Submit button is named submitReport). I have tried
myfield.form.submitReport();
but it doesn't work. I don't really have any ideas about what to do at this point. I am willing to bet that the answer is straightforward but I am stumped, I'd love any suggestions.
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{
myfield.form.submit();
return false;
}
else
return true;
}
Here is the submit button I am trying to have it enter for me.
The script must produce 'submitReport', there is a PHP function looking for $_POST['submitReport'], this is because there are a few forms on this page.
<form name= "Insert" onsubmit="return validate(this)" method="post" action="">
<textarea name ="link"></textarea>
<textarea name ="country"></textarea>
<textarea name ="province"></textarea>
<input
type ="submit"
style="visibility:hidden"
name ="submitReport"
value =''
>
</form>
Add a hidden field to the form that is named submitReport.
You only have a problem in the first place because you are using textarea elements where it seems input elements would be more appropriate.
If users recognise the textarea as a textarea, they don't expect pressing enter or return to submit the form so you are breaking the usuability of the page. If you are making the textarea elements look like inupts, then use inputs and no javascript is required to submit the form.

Can I submit a predefined local variable as a POST w/out putting into a form field?

I have a local variable I'd like to be sent along with the rest of the POST data taken from an HTML form. Is there a function that lets me put more data from the current page into the POST array?
no function, simply add a hidden field with the value.
<input type='hidden' name='what_ever' value='<?php echo $my_var?>' />
If you need the value to remain totally hidden from the user, then use the SESSION to pass it's value between requests.
I am assuming that when you say "local data" you mean client side data. You could do this with some javascript.
function addFormData(key, name) {
var f = document.getElementById('myform');
var g = document.createElement('input');
g.setAttribute('name', key);
g.setAttribute('type', 'hidden');
g.value = name;
f.appendChild(g);
f.submit();
}

Categories