richiec Posted April 27, 2009 Share Posted April 27, 2009 Im trying to get a conformation pop up in php when someone presses a button but this is the first time ive ever tried to add something like this with mixing javascript and php together.. Right now it echos "You canceled your bid." for both yes and no so any ideas of how to fix this would be appreciated! The code is below.. <SCRIPT language="JavaScript"> <!-- function go_there() { var where_to= confirm("Do you really want to?"); if (where_to== true) { <?php $confirmbid = "yes"; ?> } else { <?php $confirmbid = "no"; ?> } } //--> </SCRIPT> and then the form part echo "<input type=\"submit\" name=\"auctionbid\" value=\"BID\" onClick=\"go_there()\">"; and then the follow through after its pressed.. if ($confirmbid == "no"){ echo "You canceled your bid."; } else { ///do query } Thanks ~Rich Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 27, 2009 Share Posted April 27, 2009 javascript and PHP can not talk to each other in that way. Since one is parsed server side and the other client side, the only places these two can talk is through post and get variables ( I think, someone correct me if I am wrong) Also, you cant change variables in a PHP page once it has been loaded without reloading the page. What you want to do can be done with PHP fairly easily if you submit the form, but using Javascript and PHP in the way you want is impossible I'm afraid (as far as I know. again I could be wrong) Quote Link to comment Share on other sites More sharing options...
richiec Posted April 27, 2009 Author Share Posted April 27, 2009 ok so pressing "yes" could just reload to a ?confirm=yes and pressing "no" would just reload back to normal page and then it would be able to get it from there i guess Quote Link to comment Share on other sites More sharing options...
richiec Posted April 27, 2009 Author Share Posted April 27, 2009 ok so pressing "yes" could just reload to a ?confirm=yes and pressing "no" would just reload back to normal page and then it would be able to get it from there i guess Or not ^^ lol it loses the variables it gets when clicking bid when java changes the page i hate javascript ha Isnt there any way to do a confirmation box in just php oO Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted April 27, 2009 Share Posted April 27, 2009 ok so pressing "yes" could just reload to a ?confirm=yes and pressing "no" would just reload back to normal page and then it would be able to get it from there i guess Or not ^^ lol it loses the variables it gets when clicking bid when java changes the page i hate javascript ha Isnt there any way to do a confirmation box in just php oO no you're right. do this with javascript: window.location = 'http://www.yoursite.com/index.php?confirmed=yes' and on the index.php get the confirmed from this: $_GET['confirmed'] Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 27, 2009 Share Posted April 27, 2009 well, Im not versed to well in Javascript (I know a good amount of it, but your specific problem would take a little bit of research on my part to answer) can you do a confirmation type box in PHP? of course, but it won't really be as dynamic as in javascript (like you wont be able to make a pop-up box really). you could just make a form with two submit buttons, one called yes, and one called no, as so <input type=\"submit\" name=\"yes\" value=\"BID\" >"; <input type=\"submit\" name=olue=\"BID\">"; and on your php page do something like if (isset($_POST['yes'])){ //blah blah } else if ($_isset($_POST['no'])){ //blah blah } else { echo "YOU AINT CLICK NUTHIN" exit(); } is that what you were looking for? I hope that helps Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 27, 2009 Share Posted April 27, 2009 ok so pressing "yes" could just reload to a ?confirm=yes and pressing "no" would just reload back to normal page and then it would be able to get it from there i guess Or not ^^ lol it loses the variables it gets when clicking bid when java changes the page i hate javascript ha Isnt there any way to do a confirmation box in just php oO no you're right. do this with javascript: window.location = 'http://www.yoursite.com/index.php?confirmed=yes' and on the index.php get the confirmed from this: $_GET['confirmed'] Or do that lol Quote Link to comment Share on other sites More sharing options...
richiec Posted April 27, 2009 Author Share Posted April 27, 2009 ok so pressing "yes" could just reload to a ?confirm=yes and pressing "no" would just reload back to normal page and then it would be able to get it from there i guess Or not ^^ lol it loses the variables it gets when clicking bid when java changes the page i hate javascript ha Isnt there any way to do a confirmation box in just php oO no you're right. do this with javascript: window.location = 'http://www.yoursite.com/index.php?confirmed=yes' and on the index.php get the confirmed from this: $_GET['confirmed'] Yeah thats how i just had it set up after reading his reply but it lost the php post variables for clicking the button before java reloads the page and didnt go through Quote Link to comment Share on other sites More sharing options...
richiec Posted April 27, 2009 Author Share Posted April 27, 2009 well, Im not versed to well in Javascript (I know a good amount of it, but your specific problem would take a little bit of research on my part to answer) can you do a confirmation type box in PHP? of course, but it won't really be as dynamic as in javascript (like you wont be able to make a pop-up box really). you could just make a form with two submit buttons, one called yes, and one called no, as so <input type=\"submit\" name=\"yes\" value=\"BID\" >"; <input type=\"submit\" name=olue=\"BID\">"; and on your php page do something like if (isset($_POST['yes'])){ //blah blah } else if ($_isset($_POST['no'])){ //blah blah } else { echo "YOU AINT CLICK NUTHIN" exit(); } is that what you were looking for? I hope that helps What i am doing is making a kind of auction website for a game i play my guild members earn points for doing different things and then every week they bid against eachother with the points they have earned on the items we have won.. The auction works fine its just some noobs kept messing up there bids so i just wanted to add a conformation box to try and cut down on the amount of people who mess up and bid on the wrong thing.. So unfortunetly what you posted isnt what im looking for, unless i misunderstood what you were saying to do.. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 27, 2009 Share Posted April 27, 2009 oh, yeah I misunderstood what you were trying to do. window.location = 'http://www.yoursite.com/index.php?confirmed=yes' should work just fine to be honest. Can you post the javascript you are using with your confirmation box. by the way, dont EVER call javascript java or vice versa. Javascript, and Java are two completely different programming languages, and calling them the same thing can get you yelled at lol. Quote Link to comment Share on other sites More sharing options...
richiec Posted April 27, 2009 Author Share Posted April 27, 2009 oh, yeah I misunderstood what you were trying to do. window.location = 'http://www.yoursite.com/index.php?confirmed=yes' should work just fine to be honest. Can you post the javascript you are using with your confirmation box. by the way, dont EVER call javascript java or vice versa. Javascript, and Java are two completely different programming languages, and calling them the same thing can get you yelled at lol. This is the java"script" hehe <SCRIPT language="JavaScript"> <!-- function go_there() { var where_to= confirm("Do you really want to bid?"); if (where_to== true) { window.location="?auction=view&confirm=yes"; } else { window.location="?auctions=view"; } } //--> </SCRIPT> get part if ($_REQUEST["auctionbid"] != NULL){ //// $confirmbid = $_GET['confirm']; if ($confirmbid == ""){ echo ""; } else { blah blah } Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 27, 2009 Share Posted April 27, 2009 hmm that looks like it SHOULD work... instead of what you have try function go_there() { var where_to= confirm("Do you really want to bid?"); if (where_to)//since this already has a bool value in it there is no reason to check whether or not the value is true { window.location="?auction=view&confirm=yes"; } else { window.location="?auctions=view"; } } Quote Link to comment Share on other sites More sharing options...
richiec Posted April 27, 2009 Author Share Posted April 27, 2009 No change, however i did notice something... the url is not changing it doesnt add the &confirm=yes into the url oO edit: i just changed it to load this website after clicking ok and all it did was load the auctions page again.. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted April 27, 2009 Share Posted April 27, 2009 OOOH i see the problem (I think) When you do window location, if you dont add an absolute URL (IE http://www.website.com) then it just appends the current URL with what you are going to. try instead of what you have, putting the absolute url, and see if there is a problem. I may be completely wrong though. As I said, my knowledge of javascript is not somewhat lacking (this is the PHP forums ya know!) hope that helps! Quote Link to comment Share on other sites More sharing options...
richiec Posted April 27, 2009 Author Share Posted April 27, 2009 Ive tried it with both full links and the short one didnt do anything that was one of the first things i tested but still no change. Thanks alot for the help though mikesta707 <3 its like 5am here now though so im going to head to bed and check back tomorrow and see if anyones come up with anything Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted April 27, 2009 Share Posted April 27, 2009 i tried the below code on my server and its working perfectly fine. post ALL your code that has anything to do with this project. Including where you call the function, etc. <script type="text/javascript"> function go_there() { var where_to= confirm("Do you really want to bid?"); if (where_to) { window.location="http://www.google.com/?auction=view&confirm=yes"; } else { window.location="http://www.google.com/?auctions=view&confirm=no"; } } </script> <a href="#" onClick="go_there()">test</a> Quote Link to comment Share on other sites More sharing options...
richiec Posted April 30, 2009 Author Share Posted April 30, 2009 Alright as i said before what im trying to do is have a kind of auction set up (which is working fine) however i would like to add a conformation box whenever they bid on an item due to some noobs not paying attention when bidding. Here is the javascript <SCRIPT language="JavaScript"> function go_there() { var where_to = confirm("Only click ok if you are sure its the item you want to bid on!"); if (where_to == true) { window.location = "?confirm=yes"; } else { window.location = "?auctions=view"; } } </SCRIPT> It then calls the function when bidding <form name=\"bid\" method=\"post\" action=\"\" target=\"_self\"> <center><input type=\"text\" name=\"bidnumber\" value=\"$newbid\" size=\"3\" maxlength=\"3\" onKeyPress=\"return numbersonly(this, event)\"> <input type=\"submit\" name=\"auctionbid\" value=\"BID\" onClick=\"go_there()\"><input type=\"hidden\" name=\"auctionid\" value=\"$idid\"></center> </form> Now that form needs to get a few things (if they press ok on the conformation box) for example it needs to get the amount that was bidded and the auctionid If the person presses ok on the conformation box it then does an sql queery to update the things if they press cancel obviously it doesnt.. This is the sql queery for the example if ($_REQUEST["auctionbid"] != NULL){ //// $confirmbid = $_GET['confirm']; if ($confirmbid == "yes"){ $nick = $_SESSION["wishlistusername"]; $bidnumber = $_POST['bidnumber']; $auctionid = $_POST['auctionid']; $query="SELECT * FROM ` blah blah blah.... } else { do nothing since they pressed cancel } I really hope someone can help =\ i know its possible to do i just dont know how to do it correctly... Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted April 30, 2009 Share Posted April 30, 2009 you need to define action in the form tag. then you would do something similar to this: function go_there() { var where_to = confirm("Only click ok if you are sure its the item you want to bid on!"); if (where_to == true) { document.bid.submit(); } else { //do nothing } } Quote Link to comment Share on other sites More sharing options...
richiec Posted April 30, 2009 Author Share Posted April 30, 2009 Still couldnt get it working so opted for a simple check box which they have to tick to confirm lol but thanks for the help all >< ~Rich Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 Why not do something like this: something.html <!-- HTML here --> <form action="/path/to/something.php" method="post" name="auction" onsubmit="return validate();"> <input type="radio" name="item" value="ball" /> Ball <input type="radio" name="item" value="yoyo" /> Yo-yo <input type="submit" name="submit" value="Submit" /> Submit </form> <!-- more HTML here --> <script type="text/javascript"> function validate () { var options = document.forms.auction.item.value; for (var e = options.length; --e > -1; ) { if (options[e].checked) { return confirm("Are you sure you want to select " + options[e].value); } } return false; } </script> something.php <?php // check your PHP version to make sure filter_input works. // if not, use the superglobal $option = filter_input(INPUT_POST, 'item'); // do something ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.