<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.
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;
});
Sorry if this is a rather basic question.
I have a page with an HTML form. The code looks like this:
<form action="submit.php" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input type="submit" />
</form>
Then in my file submit.php, I have the following:
<?php
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
?>
However, I want to eliminate the use of the external file. I want the $_POST variables on the same page. How would I do this?
Put this on a php file:
<?php
if (isset($_POST['submit'])) {
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
}
?>
<form action="" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input name="submit" type="submit" />
</form>
It will execute the whole file as PHP. The first time you open it, $_POST['submit'] won't be set because the form has not been sent.
Once you click on the submit button, it will print the information.
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";
I'd like to do a dynamic search function using jQuery and PHP.
I'm struggling passing the HTML form field to the JQUERY function (which would then get passed to the PHP file).
My major questions are:
1) How do I pass form "Input" field into Jquery function?
2) How do I get the PHP result in the form "Output" field?
I currently have... (simplified)
JQUERY:
$.get("SEARCH.php", {"_input" : $('input[name=input]').val()},
function(returned_data)
{
$("input[name=output]").val(returned_data);
}
SEARCH.php:
$input = 'Welcome ' . $_GET['_input'];
echo $input;
//should be "Welcome" plus whatever I type in to "input"
HTML FORM:
input: <input type="text" name="input" value="" />
output: <input type="text" name="output" id="output" />
Thank you!
jQuery
$.get("SEARCH.php", {"_input" : $('input[name=input]').val() },
function(returned_data) {
$("input[name=output]").val( returned_data );
}
HTML
input: <input type="text" name="input" value="" />
output: <input type="text" name="output" />
PHP Use echo instead of return since it looks that your code isn't in function:
$input = $_GET['_input'];
do some parsing of the input
echo $result;
jQuery
$.get("SEARCH.php", {"_input" : $('input[name=input]').val() },
function(returned_data) {
$('#output').val(returned_data);
});
PHP
$input = $_GET['_input'];
do some parsing of the input
echo $result; // not return
HTML
input: <input type="text" name="input" value="" />
output: <input type="text" name="result" id="output" value="" />
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>';
?>