How to Convert jQuery ajax success data to a PHP variable
PHP scripts execute when you open a file and before everything else.
Ajax calls exectue when the Javascript is completely loaded. (After php).
So you cant do it.
could you give me more detail about what do you want to do?
Update PHP Variable after Ajax call
You don't want to do the live updating with PHP variables, since those are only refreshed when the page is reloaded. Instead, you want to update an element's value via AJAX. As far as I can tell, you want to update the expiration date. If you don't, just let me know and I can change the code to whatever it's supposed to do.
Here's the "control flow" of this functionality:
- (Entry point) User clicks 'Submit', jQuery event handler fires
- jQuery AJAX function is called and sends the promo code to a PHP script
- In the PHP script, the database is updated with the promo code.
- The PHP script returns the new expiry date (I'll assume that it's in the d/m/Y format you wanted)
- The callback in the jQuery AJAX function receives the data from the script.
- The callback's function body updates the "Expiry" element on the page with the value from the PHP call.
Here's how to put that into HTML / JavaScript:
<h4 class="sbText mt-10" id="expiry_label">
Trial End Date: <?php echo date("d/m/Y",
strtotime($trialExpiry)); // The initial value can be displayed as normal. ?>
</h4>
<form id='promocode'>
<input type='text' class='sbInput' placeholder='Promo Code' name='promocode'>
<input type='hidden' name='userid' value='<?php echo $userID; ?>'>
<button class='btn sbSubmit'>Submit</button>
</form>
<script>
$(function () {
$('#promocode').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '../model/process-promo-code.php',
data: $('#promocode').serialize(),
success: function (result) {
$("button.btn").css({
'transition-duration': '1000ms',
'opacity': '0.5'
});
document.getElementById("expiry_label").innerHTML = "Trial End Date: " + result;
}
});
});
});
</script>
As you can see, I only added an "id" attribute to the element, a parameter to the "success" property of the AJAX call, and a line of code to update the element.
Hope I helped!
Passing value from ajax to php variable in same page
index.php
page
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function() {
var val = "Hi";
$.ajax ({
url: "ajax.php",
data: { val : val },
success: function( result ) {
alert("Hi, testing");
alert( result );
}
});
});
});
</script>
<input type="submit" id="btn" value="submit">
<form name="numbers" id="numbers">
<input type="text" name="number" id="number">
</form>
<div id="number_filters">
<a href="abc">1</a>
<a href="def">2</a>
<a href="ghi">3</a>
</div>
ajax.php
page
<?php
echo ( $_GET['val'] );
Related Topics
Php to View Pdf on Web Page and Disable User to Download
Unset a Value in a Multi-Dimensional Array Based on One of the Values
Sum Value in Foreach Loop Every 3 Time Looping
Delete Digits After Two Decimal Points, Without Rounding the Value
Detect a Quotation in a String
How to Get the MAC and the Ip Address of a Connected Client in PHP
Executing Python Script in PHP and Exchanging Data Between the Two
Getting a File to Download Instead of Opening the Browser
Mysql Return Result If 0 Rows Returned
Option Selected With Codeigniter and Data from Db
How to Get Username from Facebook Id
Laravel - Display a Pdf File in Storage Without Forcing Download
Check If Username and Email Already Exists in Database
How to Download an Mp3 File from Remote Url
How to Get the Sucess Message in the Same Page After Submitting the Contact Form
Regex to Check the String Contains Only Letter and Numbers But Not Only Numbers