get value of input to button attributed - php

i have input type and button, i need when insert into
<input type="text" name="bills_ID" id="bills_ID" value="2">
i will get id for this item and put it into here
BillsPrint.php?bills_ID=id
this is full code
<form id="wrapper">
<input type="text" name="bills_ID" id="bills_ID" value="2">
<input name="print" type="submit" id="print" value="print" class="css3buttonblue" onclick="window.open('BillsPrint.php?bills_ID='this.id, '_blank')" />
</form>
​
how can i put the id on this link BillsPrint.php?bills_ID=2 without refresh page

You could do this:
<form id="wrapper">
<input type="text" name="bills_ID" id="bills_ID" value="2">
<input name="print" type="submit" id="print" value="print" class="css3buttonblue" />
</form>
<script>
$(function(){
$('#print').click(function(){
window.open('BillsPrint.php?bills_ID='+$('#bills_ID').val(), '_blank')
});
});
</script>

<form id="wrapper">
<input type="text" name="bills_ID" id="bills_ID" value="2">
<input name="print" type="submit" id="print" value="print" class="css3buttonblue"
onclick="window.open('BillsPrint.php?bills_ID=' + $("#bills_ID").val(), '_blank')" />
</form>
​

Remove the onclick etc. And use get method. Use this simple code and the variables will come in URL as query string.
<form action="BillsPrint.php" method="get" id="wrapper">
<input type="text" name="bills_ID" id="bills_ID" value="2">
<input name="print" type="submit" id="print" value="print" class="css3buttonblue">
</form>
You forget to specify action of form. Use this code it will work. :)
Hope it will help.

Try this:
$(document).ready(function(){
$('form').submit(function(){
var id = $('input[name="bills_ID"]').val();
window.open('BillsPrint.php?bills_ID='+id, '_blank');
return false;
});
});

$("#print").click(function(){
var id = $("#bills_ID").val();
$.ajax({
url: 'BillsPrint.php?bills_ID=' + id,
success: function(data) {
$('.result').html(data);
alert(data); //alerts output from the BillsPrint.php page
}
});
});

Related

How to make GET from "field=foo&field=bar" to "field=foo,bar"

I have a html form with many fields:
<form action="" method="get">
...
<input type="checkbox" name="price" id="price-1000" value="1000">
<input type="checkbox" name="price" id="price-2000" value="2000">
<input type="checkbox" name="price" id="price-3000" value="3000">
...
<input type="submit" value="send">
</form>
On check and send I have the following query string: ?price=1000&price=2000
How make it ?price=1000,2000 in browser URL
I think I need a rewrite rule via htaccess? Or is there some other way?
Using Jquery It will be possible.
index.php
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<form action="redex.php" method="get">
<input type="checkbox" id="price-1000" value="1000" class="ckbox">
<input type="checkbox" id="price-2000" value="2000" class="ckbox">
<input type="checkbox" id="price-3000" value="3000" class="ckbox">
<input type="hidden" name="price" id="price-1000" value="1000" class="ckvalues">
<input type="submit" value="send">
</form>
<script type="text/javascript">
$(document).ready(function() {
var favorite = [];
$('.ckbox').change(function() {
$.each($("input[type='checkbox']:checked"), function(){
favorite.push($(this).val());
});
var str1 = favorite.toString()
$('.ckvalues').val(str1)
favorite.length = 0;
});
});
</script>
redex.php
<?php
echo $_GET['price'];
?>
May this help you.

using html form with two </form> and submit buttons?

var el = document.getElementById('btn1');
el.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Add';
var form = document.getElementsByTagName('form');
form.submit();
});
var el2 = document.getElementById('btn2');
el2.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Print';
var form = document.getElementsByTagName('form');
form.submit();
});
<?php
if($_POST["hidElement"]=='Add')
echo "add";
else
echo "Print";
?>
<form action="" method="">
<input type="text" name="nasir" >
<input type="hidden" name="hidElement" id="hidElement" />
<input type="button" id="btn1" value="Submit & Add">
<input type="button" id="btn2" value="Submit & Print">
</form>
<?php
if(isset($_POST["yasir"]))
echo "Submit & Print";
else
echo "Submit & Add";
?>
<form action="" method="">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add">
</form>
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print">
</form>
There is no second starting for second form. I want both forms use same , but how PHP will recognize which form button has been pressed. I tried above but in both cases it submiting second yasir hidden input!!!
There is alot of inputs in first form.
please try this
<form action="" method="post">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add" name="first_btn">
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print" name="second_btn">
</form>
in php you can check by using this
<?php
if(isset($_POST['first_btn'])){
/*first form submit*/
}
if(isset($_POST['second_btn'])){
/*second form submit*/
}
?>
For every there should be an end also. Otherwise it will not work
try
<form action="" method="post">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add" name="btn1">
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print" name="btn2">
</form>
In that case it's better to use javascript to handle form post. You can add to buttons to same form for add and print and handle click on them using javascript. Something like below:
<form action="" method="" id="form">
<input type="text" name="nasir" >
<input type="hidden" name="hidElement" id="hidElement" />
<input type="button" id="btn1" value="Submit & Add">
<input type="button" id="btn2" value="Submit & Print">
</form>
In javascript
var el = document.getElementById('btn1');
el.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Add';
var form = document.getElementById('form');
form.submit();
});
var el2 = document.getElementById('btn2');
el2.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Print';
var form = document.getElementById('form');
form.submit();
});
In Php you can check based on hidElement value.

