Pass table value to a form on button click - php

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.

Related

Retrieving values posted using the link

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

Hidden fields are not posted if the page gets submitted using "a href"?

<?php
$sort=$_REQUEST['sortby'];
echo" $sort ";
?>
<html>
<head>
</head>
<body>
<form method="get" id="thisForm" >
<table border=1>
<tr>
<td>
Day
</td>
<td>
Server
</td>
<td>
Service
</td>
<td>
Count
</td>
</tr>
</table>
<input type="hidden" name="sortby" id="sortby"/>
</form>
</body>
<script type="text/javascript" >
var sortArray=new Array("say","server","service","count");
function sorting( cnt)
{
alert(sortArray[cnt]);
document.getElementById("sortby").value=sortArray[cnt];
}
Based on the Header clicked i am trying to build a sort query but after it is submitted on clicking the "a href" link i am unable to get the hidden field posted data ?
I have changed "a href" to submit buttons then how do you stop the page being submitted if we click the same "sort by" link.
Here is what you're missing:
A link does not submit the form with input data, it just changes the url.
Submit the form.
Override the default <a> behavior (for example, using return false).
An updated version of sorting:
var sortArray=new Array("say","server","service","count");
function sorting(cnt)
{
document.getElementById("sortby").value = sortArray[cnt];
document.getElementById("thisForm").submit();
return false;
}
You can call it using:
Day
Working example: http://jsbin.com/ukoton/3
Another option is to use the popular library jQuery, which can greatly simplify your code:
$(function(){
$('#thisForm a').click(function(ev){
var sortValue = this.name;
// an alternative to this.name is to use a data-sortBy='day' attribute,
// and then $(this).data('sortBy')
$('#sortby').val(sortValue);
$('#thisForm').submit();
ev.preventDefault();
});
});
Working example: http://jsbin.com/ukoton/4
By default, anchor tags (that being A) do not submit a form. You can either use a button or submit the form using javascript.
You can submit the form from javascript with document.thisForm.submit()
Hmm .. I am not a php guy .. so I guess that initial 4 lines tries to get the sortby field from the form, anyways href link is not equivalent to html form submit, you will need to explicitly submit the form, maybe using javascript, to get the value
Just use a Submit button and style it to look like a link, if you need to. Using Javascript shouldn't be needed here.
Using an actual link instead of a Submit button will make the form unusable for people without javascript. That might include people using accessibility tools. Apart from that, it makes your page more complex (adding illogical HTML, and requiring Javascript), and therefore more complex to maintain.

Checkbox that submits form on click

I'm using Codeigniter and wants to know how I can make a checkbox that submits the form on click?
Secondly, this checkbox will be one of several checkboxes that will act as a filter like products > $20, products < $30, how do i pass it in the url? I'm thinking /1+2+3
Haven't worked with Codeigniter much, but I can answer how to make the form submit on checking the checkbox with JS:
<form id="something">
<input type="checkbox" name="foo" id="foo" value="yes" />
</form>
<script type="text/javascript">
$("#foo").click(function() {
if ($(this).is(":checked"))
$("#something").submit();
});
</script>
The javascript questions seem to have been solved already; let's step to the codeigniter ones.
You can pass the url in one of those two ways.
Idiomatic but limited: as /1/2/3/4/etc. The controller function handling that url could both use func_get_args to read them or, if you already know how many parameters will be passed at the most, give a default value of null to all non-necessary paramenters;
Not Codeigniterish but seriously better for search parameters: enable the query strings on your config file, pass arguments as you would normally with GET (min=2&max=3 and so on) and get their value with CI's input class ($min = $this->input->get('min')).
This has nothing to do with PHP, nor CodeIgniter. The solution is to submit the form in the onclick event of the element.
<input type="checkbox" name="filterx" onclick="document.forms[0].submit()" />
You can use the OnSubmit event of the form to nicely format the url, if you like.
To do this, you can
get the values of all desired elements,
build a nice url from it,
set the url using location.href = yourniceurl,
cancel the regular submit by returning false.
Note that both solutions require javascript to be enabled. So it is a good thing to have other means of submitting the form (submit button). Don't rely on submitting by pressing Enter. Opera will use the Enter key for toggling the checkbox instead of submitting the form.
If you like, you can hide the submit button using Javascript, that way, users having Javascript will have their form auto-submitted, while users without can use the button.
You will need to make sure that your server side form validator not only accepts the nice url, but the ugly url (which posts values like ?filterx=on) too.

Detect how many times the users have click the button

Just want to know if there is a way to detect how many times a user has clicked a button by using Jquery.
My main application has a button that can add input fields depend on the users. He/She can adds as many input fields as they need. When they submit the form, The add page will add the data to my database. My current idea is to create a hidden input field and set the value to zero. Every time a user clicks the button, jquery would update the attribute of the hidden input field value. Then the "add page" can detect the loop time. See the example below.
I just want to know if there are better practices to do this. Thanks for the helps.
main page
<form method='post' action='add.php'>
//omit
<input type="hidden" id="add" name="add" value="0"/>
<input type="button" id="addMatch" value="Add a match"/>
//omit
</form>
jquery
$(document).ready(function(){
var a =0;
$("#addMatch").live('click', function(){
//the input field will append //as many as the user wants.
$('#table').append("<input name='match"+a+"Name' />");
a++;
$('#add').attr('value', 'a'); //pass the a value to hidden input field
return false;
});
});
Add Page
$a=$_POST['a']; //
for($k=0;$k<$a;$k++){
//get all matchName input field
$matchName=$_POST['match'.$k.'Name'];
//insert the match
$updateQuery=mysql_query("INSERT INTO game (team)
values('$matchName')",$connection);
if(!$updateQuery){
DIE('mysql Error:'+mysql_error());
}
}
I'm a bit confused. After this:
$('#add').attr('name', 'a'); //pass the a value to hidden input field
shouldn't you actually store the value of a?
$('#add').attr('name', 'a').val(a);
$('#add').attr('name', 'a').val(a); ????
That's not correct, you should use:
$('#add').attr('value', a);
send the content of the "a" variable to the "value" property of element with ID "add"
I do believe that's what you want to do....

Add form element dynamically using javascript -- not submitting

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>";

Categories