In same page i set the jquery value while click the button. i want pass the value to php variable in same page without form submitting.
<input type="button" name="next" class="next btn btn-primary" value="Proceed To Checkout Page" />
Jquery
$(".next").click(function(){
<?php $var1="1";?>
}
While check the php value
<?php if(isset($var1)){
echo $var1;
}else{
echo "NULL";
}?>
Every time time i got the null value. Where i getting mistake.
Ps: I cant able to send the ajax call to get the value
You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript.
You can use cookies to achieve this.
In Javascript:
<script type="text/javascript">
document.cookie = "var1=1";
</script>
And in PHP
<?php
$phpVar = $_COOKIE['var1'];
echo $phpVar;
?>
If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.
Do some thing like this.
$(".next").click(function (){
var php_val=$("#php_val").val();//it is getting value from hidden filed whose id is 'php_val'.
//make ajax call with this as follows
$.post("a.php",{php_val:php_val},function(data){
//a.php is page name where u want to send php value.
})
});
<?php $var=1; ?>
<input type="hidden" id="php_val" value="<?php echo $var; ?>"/>
I hope this answers your questions
It will help you ,
<script>
$(document).ready(function() {
$(".next").click(function() {
<?php echo $var = 1 ; ?>
});
});
</script>
<?php
if (!empty($var)) {
echo $var;
}
?>
Related
i know the question doesn't looks good but am not getting any idea because am new to here.my question is can we store the value of an input type in to an variable.here in this case
<input type="text" name="statustochange">
having an value now i want to store that value in to an variable with out submit or click
i got the value in to
name="statustochange"
from this way
<script type="text/javascript">
$('.modal').modal({
ready: function(modal, trigger) {
modal.find('input[name="statustochange"]').val(trigger.data('status'))
}
});
</script>
Try this:
<input type="text" name="statustochange" value="<?php echo $str; ?>">
Into a js variable? Use a onChange event!
Into a php variable? Use a js onChange event to trigger a postback in Ajax (invisible to the user)
Edit: More realavent link
I'm creating an employee scheduler application. I want to create a button to change the calendar view to next or previous month view. But the problem is, when I click the button, the calendar changed, but the employee which has been selected to be viewed on calendar is going back to default.
I have this button in my php file:
<input type="submit" id="month-next" value="Next Month">
and I have created jquery for my button:
$('input#month-next').click(function(id){
if(month==12){
++year;
month=0;
}
++month;
$('div#month-data').load('scheduler/ajax/calendar.php?month='+month+'&year='+year+'&empid='+id);
});
so, how can I pass a my 'empid' value to my jquery function from the php page?
If your jquery in same php page you can use
$('input#month-next').click(function(){
var id = '<?php echo $empid ?>';
//your remaining code
});
If your jquery at another place you need to pass as parameter in function call:
<input type="button" id="month-next" value="Next Month" onclick="monthNext('<?php echo $empid; ?>');" >
Use a function instead of click event as above:
function monthNext(id)
{
if(month==12)
{
++year;
month=0;
}
++month;
$('div#month-data').load('scheduler/ajax/calendar.php?month='+month+'&year='+year+'&empid='+id);
}
Use button instead of input type=submit because submit will reload page.
With HTML 5 you can use data attributes.
<input type="submit" id="month-next" value="Next Month" data-empid="5">
JS:
$('input#month-next').click(function(){
id = $(this).data("empid");
// rest of your code
}
take a hidden field where you shore the employee id and after click on the button get that hidden field value like this
$('#button').click(function(){
emp_id = $('#emp_id').val();
// rest of your code
}
I have a HTML form which when the user clicks submit, all information is sent to email via a php script using the POST method. What i want to do is disable all user input after the submit button is clicked or grey out all text box input fields. How can i go about doing this?
the code for the submit button at the moment looks like this:
<input type="submit" value=" Continue " style="width:200px;height:40px">
Add below to each HTML input element you want to disabled. Since you have not mentioned I assume that you can use PHP for this.
<?php if(isset($_POST['submit'])) echo 'disabled="disabled"'; ?>
If you are using AJAX, when the event is fired, inside particular event
$("input").prop('disabled', true);
Onclick of submit button make all input to disabled
$("input").attr('disabled', true);
Since whether mail has been sent is server side processing, its better if you handle this from server end than from client side like jQuery and javascript :
$form_state = ($is_mail_sent)?"":"disabled='disabled'";
<input type="submit" <?php echo $form_state ?> value=" Continue " style="width:200px;height:40px">
without db interaction you can't do this out, have a flag in db table. Set the flag 1 if user submit a form, by default 0. Based on the flag value you can disable the form.
if(mailsent_status_of_the_user == 1){
$status = "disabled='disabled'";
}else{
$status = "";
}
<input type="submit" value=" Continue " <?php echo $status; ?> >
By using jQuery : $("input").prop('disabled', true);
But in pure Html is not possible without refreshing page !
Edit :
Javascript :
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(e) {
$("#yourFormID").submit(function() {
$("#theInputToDisable").prop('disabled', true);
//Or to disable all inputs
//$(this).children("input").prop('disabled', true);
//$(this).children("input").attr('disabled', 'disabled');
return false;
})
});
</script>
HTML
<form id="yourFormID">
<input type="text" />
<input type="submit" value="Send"/>
</form>
with the code I have I can echo:<a id='test'>
and it displays what was typed, however I need to make<a id='test'> into a variable so I can use it for mysql query's etc. is this possible at all?
<script type="text/javascript">
function email(){
var input = document.getElementById('input').value;
document.getElementById('text').innerHTML = input;
}
</script>
<html>
<p><input type='text' id='input' value='' />
<input type='button' onclick='email()' value='submit'/></p>
</html>
<?php
$info="<a id='text'>"//make this a variable;
echo $info;
?>
using AJAX with jQuery will help you make a simple call to the server, without page reloading. Then you can send any variables to a PHP file that will store them in your database.
I have ajax code which take textfiled variable. it alert the value, but when I want to get in php, it gives me an error of Undefined index
JavaScript Code
<SCRIPT>
function go(UserID) {
var name= encodeURI(document.getElementById('name').value);
var design= encodeURI(document.getElementById('design').value);
http.open('get', 'testAjax.php?design='+design+'&name = '+name);
http.send(null);
}
</SCRIPT>
HTML Code
<INPUT ID='name' type='text'>
<INPUT ID='design' type='hidden' value="<?php echo $UserID; ?>">
Link
Php Code
if(isset($_GET['process'])) {
echo $_GET['design'];
echo $_GET['name'];
}
please let me know how to solve it.
You have an anchor tag on which you have an onclick event. Onclick event when fired will send an ajax request to testAjax.php? url.
However since you haven't prevented default behaviour of clicking an anchor tag, as soon as this ajax request is sent, page will redirect to ?process and where ofcourse you will have nothing to receive as $_GET parameters.
Assuming you have your ajax going right, add return false; at the end of your go() function. Also since on server side you have a clause like if(isset($_GET['process'])) { , you should also update your ajax url to something like: testAjax.php?process
Ok, a couple of things I spotted that maybe causing this issue.
Try:
<script>
function go(UserID) {
var name= encodeURI(document.getElementById('name').value);
var design= encodeURI(document.getElementById('design').value);
http.open('get', 'testAjax.php?process&design='+design+'&name='+name);
http.send(null);
}
</script>
<input id='name' type='text' />
<input id='design' type='hidden' value="<?=$UserID; ?>" />
Link
As you can see, we remove the html variable "process" and make it a part of the ajax call.