PHP/Javascript passing message to another page - javascript

So let me explain:
I basically want so when you post a comment, (i use a js/jquery script to send string to insert.php which inserts to the database) you will receive 2+ points. Now i have done so you get +2 points, BUT i want to display a message like stackoverflow. I already know how to display a message like stackoverflow, but in somehow i need to send from insert.php(after you inserted), this:
<div id='message' onclick="closeNotice()" style="display: none;">
Hey, <b><? echo $pusername; ?></b> - You've just got +<? echo $gpm; ?> points for your comment!
X
</div>
to index.php..
I was thinking of maybe coding into my current script(that are sending string to insert.php) that it should find #message and throw it in #box (div called "box" in index.php).
But how should i do this? Should i like, after you got through insert.php, then you activate a function in javascript that does:
function showmessage() {
$("#box").html(data).find("#message").fadeIn("slow")
}
and as i said you activate the script doing:
<script type="text/javascript" language="javascript">
showmessage();
</script>
after you succesfully have inserted to database and gived points to the user?
Ive just tested this, and i cant get it to work.
my site is integrated with sessions from the phpBB login (phpBB forum i've got), so i don't think i can use $_SESSION.
And the insert.php is opened in a frame.
My problem is that the action, and the displaying of the confirmation take place on different pages.

If I understand you correctly, your problem is that the action, and the displaying of the confirmation take place on different pages.
One approach to do this is to store the message that is to be displayed on the next page in the user's session:
// insert.php
$_SESSION["user_message"] = "You were awarded +2 points.";
and output it on the following page:
// thankyou.php
echo $_SESSION["user_message"]; // Or show the box, or whatever
$_SESSION["user_message"] = null; // Clean up
the potential downside to this is that if the user has two or more pages/tabs of your site open, and navigates a lot across them, the message may appear in the wrong context. For example, if I click "save" in tab A, and refresh tab B, it could happen that the message intended for tab A is displayed in tab B.
You could help that by adding a randomly generated key to the message's variable name, and passing that key on to the page you want to display the message on:
// insert.php
$key = "123456"; // Insert random generation method here, e.g. using rand()
$_SESSION["user_message_$key"] = "You were awarded +2 points.";
header ("Location: thankyou.php?message=$key"); // Pass the key to the next page
// thankyou.php
$key = $_GET["message"]; // No sanitation necessary here AFAICS
echo $_SESSION["user_message_$key"]; // Or show the box, or whatever
$_SESSION["user_message_$key"] = null; // Clean up
This is very elegant because
the message you want to display remains in your internal session store, and at no point is passed on in the browser, reducing the risk of security holes and such.
by unsetting the session variable, you make sure the message is shown only once, even if the user reloads the page.

If insert.php is being opened by the page that needs to display the mesage, you can use window.opener to access the originating page. I think something like this might work:
window.opener.$("#message").html($("#box").html()).fadeIn("slow");

I'm not sure if I understand correctly, but if the user makes the input in a field on index.php, then maybe you could send it to insert.php with an ajax request, and use the callback of the ajax to display the notice to the user?

Related

Passing script variable to the next page

I've got spinning wheel script that I'm trying to use to give away small prizes to visitors when they login to a website. The wheel animation all works great and the script puts the 'result' into a variable and displays that in a modal on the page. You can see it in action here...
https://ezclix.club/wheel2/index.asp
The result value shows up as the third line in the modal, inserted t oa span #displayprice.
var response = "";
response += selectedSegment.winResult;
$("#displayprice").html(response);
I just need to get that value to the next page, so I can actually 'award' the prize, but so far nothing I've tried seems to work.
I tried setting a cookie but that doesn't make it through.
setCookie("Prize", response, 1)
I've tried adding a form to the modal, and using jquery to update an input with the result value, but that doesn't want to work either.
$('input[id=passprize]').val(response);
$('input[name=passprize]').val(response);
I've tried adding a whole new form input, but no go there as well.
I don't do much with jquery and my experience is mostly limited to minor edits to existing scripts, but the edits above are all things I've done in the past, so I'm stumped.
Any ideas at all much appreciated!
Here's the full function...
function alertWinResult(selectedSegment) {
jQuery(".spin_pin").rotate(0);
$("#spinWinResult").text(globlefuncgeneral.gameover_text);
$(".power_controls").hide();
var response = "";
response += selectedSegment.winResult;
$("#displayprice").html(response);
// My variousattempts...
setCookie("Prize", response, 1)
$('input[id=passprize]').val(response);
$('input[name=passprize]').val(response);
}
You can use url parameter to pass the prize when redirect to next page url/action?prize={response}
enter code here
//Using cookie
setCookie("Variable", response, value)
//using url redirect
next page url/action?Variable=value
I'm not sure how or why, but the form part of this is now working. The snippet above is populating the field so that's getting passed to the next page.
I left it for a while, came back a few hours and it worked without any further changes. So guessing there's some caching going on maybe?
Either way, sorry to waste people's time on this and thanks for your suggestions.

How to give permission to access denied on dynamic page click in php?

I want to write the access denied code in PHP on the page to check if the user comes directly to my page without the "book_id" button clicked.
If he comes to my page from the previous page using Check Below Line of code otherwise access denied.
<a class="btn btn-warning" href="book_detail.php?book_id=<?=$book_id['id'];?>" style="width:120px;"><span class="glyphicon glyphicon-shopping-cart"></span>Add To Cart</a>
Supposed:
book_id = 1 // (This is the real problem- its dynamic, could be any number 1,2,3,4,5,......)
User id = 5
I have a dynamic code in php to new page like:
<a class="btn btn-warning" href="book_detail.php?book_id=<?=$book_id['id'];?>" style="width:120px;"><span class="glyphicon glyphicon-shopping-cart"></span>Add To Cart</a>
Now I want to know:
book_detail.php?book_id=1; //This will work if user come from button click
book_detail.php?book_id=2; // This will also work if user come from button click
But This will access denied because the user directly created the URL like:
book_detail.php?book_id=5 // or any other number/id which are in the database;
I tried: But not working as I am failed to implement this. Not know how to implement this $url != 'what to pass here as I have dynamic URL';
$url != 'your-url-which-you-do-not-what-direct access';
if ($_SERVER['HTTP_REFERER'] == $url) {
header('Location: otherurl.php'); //redirect to some other page
exit();
}
Any ideas or suggestions would be welcome.
Thank You.
The approach you are trying to use might not work out for you. Try this instead. Before you render each button on a page assign it a unique id and store the id somewhere, probably in an array in session.
When the page book_detail.php?book_id=5 loads get the value of book_id, if it's present check if the value exists in the array of ids you created earlier and stored in session, If it does render ur page if it does not exist or book_id is not present display permission denied.
However keep in mind that:
1. The ids would have to be changed each time the page is refreshed.
2. Someone can still inspect ur page copy the id for the button and run in the browser although it really won't make any sense for them to do that especially since the id would expire after first use.

How do I change a HTML class via PHP?

I'm currently stuck at my "Newsletter Subscribe Form". I use MailChimp to send my newsletters and have managed to redirect the visitor of my page to a custom PHP file after entering the email.
The process goes like this:
User enter email
Email silently (user doesn't see it) gets added to the MailChimp Database (Email List)
Instant redirection to a self hosted PHP script.
4. The PHP Script changes the color of the button via a CSS Class in the HTML file.
I'm stuck right at Point 4, since I don't really know how to change a CSS Class in a HTML file with PHP. It's important that the webpage still remains in HTML (otherwise i'd use a simple variable in PHP).
Do I need to parse the PHP value to AJAX or JSON which then changes the class (probably with jquery?)
If yes, could you give me an example on how to do it? I have never really used JSON or AJAX before.
Thank you guys very much :)
It's a little bit difficult to understand the question completely.
If you're trying to output a class when the page loads, that's very easy using php:
<?php
$classVariable = "myClass";
?>
<div id="someID" class="<?php echo $classVariable; ?>">Some content</div>
PHP loads once, before anything else on the page, and never runs again. You can call other scripts using AJAX which can then return data that you can use. For example (using jquery)
<script type="text/javascript">
$.get("path-to-myscript.php", function(response){
// read the response in, perhaps you grab some class name from it
// for demo purposes, lets say this response is in json formatted as a simple string:
// '{ "class" : "someClass" }'
var data = $.parseJSON(response);
var newClass = data.class;
$("#someID").attr("class", newClass);
});
</script>

