I have this form :
<form action="index.php?explore=browse" method="post">
<input type="hidden" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a onclick="this.form.artist.value=all; this.form.submit();return false" href="#">All</a>
</form>
and I'd like to know :
Why it doenst put the value "all" to the artist field?
Is this Javascript? Or easy HTML?
Is better translate this with jQuery/JS Handler or this is better? (light, crossbrowsers..and so on)
Hope you can help me!
You need to change the
this.form.artist.value=all;
to
this.parentNode.artist.value='all';
The way you use it makes two wrong assumptions
it assumes that links inside a form have a form attribute.. They do not. Only input elements have a form attribute. Using parentNode should do the trick for you particular case since the link is a direct child of the form element.
it expects a variable with name all exists and it tries to put the content of that variable in the input.
Making all be a string by wrapping it to single quotes ' should do what you want.
demo at http://jsfiddle.net/gaby/vzuFD/
With jQuery you could do
<form action="index.php?explore=browse" method="post">
<input type="hidden" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a id="all" href="#">All</a>
</form>
and
<script type="text/javascript">
$(function(){
$('#all').click(function(){
$(this)
.closest('form')
.find(':input[name=artist]')
.val('all')
.end()
.submit();
});
});
</script>
demo at http://jsfiddle.net/gaby/vzuFD/1/
If you want to use this JavaScript line then change your code with: this.form.artist.value="all"
Yes it is HTML with simple inline JavaScript.
You can use JQuery also by following changing :
First change your HTML Code with the following:
<form action="index.php?explore=browse" method="post">
<input type="hidden" id="artist" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a id="linkAll" href="#">All</a>
</form>
Then use the following jQuery :
$('#linkAll').click( function(){
$('#artist').val('All');
});
all is a reference to a variable that does not exist. 'all' is a string containing the text "all".
Additionally, you assume that this.form exists (but it likely doesn't). You could use parentNode instead, but this may stop working if you move the <a> tag.
So:
<form action="index.php?explore=browse" method="post">
<input type="hidden" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
<a onclick="this.parentNode.artist.value='all'; this.parentNode.submit();return false" href="#">All</a>
It's usually preferred not to write onclick handlers inside HTML, instead writing all your Javascript elsewhere in a dedicated Javascript block/file. It keeps things nice and separated.
We also prefer e.preventDefault to return false, here, though that's a little trickier to make cross-browser so I'll leave it for another question.)
Here's an example demonstrating an overall better solution:
<form action="index.php?explore=browse" method="post" id="theForm">
<input type="hidden" name="artist" value="<?=$artist?>" />
<input type="hidden" name="event" value="<?=$event?>" />
<input type="hidden" name="date" value="<?=$date?>" />
All
</form>
<script type="text/javascript">
var theLink = document.getElementById('theLink');
var theForm = document.getElementById('theForm');
theLink.onclick = function() {
theForm.artist.value = 'all';
theForm.submit();
return false;
};
</script>
A version of this with the use of jQuery might look like:
<script type="text/javascript">
var $theLink = $('#theLink');
var $theForm = $('#theForm');
$theLink.click(function() {
$theForm.find('[name=artist]').val('all');
$theForm.submit();
return false;
});
</script>
Related
I've 2 input fields which I want to have the same value.
When input field id="name_1" is filled in, I want to have it copied into input field id="name_2".
How, or is this possible?
<form action="" method="post">
<input type="text" name="name_1" id="name_1" value="">
<input type="text" name="name_2" id="name_2" value="">
</form>
You can do expected functionality using Jquery or Javascript. To make below operation work properly, you have to include latest jQuery in your html page
Try this
<form action="" method="post">
<input type="text" name="name_1" id="name_1" value="">
<input type="text" name="name_2" id="name_2" value="">
</form>
<script>
$("#name_1").keyup(function () {
var value = $(this).val();
$("#name_2").val(value);
}).keyup();
</script>
JS-FIDDLE
<script type="text/javascript">
$(document).ready(function(){
$('#field1').blur(function(){
$('#field2').val($(this).val());
});
});
</script>
<input type="text" name="field1" id="field1">
<input type="text" name="field2" id="field2">
HTML:
<input id="name1" class="name" type="text" /><input id="name2" class="name" type="text" />
jQuery:
$(document).ready(function(){
$(".name").keyup(function(){
var val = $(this).val();
$(".name").each(function(){
$(this).val(val);
});
});
});
JSFIDDLE: https://jsfiddle.net/yzmdu308/
This way, if the 1st one gets changed, it changes the value of name2, and if name2 gets changed, it changes the value of name1.
Without using Angular JS
It is better to use class selector to update bind to two input text box elements.
HTML
<input id="name1" class="name" type="text" />
JS
$(".name").keyup(function(){
$(".name").val($(this).val()); });
JS FIDDLE
Two way binding without Angular
With Angular JS
Angular providing very simple and efficient two way binding options. Please check the below sample.
HTML
<div ng-app>
<input id="name1" ng-model="name" type="text" />
<input id="name2" ng-model="name" type="text" />
JS FIDDLE
Two way binding with Angular
I just cant get around this simple requirement. I am new to PHP and need help.
I need to capture the value in a Search Box and then pass it in the URL which opens in a new tab when hit on Submit.
What am I missing here..it seems like I am missing a lot of things for this to work..
<?php
if (isset($_POST["submit"])){
$example = $_post['searchon'];
echo '<a target = '_blank' href=http://www.amazon.in/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=.$example.&tag=orientmarketi-21></a>';
}
?>
<form action="index.php" method="post">
<input type="search" name="searchon" id="searchon" />
<input type="submit" name ="submit" />
</form>
try this code instead of your one:
<form action="http://www.amazon.in/s/ref=nb_sb_noss_1" method="get" target="_blank">
<input type="hidden" name="url" value="search-alias=aps" />
<input type="hidden" name="tag" value="orientmarketi-21" />
<input type="text" name="field-keywords"/>
<input type="submit" />
</form>
I have a basic form that I am using as a dummy to test some JavaScript functionality. My goal is to click the submit button and have submission paused until a javascript function is executed, then submit the form after that. the code I currently have is below, but the submission of the form is not prevented, and the hidden field's value is never set. can someone explain why this is happening?
NOTE I am aware of a jQuery method of performing the same functionality. I am specifically trying to solve this problem without using jQuery. If your only answer is "jQuery is better", please do not bother.
<html>
<head>
<script type="text/javascript">
function test(){
document.getElementById("hidden").value="hidden";
var queryString = $('#decisionform').formSerialize();
alert(queryString);
document.getElementById("form").submit();
}
</script>
</head>
<body>
<?php
if ($_POST){
foreach ($_POST as $key=>$value){
echo "<b>$key</b> $value<br>";
}
}
?>
<form action="hello.php" method="post" id="form">
<input name="first" type="text" />
<input name="second" type="text" />
<input name="third" type="text" />
<input name="hidden" type="hidden" />
<input name="submit" type="submit" value="Submit" onclick="test();return false;" />
</form>
</body>
</html>
You don't have an element with the id hidden so document.getElementById("hidden").value="hidden"; will throw an error as you can't set properties on undefined. The script will stop running and never reach return false so the form will submit as normal.
try <form action="hello.php" method="post" id="form" onsubmit="test(); return false;">
to prevent a submit, you actually need to have a return false, in the onsubmit even of the form itself
along with not having an id on the hidden field.
<input name="submit" type="submit" value="Submit" onclick="test();return false;" />
use a regular button in this case because your function is performing the submit.
<input name="submit" type="button" value="Submit" onclick="test()" />
You're missing the id attribute from your input element. Modify <input name="hidden" type="hidden" /> to <input name="hidden" type="hidden" id="hidden" />.
You can use
onclick="return test()"
and just return false at the end of the function.
Add id="hidden" to your input, like this:
<input name="hidden" type="hidden" id="hidden" />
I'm making a site in php and I'm using google custon search for my site.for this I'm using this code
<form action="http://192.168.0.113/boobloom/site/search.php" name="search" id="cse- search-box" >
<input type="hidden" name="cx" value="018095423333672301222:n5ebktimfxc" />
<input type="hidden" name="cof" value="FORID:9" />
<input type="hidden" name="ie" value="UTF-8" />
<div class="txt-box"><input type="text" name="q" /></div>
<div class="boob-img">
<input type="image" src="images/boob-img.jpg" name="sa" value="Submit" /></div>
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse- search-box&lang=en"></script>
but now my requirement is show the result on new page with Iframe.
I Never use Iframe before this.
how can I do this?thanks in advance..
finally I took code from Google Custom Search Engine and put the result in a page.and its working correctely
but the IFrame height is too much..
<script type="text/javascript">
var googleSearchIframeName = "cse-search-results";
var googleSearchFormName = "cse-search-box";
var googleSearchFrameWidth = 600;
var googleSearchDomain = "www.google.com";
var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="www.google.com/afsonline/show_afs_search.js"></script>
I try to change the FrameWidth variable to lower value but it still doesn't work..how can I do this?
var googleSearchFrameHeight = 300;
It's been a while since I worked with iFrames so this could be slightly wrong & need a bit of modification, but try this:
<form action="http://www.google.com/search" target="iframe" method="get" onSubmit="Gsitesearch(this)">
<input name="q" type="hidden" />
<input name="qfront" type="text" />
<input type="image" src="images/boob-img.jpg" value="Submit" />
And then the iFrame could look something like this:
<iframe name="iframe" src="" />
Basically, give the iFrame a name value & have URLs or form actions target it.
Hope this helped!
I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.