if i have a text field like this:
<input type="text" name="quantity" id="txtquantity">
what i am trying to figure out is how can i grab the value of that text field and append it to a link like this:
index.php?quantity=(the value)
if this is done with jquery how would i do it?
Im not sure if you are trying to do this on the page the input gets posted to, or the current page. If you are trying to do it on the page the form gets posted to you would need to use PHP like the following.
<?php
echo '<a href="'.$_GET['txtquantity'].'" />';
?>
However, if you are trying to do it before the form is submitted then using jQuery you would do it like this,
$("a").attr("href", $(this).attr("href") + $("#txtquantity").val());
You can do like:
<a id="your_link_id" href="whatever">A Link</a>
jQuery:
$('#your_link_id').attr('href', $(this).attr('href') + $('#txtquantity').val(););
Related
is it possible to somehow send $_POST[] data via a <a> tag? because it seems to automaticly execute the $_POST[] once the page loads rather than when I press the Anchor tag.
edit:
so what I'm trying to achieve is something like this:
I want an anchor tag which would normaly go to somepage.php?variable=something
but instead of using $_GET[] I want to send the variable via $_POST
Nothing in HTML will cause a link to trigger a POST request or encode data in the request body.
You can bind a JavaScript event handler to a link, cancel the default behaviour and then send a POST request by programmatically submitting a form or using XMLHttpRequest. This generally isn't a good idea and you should use a submit button instead (which you can style to look like a link if you really, really want to).
You can achieve this using jQuery and a HTML form
HTML:
<form method="post" name="redirect" class="redirect">
<input type="hidden" class="post" name="post" value="">
<input type="submit" style="display: none;">
</form>
Button: (html)
<a href='javascript:void(0)' class='button' var='DATAHERE'>sometexthere</a>
Javascript, or rather said jQuery:
$(".button").click(function() {
var link = $(this).attr('var');
$('.post').attr("value",link);
$('.redirect').submit();
});
this jQuery code listen's to any clicks on the items with the class button attached to them,
and reads out their "var" value, basicly you could use any kind of HTML element using this method as long as they have the button class attached to it.
Answer is no. What you can do is set the value of an input type when the <a> tag gets clicked. Using Javascript.
You could get hold of the query string from the href attribute of the anchor tag (you would need to parse the href and get hold of the string on the right hand side of the & symbol) and then post it using javascript or jquery (easier to use jquery).
http://api.jquery.com/jquery.post/
Would have to ask why you would want/need to do this?
I don't know if this is possible but what I'm trying to accomplish is that when ever I click this link
<a href=?name=$value1 id=$value1 class='txt_value'>Edit</a>
$value1 is a variable of my ID which i retrieved from my database.
my link could be
<a href=?name=$value1 id=$value1
<a href=?name=$value2 id=$value2
<a href=?name=$value3 id=$value3
etc..
my point is whenever I click any link above, a dialog box(jquery) would appear and insert
$value1 into a textbox inside the dialog box. Question is, is it possible that whenever I click a link all data that is associated with that ID (ex. first name, last name with ID #1 ) would also be called and displayed into that dialog box.
this is my initial code
$(".bordered a").click(function(){
$(".ok").val($(this).attr("id"));
$(".create-modal").dialog({
height: 200,
modal:true,
width:300
});
});
<div class="create-modal">
<input class="ok" type="text">
</div>
This is possible. The simplest way is to make a ajax request via jQuery to a PHP script which will return a json string. You can get a PHP array and print it via json_encode function to accomplish that. The JQuery function will take care of the json string and convert it into a JavaScript object.
I am new here and also don't know PHP.
I have got a project in which there is a table containing a Buy button in one cell and its value in another cell.
I want it like that, when the user clicks on "BUY", it's value gets passed to a contact form.
Like for example: ?buyrate={value from table cell}
I have got the form working to receive the value from the URL.
Eg: ?buyrate=25 . The form field gets the value 25.
But i dont know how to get the value from the cell, so that the admin can be able to change the Value and doesn't need to change the Button URL every time.
Please help anyone.
Thanks
Since you are already getting buyrate from the URL (I assume from a $_GET ie $buyrate = $_GET['buyrate'];), why don't you simply use that value in your button code, like such:
<input type="submit" name="buyrate" value="<?php echo $buyrate; ?>" />
Without seeing code, we're not sure what else you want to submit, but this should pass that value to whatever your form action is.
you need to send data via jquery to contact form like
//include jquery.js here
<script>
$(document).ready(function(){
$('#but').click(function(){
var val=$('#v').html();
window.location.href="somepage.php?buyrate="+val;
});
});
</script>
<table>
<tr><td id="but"><button value="Buy" /></td></tr>
<tr><td id="v">60</td></tr>
</table>
//this is just an example you can modify the code as per your conditions and requirements.
I am trying to submit a form using a hyperlink and it is not posting values onto the next page.Here is my code form:
<?php
$email = array('name'=>'accountno','id'=>'accountno','value'=>set_value('email'));
?>
<form method="post" id = "login_form" action="/salesrep/check" name = "login_form" class="custLogin">
<fieldset style="color: #BD1313; width: 440px;"> <input type="hidden" name="submit_type" id="submit_type" value="account_only">
<br><center><label for="customerNo"><b>Customer No:</b></label>
<? echo form_input($email);?>
Submit<? echo form_input($button);?> </center>
<p> </p>
</fieldset>
</form>
The code on the next page looks like this:
<?
print_array($_POST);
die();
?>
When i use the button here,it posts values to next page successfully.BUT I HAVE not been able to post values using the hyperlink on onclick event. Where i am making mistake??
Why i am getting empty array when i am already inserting value in the text box.?? Or is there any way i could post values using the link and retrieve them in the next page???
The real problem is that you are trying to use a link plus some JavaScript to submit a form in the first place. Don't do this. Buttons inform users that they will submit the form. They will show up in screen readers when they are in forms mode (a link with some JavaScript won't). They "just work".
If you insist on using a link and some JavaScript, then the reason that your code doesn't work is that the JavaScript runs, the form starts to submit, then the link is followed and a GET request is made to the page instead.
Normally you could call preventDefault on the event to stop this, but you are using old style intrinsic event attributes so you need to return false; from there instead.
Recommended reading: Progressive Enhancement and Unobtrusive JavaScript
Edit: I fixed the problem by just starting from scratch. Sorry to waste y'alls time. Please vote to close if you feel so inclined.
I'm having trouble getting the actual data in a form to submit when the input fields are added via javascript. The fields show up, and in the right place, but when I do a var_dump( fieldname) on the server side, nothing is showing up. Inspecting with Live HTTP headers tells me that the browser isn't trying to submit the dynamically added form fields.
Do I need to somehow "attach" my dynamically created form inputs to the form?
My code:
HTML
<form id="att_form" method="post" name="add_attachments" enctype="multipart/form-data" action="#">
<-- below input to prove form submits, just not the dyn added elements -->
<input name="data[one]" type="text" />
<div id="add_fields"></div>
<input type="submit" />
</form>
Javascript
function addFormField()
{
var id = 1;
var htm = "<p id='row" + id + "'><input type='text' size='20' name='txt[]' id='txt" + id + "' /></p>";
$("#add_fields".append( htm );
}
When I submit the form, my input named data[one] shows up as a POSTd value, but those added with addFormField() do not. They show up, in the HTML, at the correct place, but don't POST. Do I need to append the element as a child to my form element to get it to submit?
I've seen Submit form input fields added with javascript which is where I got the idea of appending the data specifically to the form child, but that will require some restructuring of my CSS. So, I'm hoping that it's something simple I can do in the above code.
Thanks in advance!
edit: fixed the typos, but still not working. I've decided to use the library free JS method discussed in the SO link above, as that works fine. Thanks for the help though!
there is 2 typos in your code:
htm instead of html
and add_fields / add_files
There were a lot of typos in your code. For example, you didn't close the selector tag for jquery.
You put
$("#add_fields".append( htm );
Should have been
$("#add_fields").append( htm );
Notice the missing parantheses.
But I believe your problem lies mainly in how you're trying to access the values through PHP. I just put your source in a test page and it all works if you access the values correctly.
<?php
foreach ($_REQUEST['txt'] as $printme)
echo $printme."<br/>";
?>
The above source works fine.
When you are adding/appending or using innerHtml to place form fields as html, then sometime it will place it outside the form, you will see it as part of form but internally it is not.
To resolve this issue you need to add form attribute with input filed like form=myForm, and myForm should be your form id.
Is that your actual code?
If so, you didn't close the input tag and the p tag.. so maybe the form ignore them..
var htm = "<p id='row" + id + "'><input type='text' size='20' name='txt[]' id='txt" + id + "' /></p>";