save and restore dynamic form value after ajax refresh

Got a page where users make comments on any desired post. Each post has a form with just one textarea field created dynamically from js and I'm wondering if anyone has an idea on how to save and restore the comment being typed by a user (i.e save whatever a user has typed so far before ajax refreshes the div holding all the post and comments and then restore back after the div refresh)
Tried creating something around this but not getting it to work:
<textarea id="comment_field" onKeyUp="return saveAndRestoreTypedStrings(this)"></textarea>
or
<textarea id="comment_field" onchange="saveAndRestoreTypedStrings(' + id + ');"></textarea>
The function:
function saveAndRestoreTypedStrings(id){
document.getElementById("post_comment").onchange = function() {
localStorage['post_comment'] = document.getElementById(id).value;
}
window.onload= function(){
if(localStorage['post_comment'])
document.getElementById(id).value = localStorage['post_comment'];
}
}
To identify the form a user is on, a unique id for that form had to be passed to the saveAndRestoreTypedStrings(id) function.
For clarity, ajax only refreshes the div holding all the posts and comments made on each post. So, that div is refreshed for latest posts and comment every 3 seconds and if a user is typing to make a comment on a post and ajax reloads the div content, the user loses whatever is being typed.
Would be pleased to get ideas around this....
window.onload fires up when the page is completly loaded soo i dont think this would apply for your case.
why dont you try using a callback function after AJAX is complete so you can "restore" the info by calling localStorage ?
Note: sorry, i cant comment yet.

Insert javascript variable into a form on another page?

I am wondering if there's a good way to take a variable built in Javascript and then insert it into a form that's on another page.
My application is a survey: I've got it so that at the end of the survey, all the results are displayed so they can look over their answers. Now I want to have the user click a link and have the answers of the survey show up automatically in the body of the form where they'll then add their email and contact info and click "send."
Any ideas?
Knowing that this ISN'T possible is fine too...if not, what alternate methods might I accomplish the end result?
You can create a hidden field in first page and change that value from javascript variable. When you post that page it can be another page where you need to display in form element.
EDIT
If you need without form in first page you need to link with hyperlink like below.
<a href='#' onClick='OpenPage()'> Go To Result </a>
<script>
var val=20 ; // say this is the value you want to pass to second page
function OpenPage ()
{
window.location.href = "resultpage.php?param=" + val;
}
</script>
After you need to read that param in resultpage either with javascript through URL string or from server side (in PHP $_GET).
Hope it helps.

Categories