I would like to destroy $_SESSION['userList'] after cliking 'Cancel' button. However, the $_SESSION['userList'] is destroied when page load.
Below is my code:
<span>Cancel</span>
<script type="text/javascript">
function resetForm() {
<?php
unset($_SESSION['userList']);
?>
}
</script>
Really appreciate for any help.
You cannot execute PHP (server-side) in your javascript (client-side). You need to issue an HTTP request to invoke PHP. You can do that using AJAX.
JAVASCRIPT
$.ajax({
type: "POST",
url: "phppage.php",
data: "action=unsetsession",
success: function(msg){
alert(msg);
if(msg == "success"){
//cleared session
}else{
//failed
}
},
error: function(msg){
alert('Error: cannot load page.');
}
});
PHP
if($_POST['action'] == "unsetsession"){
unset($_SESSION['userList']);
echo "success";
}
You can not execute php code in the client side like you try in your example. If you want to to destroy session withou reload then you must use ajax request.
You better use jquery for this
<span>Cancel</span>
<script>
function resetForm()
{
jQuery.ajax({
type: "POST",
url: "cancel.php",
data:,
cache: false,
success: function(response)
{
}
});
}
</script>
And create a new cancel.php which will be like this -
<?php
unset($_SESSION['userList']);
?>
You can't call PHP from JS like this. You'll need to do a call back to the server.
Use submit button for Cancel (You can use CSS to make it look the away you want)
//HTML
<form method="post" >
<input type="button" value="Cancel" name="cancel" id="cancel" />
</form>
//PHP
if(isset($_POST['cancel'))
{
unset($_SESSION['userList']);
}
Related
I am trying to pass data from a hyperlink to a page via a get request using Ajax
Initially I am able to do this without Ajax directly as below
<a id="link" href="page.php?id=12&pid=12" >pass data</a>
and I catch below in php
<?php $_GET['id'] ?>
<?php $_GET['pid']?>
now I want to do this using Ajax so the page does not need to load
I am trying with the below
<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
$(document).ready(function() {
$(".choice").click(function(e) {
e.preventDefault();
$.ajax( {
<!--insert.php calls the PHP file-->
url: "votes.php",
method: "get",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
but I am unable to get this to work. I need help on the correct implementation of send data using Ajax to my php file
First of all, a <script> tag is required for JavaScript part. Ajax URL can read from <a> tag href attribute by $(this).attr('href'). Also based on your code, a div with vote id is needed so <div id="vote"></div> added.
<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
<div id="vote"></div>
<script>
$(document).ready(function() {
$(".choice").click(function(e) {
e.preventDefault();
$.ajax( {
url: $(this).attr('href'),
method: "get",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
</script>
Document
pass data
<script>
$(document).ready(function() {
$("#choice").click(function(e) {
e.preventDefault();
$.ajax({
url: $(this).attr('href'),
method: "GET",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
</script> </body>
this code is about showing more data from database within the same page,without refreshing page,but this deos not work.need some help.
function showmore()
{
$.ajax({
type: "POST",
url: 'showmore.php',
data:{action:'true'},
success:function(html) {
alert(html);
}
});
}
<button type="button" onclick="showmore()">showmore</button>
showmore.php
<?php echo "<div><p>something</p></div>"?>
Change this to update your page dynamically:
In the php function:
<?php echo 'something'; ?>
Add this line to your html document (it is good practice not to return html from the server):
<div><p id="the-p-id"></p></div>
And in your jquery function, on success:
success: function(result) {
$('#the-p-id').html(result);
}
This is work for me. If I use library of jquery like this
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function showmore(){
$.ajax({
type: "POST",
url: 'showmore.php',
data:{action:'true'},
success:function(html) {
console.log(html);
}
});
}
</script>
<button type="button" onclick="showmore()">showmore</button>
**showmore.php**
<?php echo "<div><p>something</p></div>"?>
Output
<div><p>something</p></div>
I am trying to get response trough ajax,my code (index.html) :
<button id="register">Register</button>
<p id="result">xxx</p>
<script>
$("#register").click(function(){
$.ajax({
url:'registration.php',
type: 'POST',
success:function(response){
$("#result").html(response)
}
})
})
</script>
and php (registration.php):
<?php
echo "yyy"
?>
I am working with xampp, I get response but it fades from page instantly. And again xxx is present inside p tag, does anyone know what is the reason why this happens.
Thanks
It appears that when you click the button to get your response, it also refreshes the page in your browser. You can try the following to prevent this:
<script>
$("#register").click(function(evt) {
evt.preventDefault()
$.ajax({
url:'registration.php',
type: 'POST',
success: function (response) {
$("#result").html(response)
}
})
})
</script>
This prevents your browser from doing what it normally does when you click a button.Any button inside <form>tags will automatically send a GET request within the current window, causing the page to refresh. The other alternative to preventDefault() is to use the attribute type="button" on your button and this will stop the button from being a type="submit" button.
You can read more detailed information about the function I've used here:
https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
https://api.jquery.com/event.preventdefault/
Just prevent page before submitting.
<input type='submit' id='register'>
<div id='result'></div>
$("#register").click(function(e){
e.preventDefault();
$.ajax({
url:'registration.php',
type: 'GET',
success:function(response){
document.getElementById("result").innerHTML = response;
}
})
});
I am trying to pass a variable when a class("women") is clicked using ajax to a php file but it is not working. Here is my code
jquery:
$('.women').click(function(){
var test="hello";
$.ajax({
type: "POST",
url: 'data.php',
data: {'variable':test},
success:function(data){
console.log(data);
},
});
$(".women").attr('href','data.php');
})
php code:
if (isset($_POST['variable']))
{
echo($_POST['variable']);
}
else
{
echo ("failure");
}
html:
<li class="nav-item mr-auto ml-auto" data-target="#collapsewomen">
<a class="nav-link active women productlink" href="#">Women</a>
</li>
In the console I can see "hello" which mean ajax is working, but once directed to php page I get "failure". What I am not able to pass test variable to php file
The purpose of ajax is to send data to a URL without refreshing the page. If you want redirect the page, there is no use of using ajax.
Doing an ajax call will not automatically save the data sent and the data can't be use if you redirect to that page.
Using GET
$('.women').click(function(){
var test="hello";
window.location.href = "data.php?variable=" + test;
})
On your php
if (isset($_GET['variable']))
{
echo($_GET['variable']);
}
else
{
echo ("failure");
}
Using POST, one option is to use hidden form like:
On your main page:
$('.women').click(function(){
var test = "hello";
$('[name="variable"]').val(test); //Update the value of hidden input
$("#toPost").submit(); //Submit the form
})
HTML:
<a class="nav-link active women productlink" href="#">Women</a>
<form id="toPost" action="data.php" method="POST">
<input type="hidden" name="variable" value="">
</form>
On your data.php:
<?php
if (isset($_POST['variable']))
{
echo($_POST['variable']);
}
else
{
echo ("failure");
}
?>
I think it should be data: {variable:test} not data: {'variable':test}
add this to your frontend file
<input type="button" class="women" value="Button" name="ash_b" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js" >
$('.women').click(function(){
var test="hello";
$.ajax({
type: "POST",
url: 'data.php',
data: {'variable':test},
success:function(data){
console.log(data);
},
});
$(".women").attr('href','data.php');
})
</script>
And in your data.php use following code
if (isset($_POST['variable']))
{
echo($_POST['variable']);
}
else
{
echo ("failure");
}
it works perfectly for me, try this if this doesnt work show your both files
Please try this code:
$('.women').click(function(){
var test="hello";
var datastr = 'test='+test;
$.ajax({
type: "POST",
url: 'data.php',
data: datastr,
success:function(data){
console.log(data);
},
});
$(".women").attr('href','data.php');
})
I have OOP php file and I would like to call one of the function by AJAX, i have read a lot about this (mostly stackoverflow), but for some reason, it just doesnt work.
I know that ajax function is called (I tried to add some alert into success function and that alert appeared after clicking the button), but somehow it ignores everything from file ajax.php (i even tried to add some echo at the top of ajax.php file, but nothing happened)
also note that I need button, not input (although that would be much easier)
this is my button:
<button class='formular-button' type='button' onclick='prihlasPA()'> vypiš přihlaš </button>
this is my script for button:
<script>
function prihlasPA() {
$.ajax({
type: "POST",
url: "ajax.php",
data: {action: 'prihlasP'},
success: function(){},
error: function(){
alert("chyba");
}
});
}
</script>
and this is inside ajax.php, which is called by AJAX:
include( 'navstevnik.php' );
echo "aaa";
if(isset($_POST['action']) ) {
$navstevnik = new navstevnik; //navstevnik is name of my class btw
$navstevnik->vypisPrihlas();
}
I dont think it matters, but in my function vypisPrihlas() is this code:
public function vypisPrihlas(){
$this->index=1;
echo '
<fieldset class="formular">
<div class="pure-control-group stred" >
<input id="nick" type="text" name="nickP" placeholder="Nickname">
</div>
<div class="pure-control-group stred">
<input id="password" type="password" name="hesloP" placeholder="Heslo">
</div>
<input class="pure-button pure-button-primary stred" style=width:188px; type="submit" name="buttonP" value="přihlásit" />
</fieldset>';
}
I don't see any issues with the above code.
When the function vypisPrihlas echos the HTML, you won't see it on your main page. Instead, you need to load it in from your success function and append the HTML somewhere on your page.
$.ajax({
type: "POST",
url: "ajax.php",
data: {action: 'prihlasP'},
success: function(data){
$( ".your_favorite_div" ).append(data);
},
error: function(){
alert("chyba");
}
});
By what you said, the ajax function is fine, right?
I think your problem is the path for PHP, since it is not called. try change this line
url: "ajax.php",
for the complete path, like \var\www\path_to_code\ajax.php. Maybe it helps.