Please consider the following code,
<form action="index.php" method="post" name="adminForm" enctype="multipart/form-data">
<input type="hidden" name="showtime[]" id="showtime_1" value="1" />
<input type="hidden" name="showtime[]" id="showtime_2" value="2" />
<input type="hidden" name="showtime[]" id="showtime_3" value="3" />
<input type="hidden" name="mid" id="movie_id" value="100" />
.
.
.
</form>
I want to use ajax post to submit this.
$.post('index.php',{
'option':'com_movies',
'controller':'movie',
'task' : 'savedata',
'format':'raw',
'mid':$('#movie_id').val()
},function(result){
if(result)
alert(result);
return;
});
I want to know how I send showtime ids using ajax post.
You can use
$('input[name="showtime[]"]').serialize()
Which will give you all inputs with showtime name in format you can send by ajax...
Check on jsfiddle...
Demo
Related
I have this HTML form:
<form action="" method="post">
<input type="hidden" name="data_first" value="9" />
<input type="hidden" name="data_second" value="2" />
<input type="hidden" name="date" value="2018-01-25" />
<input type="text" name="posted_data" value="0.1" />
</form>
I want combine this Posted data, with serialize function.
I can serialize only one input.
example: serialize($_POST['posted_data']);
I need function like this:
serialize($_POST['posted_data'],$_POST['data_first'],$_POST['data_second'],$_POST['date']);
Any ideas?
You can simply do -
serialize($_POST);
It will serialize all the posted data. And then process them accordingly.
Update
You can use input arrays -
<form action="" method="post">
<input type="hidden" name="data[data_first]" value="9" />
<input type="hidden" name="data[data_second]" value="2" />
<input type="hidden" name="data[date]" value="2018-01-25" />
<input type="text" name="data[posted_data]" value="0.1" />
</form>
And
serialize($_POST['data']);
Will serialize those specific inputs present in data.
there are two ways
1) By sending post array
e.g serialize($_POST)
2) By sending post data through array
e.g serialize(array($_POST['posted_data'],$_POST['data_first'],$_POST['data_second'],$_POST['date']));
<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>
I want to know how to get the value of customized attributes of between several radio buttons like example above by using php.
How can i get the value of customizedValue1 and customizedValue2 in php?
Thanks
You can't access directly from PHP to this values, you need to pass them as AJAX POST values to the PHP file like this:
FORM
<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>
JS
$('#test').on('submit',function(){
var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');
$.post('getValue.php',{'customizedValue1':customizedValue1});
});
On getValue.php you can access to the value:
echo $_REQUEST['customizedValue1'];
If they are connected to eachother somehow. You can also use the values as an array in html form
<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$customizedValue1 = $_POST['data']['A']['customizedValue1'];
$customizedValue2 = $_POST['data']['A']['customizedValue2'];
echo $customizedValue1;
echo $customizedValue2;
}
?>
Let's say I have a form that I send via GET:
<form method="get" action="/search.php?foo=bar&test=1&something=else">
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
And after submitting my form and processing the data (which consists of only saving it to a file), the url is changed to:
search.php?day=test&link=google.com
What should I do so the url becomes:
/search.php?foo=bar&test=1&something=else&day=test&link=google.com
(preserving the old parameters that were included in action attribute.)
The form action will change every time and it's difficult to keep the old GET parameters in the form action.
However, you can go with hidden fields.
Try this:
<form method="get" action="/search.php">
<input type="hidden" name="foo" value="bar"/> <!-- Add this -->
<input type="hidden" name="test" value="1"/> <!-- Add this -->
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
You could try changing the action before submitting, depending how you're going to submit the form. This can be done if you apply IDs to your texts and either a name or an ID to the form.
HTML:
<form id="frm" method="get" action="/search.php?foo=bar&test=1&something=else">
<input type="text" name="day" id="day" placeholder="day"/>
<input type="text" name="link" id="link" placeholder="link"/>
</form>
Then with JavaScript you can run a function and change the form's action:
var _form = document.getElementById('frm');
var day = document.getElementById('day').value;
var link = document.getElementById('link').value;
_form.action += '&day=' + day + '&link=' + link;
_form.submit();
You could try to use a hidden input field.
<input type="hidden" name="name" value="value">
if you are getting your variables first from get and then want to add into 2nd form you could get this and create input fields hidden with these get values
Now when you submit form in get url you will get all you desire data
<?php
if($_GET){
if(isset($_GET['submit1'])){
$foo = $_GET['foo'];
$test = $_GET['test'];
$something = $_GET['something'];
?>
<form method="get" action="/search.php">
<input type="hidden" name="foo" placeholder="foo" value="<?php echo $foo; ?>"/>
<input type="hidden" name="test" placeholder="test" value="<?php echo $test; ?>"/>
<input type="hidden" name="something" placeholder="something" value="<?php echo $something; ?>"/>
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
<?php
}
}
?>
I have 2 forms on my page and 1 of them has 2 different ways to be submited, a submit button and a jQuery on click event, it looks something like this:
<script>
$(document).ready(function() {
$('#img_send_form').click(function() {
$('#form2').submit();
});
});
</script>
<form name="form1" action="#" method="post">
<input type="text" name="field1"/>
<input type="submit" name="send1"/>
</form>
<form name="form2" action="#" method="post">
<input type="text" name="field1"/>
<input type="text" name="field2"/>
<input type="text" name="field3"/>
<input type="text" name="field4"/>
<input type="text" name="field5"/>
<input type="text" name="field6"/>
<input type="text" name="field7"/>
<input type="submit" name="send2"/>
</form>
<img src="xxx" id="img_send_form"/>
What is the best way to check if form2 was submmitted on php? Do I need to use isset for every form field ?
if (isset($_POST['field1'])||isset($_POST['field2'])||isset($_POST['field3'])||isset($_POST['field4'])||isset($_POST['field5'])||isset($_POST['field6'])||isset($_POST['field7']))
or is there another "better" way to do it?
Take Hidden Field with Same Name in Both Forms (but differ Ids if you need)
Then you will only need to check the that hidden field
just add a hidden field to the second form, and in PHP check if it's set, in this case was used the second form
Not necessary to take hidden fields,
PHP :
if(isset['send2'])) { echo "Form2 submitted !" ;?> }
<script>
$(document).ready(function() {
$('#img_send_form').click(function() {
$('#form2').submit();
});
});
</script>
<form name="form1" action="#" method="post">
<input type="text" name="field1"/>
<input type="submit" name="send1"/>
</form>
<form name="form2" action="#" method="post">
<input type="hidden" name="form2_send"/>
<input type="text" name="field1"/>
<input type="text" name="field2"/>
<input type="text" name="field3"/>
<input type="text" name="field4"/>
<input type="text" name="field5"/>
<input type="text" name="field6"/>
<input type="text" name="field7"/>
<input type="submit" name="send2"/>
</form>
<img src="xxx" id="img_send_form"/>
And php :
if(isset['form2_send'])) { echo "Form2 submitted !" ;?> }
I have the below form that i want to remove the submit button from (or hide it).
<form action="cart.php" method="post">
<input name="quantity" type="text" value="3" size="1" maxlength="2" />
<input name="adjustBtn" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="1" />
</form>
In a jQuery file called test.js that i have included in the page i have the bellow that should submit the form when the input field "quantity" has changed, but it does not, i cannot get it to work and i have tried all sorts of form submit functions from other posts on SO. One person even said that if the submit button had a name of submit it would not work but i do not have that.
$(document).ready( function() {
$('#quantity').change(function(){
//$(this).closest("form").submit(); <-- commented out as not working either.
//$(this).closest('form')[0].submit(); <-- commented out as not working either.
$('#adjustBtn').submit();
});
} );
Anyone have any idea why it wont submit the form? I have tested in chrome and IE.
You need to submit the form instead of the button:
$("#adjustBtn").closest("form").submit();
Also, you didn't give an id to your adjustBtn, thus $("#adjustBtn") won't work;
<input id="adjustBtn" name="adjustBtn" type="submit" value="change" />
The same thing happens with #quantity, that's why the change handler doesn't get fired, you need to also add the id:
<input id="quantity" name="quantity" type="text" value="3" size="1" maxlength="2" />
It should be:
<form action="cart.php" method="post" id="autoSubmit">
<input name="quantity" type="text" value="3" size="1" maxlength="2" />
<input name="item_to_adjust" type="hidden" value="1" />
</form>
And the jQuery:
$(document).ready( function() {
$('#quantity').change(function(){
$('#autoSubmit').submit();
});
} );