Please, help me to solve this problem
When I am trying Single input value then input value transfer data.php file successfully and I get input value in data.php file to write this code echo $name=$_POST[‘name’]; but I can transfer input array value in data.php file, actually I have not enough knowledge about jquery. So anyone can help me to solve this problem. What’s the write jquery code for transferring array value in data.php file and what’s the write php code for echo.
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function get(){
$('#age').hide();
$.post('data.php',{ name: form.name.value },
// this code don't work in array
// how can i transfer name[] value in data.php file
function (output){
$('#age').html(output).fadeIn(1000);
});
}
</script>
<body>
<form name="form" action="a.php">
<input type="text" name="name[]" value="1"/>
<input type="text" name="name[]" value="2"/>
<input type="button" value="Get" onclick="get();"/>
</form>
<div id="age">
</div>
</body>
try something like this
<form>
<input name="name[]" value="1" />
<input name="name[]" value="2" />
</form>
$.post('data.php',$('form').serialize(), function(data) {
alert('success'); /// console.log(data); use this rather than alert with firebug
});
<?php
//data.php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
Related
I have checked this question in SOF,
How to prevent form element from sending some fields we don't want?
Here is my code
index.php
<script type="text/javascript">
$(formElement).submit(function() {
... how to do the jquery stuff..
return true;
});
<?php
$my_variable_with_value1 = 'abc';
$my_variable_with_value2 = 'def';
$another_one_variable = 'mango';
?>
<form method="post" action="process.php">
<input type="text" id="id1" name="name1" value="<?php echo $my_variable_with_value1 ?>" />
<input type="text" id="id2" name="name2" value="<?php echo $my_variable_with_value2 ?>" />
<input type="submit" value="Submit" />
</form>
I need to do the following
1. Before the page is submitted i need to unset/empty the variable values using JQUERY ONLY
2. I have to show the variable in index.php and not in the next page process.php
In other words, HOW CAN I EMPTY THE VARIABLES $my_variable_with_value1 AND $another_one_variable BEFORE PAGE SUBMIT USING JQUERY
Thanks,
Kimz
Instead of directly submitting the form using
$(formElement).submit(function() {
call an onclick function in button as:
<input type="submit" value="Submit" onclick="submitform()" />
and in the js function you can set the value of input fields to empty like:
function submitform()
{
$('#id1').val('');
$('form').submit();
}
Using below code you can set value of var1 (ID) and var2 (ID) to blank.
<script type="text/javascript">
$(formElement).submit(function() {
$("#Var1").val("");
$("#Var2").val("");
return true;
});
I use daterangepicker() to get two dates ( begginning and ending ), I'd like to put those dates in php variables.. i've succeeded to do an alert() to show those variables using a button.. but i don't know how to put them in the php.
here's my code if anyone have an idea..
<script type="text/javascript">
$(function(){
$('#rangeBa, #rangeBb').daterangepicker();
});
function test(){
var dates = new Array();
dates[0] = document.inputdate.rangeBa.value;
dates[1] = document.inputdate.rangeBb.value;
return dates;
}
</script>
<body>
<form name="inputdate" method="post">
<input type="text" value="m/jj/aaaa" id="rangeBa" name="rangeBa"/>
<input type="text" value="m/jj/aaaa" id="rangeBb" name="rangeBb"/>
</form>
<button onclick="alert(test())">Click on this button</button>
</body>
You have to add a submit input element in your form and add an action parameter in your form:
<form name="inputdate" method="post" action="yourfile.php">
<input type="text" value="m/jj/aaaa" id="rangeBa" name="rangeBa"/>
<input type="text" value="m/jj/aaaa" id="rangeBb" name="rangeBb"/>
<input type="submit" value="Click to send" />
</form>
And in yourfile.php: you get the variables by $_POST['rangeBa'] and $_POST['rangeBb']
Feel free then to use ajax method if you don't want a refresh of the page.
After you submit your form you can find the form variables by name in $_POST. for example $_POST['rangeBa']
I have a form in PHP which looks like this:
<?php
$txtVal1 = "value1";
?>
<script type="text/javascript">
function post_data(){
alert(document.getElementById("text1").value);
}
</script>
<form method="post" action="">
<input type="text" value="<?php echo $txtVal1;?>" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>
My question is whenever I click "Post" button I get an alert message as "value1". This is fine. But now if I change the text box value to something else from the UI I still get the old value. Is there any solution to get the changed value?
Try this. this works. check if your php value is putting value correctly or not
<script type="text/javascript">
function post_data(){
alert(document.getElementById("text1").value);
}
</script>
<form method="post" action="">
<input type="text" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>
<pre>
<?php <br/>
$txtVal1 = "value1";<>
?>
<script type="text/javascript">
function post_data(){
alert(document.getElementById("text1").value);
}
function test(val){
document.getElementById("text1").value = val;
}
</script>
<form method="post" action="">
<input type="text1" value="<?php echo $txtVal1;?>" onChange="test(this.value)" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>
Try this jQuery fiddle
It may be overkill but it works. StackOverflow doesn't like links to jsfiddle?
$("#submit").click(function(){
alert($("#text1").val());
});
Your page when ever load the first php value assign and after that run all value that is the problem for that. You change to assign value for the above one $txtVal1 = "value1";
<script type="text/javascript">
// index.php
function get(){
$('#age').hide();
$.post('data.php',{ name: $('form').serialize() },
function (output){
$('#age').html(output).fadeIn(1000);
});
}
</script>
<body>
<form name="form" action="a.php">
<input type="text" name="name[]" value="1"/>
<input type="text" name="name[]" value="2"/>
<input type="button" value="Get" onclick="get();"/>
</form>
<div id="age">
</div>
but result show
Array ( [name] => name%5B%5D=1&name%5B%5D=2 )
I want to get 1 by 1 value. so that I can collect data from database running a query to based on value please, kindly help me
data.php:
<?php //data.php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
This approach only works when you POST the form normally, using submit.
You now need to unserialize the data:
<?php
var_dump(unserialize($_POST['name']));
?>
This line
$.post('data.php',{ name: $('form').serialize() },
Says that you post your form (serialized) as the value of key 'name'. While this is possible, I presume you just want your values.
If you take a quick look over at the jquery post manual youc an see that you can make an object like this:
{ name: "John", time: "2pm" }
Just read your form-values with jquery and put them in an object like that, instead of serializing the form.
I'm not sure if I understand your question, but I'm guessing you're not receiving the correct value in your php file?
Try this --
In your HTML file, use this:
<script type="text/javascript">
// index.php
function get()
{
$('#age').hide();
$.post('data.php',
{
"name1":$('#name1').val(),
"name2":$('#name2').val()
},
function (output){
$('#age').html(output).fadeIn(1000);
});
}
</script>
<body>
<form name="form" action="a.php">
<input type="text" name="name1" id="name1" value="1"/>
<input type="text" name="name2" id="name2" value="2"/>
<input type="button" value="Get" onclick="get();"/>
</form>
and in your php page, have this:
<?php
echo "<pre>";
echo "this is the first input: " . $_POST['name1'] . "<br />";
echo "this is the second input: " . $_POST['name2'] . "<br />";
echo "</pre>";
?>
Hope I interpreted your question correctly.
I am completely new to javascript/jquery, and would appreciate any help.
I am having trouble with the function $.post because I am using radio in the form. I need to use the value of the chosen radio in a different file, so that I can process what should be outputted, and then I want to output something in place of where the form is.
Here is the form with type radio input:
<div id='poll'>
<form name='poll_form' id='poll_form'>
<INPUT TYPE="radio" name='poll' value ='poll1'/>Option1<br/>
<INPUT TYPE="radio" name='poll' value='poll2' />Option2<br/>
<INPUT TYPE="radio" name='poll' value='poll3'/>Option3<br/>
<INPUT TYPE="radio" name='poll' value='poll4'/>Option4</br>
<INPUT TYPE='button' value='Submit Vote' onClick="vote();" />
</form>
</div>
Here is the javascript/jquery to define the "vote();" function:
<head>
<script type = "text/javascript" src="jquery.js"></script>
<script type = "text/javascript">
function vote() {
$.post('file.php',$('input:radio[name=poll]:checked').val(),
function(output){
$("#poll").html(output).show();
});
};
</script>
</head>
Is $('input:radio[name=poll]:checked').val() the correct thing to use? And if so, how do I retrieve the value of $('input:radio[name=poll]:checked').val() in file.php?
To post values you would have to declare a post-variable and assign your value to it, i.e.
$.post('file.php',{ poll: $('input:radio[name='poll']:checked').val() }, function() {
$("#poll").html(output).show();
});
In your PHP file you can access the value via
$_POST['poll']