Retrieving already assigned text box value dynamically in javascript - php

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

Related

Empty variables before post using jQuery ONLY

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;
});

getting value from a javascript array and put it in php variable

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']

Form POST action wont work

Why wont this work?
<form action="" method="POST">
<input type="text" class="searchBar" name="domain" /><input type="button" value="SEARCH" class="searchButton" id="srch" />
</form>
<? print $_POST['domain']; ?>
I have tried putting it into a var too. I just got an error.
Thanks
Edit:
Suggestions have been made of changing button to submit, but my jQuery bug's up.
<script type="text/javascript">
$('#srch').click(function(){
$('#notActive').attr('id','Active');
});
$(document).ready(function(){
// Hide div 2 by default
$('#Active').hide();
$('#srch').click(function(){
$('#notActive').hide();
$('#Active').fadeIn();
});
});
</script>
I have managed to change the Button to Submit by using the action as javascript: void(0); for my form.
The form cant submit if contains no type="submit" input.
<input type="button" /> cant submit form. You should add <input type="submit" /> to form.
You can't access the posted variable before a form is submitted.
if (isset($_POST['domain'])) {
// a form is submitted that contains the 'domain' parameter
print $_POST['domain'];
}
Try setting an action:
<form action="index.php" method="POST">.
Also, change your second line to this:
<? if (isset($_POST['domain'])) echo ($_POST['domain']); ?>

How to send dynamic array value in another php file using jQuery

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>';
?>

choosing checkbox echoing submit button immediately

what is the code if I have checkbox and by choosing it(without any submit button) showing me a submit button
<html>
<head>
<title>test</title>
<form method="post">
<?php
//what will be the code here?
?>
<input type='checkbox' name='cb'>
</form>
</html>
Simply CAN'T DO without Javascript. With Javascript?
Easy, quick, sweet and approachable
<script>
function openinput() {
document.getElementById('submit').style.visibility = "visible";
}
</script>
<form>
<input type="checkbox" onclick="openinput()" />
<input type="submit" id="submit" value="go" style="visibility: hidden" />
</form>
Not a JS guy but something simple like this should work (untested code):
<script>
function showHide(obj){
if(obj.checked==true){
document.getElementById('submit_btn').style.display = 'inline';
}else{
document.getElementById('submit_btn').style.display = 'none';
}
}
</script>
<form method="post">
<input type='checkbox' name='cb' onclick="showHide(this)">
<input type="submit" id="submit_btn" value="submit" style="display:none" />
</form>
Ofcourse there are more elegant solutions using frameworks like JQuery
Something like this, but this will only work if you indeed submit the form but don't do anything with the post values other than this check. JavaScript would be the better option.
<?php
if(isset($_POST['cb'])) {
?>
<input type="submit" value="go power rangers" />
<?php
};
?>
raw code: (in jQuery v1.6)
$('input[type="checkbox"]').change(function() {
var $input = $(this);
if($input.prop('checked')){
alert('checked');
$('input[type="submit"]').show();
}
else
$('input[type="submit"]').hide();
});
DEMO

Categories