Does a jquery event overwrite the submit action?

I have one form with a few submit buttons. I want to submit the form via POST to itself to process the filled out form fields... does the jquery override the submit?
$('#myButton').click('myAction') <!-- not actual code, just for the idea -->
<input type="button" type="submit" id="myButton" value="do something">
you can use something like this:
html form:
<form id="myform" method="post" name="form" action="">
<input type="text" name="name" value="">
<input type="text" name="other" value="">
<input type="submit" type="submit" name="myButton" value="do something">
</form>
js:
$('#myform').on('submit',function(e){
e.preventDefault();
var addnew='&addnew=1234';//additional data
var _data = $('#myform').serialize();
_data = _data+addnew;
console.log(_data);
});
sample jsfiddle

jQuery update content from PHP

I have a table with a lot of rows, each row has 2 input fields and 1 submit button. When I click on the submit button the data is passed to a PHP script, the PHP script processes the data and returns some text.
Currently the submit button is replaced with the text that is returned by the PHP script. But I want that the returned text is being shown next to the button, so the button doesn't disappear.
I am not able to fix that, maybe somebody can help me with that?
<html>
<head>
<title>Add data with Ajax</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).on('click', '#submitButton input[type=button]', function(){
var field1 = $(this).parent().parent().find(".field1").val();
var field2 = $(this).parent().parent().find(".field2").val();
var field3 = $(this).parent().parent().find(".field3").val();
var submitButton = $(this);
$.ajax({
type: "POST",
url: "add_data.php",
data: { field1: field1, field2: field2, field3: field3 }
})
.done(function(msg) {
submitButton.parent().html(msg);
});
});
</script>
</head>
<body>
<form method="POST">
<input type="hidden" class="field1" value="product 1">
<input type="text" class="field2" value="data1">
<input type="text" class="field3" value="data2">
<div id="submitButton">
<input type="button" name="submitButton" value="Add">
</div>
</form>
<form method="POST">
<input type="hidden" class="field1" value="product 2">
<input type="text" class="field2" value="data1">
<input type="text" class="field3" value="data2">
<div id="submitButton">
<input type="button" name="submitButton" value="Add">
</div>
</form>
<form method="POST">
<input type="hidden" class="field1" value="product 3">
<input type="text" class="field2" value="data1">
<input type="text" class="field3" value="data2">
<div id="submitButton">
<input type="button" name="submitButton" value="Add">
</div>
</form>
</body>
Well, instead of replacing the content:
submitButton.parent().html(msg);
append to the content:
submitButton.parent().append(msg);

Hiding div when is submitted and send values to PHP

<?php
echo $_POST['textvalue'];
echo $_post['radiovalue'];
?>
<div id="hidethis">
<form method="POST" action="">
<label>Tekst Value</label>
<input type="text" name="textvalue">
<label>Radio Value</label>
<input type="radio" name="radiovalue" value="autogivevalue">
<input type="submit" id="submit" name="submit" value="submit">
</div>
http://jsfiddle.net/Bjk89/2/ here is it with the jQuery.
What i try to do is to hide the <div id="hidethis"> when it's clicking submit.
I know i can make another page where i can recieve the values without the <form> section, but i want to put both in one page, make the <div id="hidethis"> hidden after submit.
So i'll be able to get echo $_POST['textvalue']; and echo $_post['radiovalue']; as results
RESULT MUST BE LIKE
A Text // This is the value you input into Tekst Value
autogivevalue // This is the value from the radio button
----- INVISIBLE -----
<form is hidden because we set it in jQuery so>
</form>
Try this. No need to use jQuery here.
<?php
if($_POST) {
echo $_POST['textvalue'];
echo $_post['radiovalue'];
} else {
?>
<form method="POST" action="">
<label>Tekst Value</label>
<input type="text" name="textvalue">
<label>Radio Value</label>
<input type="radio" name="radiovalue" value="autogivevalue">
<input type="submit" id="submit" name="submit" value="submit">
</form>
<?php
}
?>
Try adding '#' in your jquery code. Your version does not have # next to submit. Also your form is missing a closing tag here and in your JSFiddle code.
Try this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#submit').click(function () {
$('form').submit();
$('#hidethis').hide();
});
});
</script>
<form method='post' id="hidethis" name='form'>
<input type="text" name="textvalue">
<input type="radio" name="radiovalue" value="1">
<input type="button" id="submit" name="submit" value="submit">
</form>

Categories