ChrisPHP Posted January 15, 2013 Share Posted January 15, 2013 Hey everyone, Hope you're feeling good.. I was trying something and I can't seem to work it out.. <form action="content/QuickSearch.php" method="post"> <?php $_SESSION['nom_v']= $row['Nom_v']; $n = $_SESSION['nom_v']; echo "<center><input type=submit value=Reserve id='".$n."' name='".$n."' onmousedown='Reserve(".$n.");'/></center>"; ?> </form> <script> function Reserve(a){ <?php { $sql="SELECT Reserved FROM voiture WHERE Nom_v = '<script>a</script>' "; $row['Reserved'] = 1; header('Home.php?reserve'); } ?> } </script> The session actually works and the button is named properly.. but can I use this method? will it actually work? Because the code doesn't seem to go into the function Reserve ever... Thanks in advance anyway Regards, Chris Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/ Share on other sites More sharing options...
requinix Posted January 15, 2013 Share Posted January 15, 2013 It won't. PHP and Javascript are entirely separate, and the only way to send something from Javascript to PHP is with AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405957 Share on other sites More sharing options...
ChrisPHP Posted January 15, 2013 Author Share Posted January 15, 2013 Oh ok thanks for your quick reply:) My idea was to display all the 'cars' available in a db with a button Reserve for every car with the id = to the name of this car.. I tried the if(isset($_POST['$n'])) and it didn't work either.. Can anyone help me out with this idea please? Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405959 Share on other sites More sharing options...
kicken Posted January 15, 2013 Share Posted January 15, 2013 You can just create a separate form for each button with a hidden input. Eg: foreach ($Cars as $car){ echo '<form action="content/QuickSearch.php" method="post">'; echo '<input type="hidden" value="'.$car['id'].'" name="n">'; echo '<input type="submit" value="Reserve">'; echo '</form>'; } Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405965 Share on other sites More sharing options...
ChrisPHP Posted January 15, 2013 Author Share Posted January 15, 2013 Yea I did this.. but the if(isset($_POST['$n']))... wouldn't actually work when pressing the button.. Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405968 Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 Probably because '$n' means the literal string '$n'. You probably want to use $n. But the code you've posted has nothing showing either of those. Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405969 Share on other sites More sharing options...
ChrisPHP Posted January 15, 2013 Author Share Posted January 15, 2013 Yea I know this code uses a different idea or method.. The original code used to have ths instead of the function Reserve(): <?php if(isset($_POST[$n])){ $sql="SELECT Reserved FROM voiture WHERE Nom_v = '$n' "; $result=mysql_query($sql); $row = mysql_fetch_array($result); $row['Reserved'] = 1; header('Home.php?reserve'); } But it didn't work so I tried this method above Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405972 Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 And where is $n defined? Before you respond, I suggest you read the links in my signature about how to get good help. Otherwise this is going to take 5 times longer than it needs to, and I just don't care to do that. Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405974 Share on other sites More sharing options...
Psycho Posted January 15, 2013 Share Posted January 15, 2013 Well, I say we take a step back first. You are trying to output a list of records and display a button next to each of them. Great, we got that. So, what - exactly - are you wanting to happen when the user clicks a button? Based upon the first post the user would get redirected to the same page no matter what button was pressed. I *think* you are wanting to let the user "reserve" multiple records on the same page by clicking multiple buttons without having to refresh the page. If that is the case you have two options: 1. Use AJAX. Trying to teach you thins in a forum post will not be productive. You can use frameworks (such as JQuery) to do a lot of the difficult part - but you will still need to do the work of how to implement this. 2. Forget about the multiple buttons. Give the user a checkbox next to each record and a button to submit the entire page and all their selections in one go. Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405978 Share on other sites More sharing options...
ChrisPHP Posted January 15, 2013 Author Share Posted January 15, 2013 @Jessica The $n was defined in $_SESSION['nom_v']= $row['Nom_v']; $n = $_SESSION['nom_v']; mentioned earlier in my first post @Psycho The idea was next each car there's a button... When the button is clicked it will change the row 'reserve' to 1 (to define that the car is reserved) and redirects the user to Home.php?reserved where it will display that the car is successfully reserved.. I'm using Jquery in all of the site but this idea seemed more appropriate like this... :/ Thanks again for your help all of you Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405983 Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 And where is $row['Nom_v']; defined? Do you see a pattern yet? Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405984 Share on other sites More sharing options...
ChrisPHP Posted January 15, 2013 Author Share Posted January 15, 2013 I have all the code but it's actually 300 line so I wont actually display it all But I can assure you that the $n and the $row['Nom_v'] actually work and they're defined properly.. I was just wondering if there's a way to work it out because I'm frustrated by it.. If you want just drop it out don't worry I'll probably someday will figure it out lol Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405987 Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 I say you should go with what Psycho said, but if you want to keep trying to make this code work you have to explain what you mean by "doesn't work". What part doesn't work? What steps have you taken to debug it? Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405990 Share on other sites More sharing options...
Psycho Posted January 15, 2013 Share Posted January 15, 2013 The idea was next each car there's a button... When the button is clicked it will change the row 'reserve' to 1 (to define that the car is reserved) and redirects the user to Home.php?reserved where it will display that the car is successfully reserved.. Ok, I think you are making this more difficult than it needs to be. You can just make each button its own form and the button is a submit button. And in each of these forms create a hidden field that has the value of the record ID. Lastly, set the action of the form to the page where you want to redirect them and set the method of the form to GET. When the user clicks the button the form will send the form values (i.e. the hidden field) on the query string to the requested page. That page can then take that ID and perform the update you want to achieve. Alternatively, forget the button altogether and just create a "Reserve" link next to each record as a hyperlink with the ID already included in the URL of the hyperlink. based upon the new information, I think AJAX would be a waste for this solution. Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405991 Share on other sites More sharing options...
ChrisPHP Posted January 15, 2013 Author Share Posted January 15, 2013 @Jessica Well the code won't go through the if(isset) loop and it won't direct to the page set nor it would change the value of the reserved to 1.. I tried debugging the code with firebug and get no errors.. @Psycho Ok I think I get what you mean i'll work on that and i'll keep u updated Thanks a million both of u Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1405993 Share on other sites More sharing options...
ChrisPHP Posted January 15, 2013 Author Share Posted January 15, 2013 Ok so I figured out a way for this code to work thanks for Psycho I used echo '<form action="content/QuickSearch.php" method="get">'; $n=$row['Nom_v']; echo '<input type="hidden" value="'.$n.'" name="n" >'; echo "<center><input type=submit value=Reserve id='".$n."' name='".$n."' /></center>"; echo '</form>'; if(isset($_GET['n'])){ $n = $_GET['n']; $sql="SELECT Reserved FROM voiture WHERE Nom_v = '$n' "; $result=mysql_query($sql); $row = mysql_fetch_array($result); $sqll="UPDATE `voiture` SET `Reserved`='1' WHERE Nom_v = '$n' "; $res=mysql_query($sqll,$dbcon); $res=mysql_query($sql,$dbcon); if(!$res) { echo "<html><font size='4' color='white'><b>Can't remove..</b></font></html>"; die(mysql_error()); } else { header('Location: ../Home.php?reserve'); } mysql_close($dbcon); } And it's now fully working Thanks a lot again and again and again all of u:) Quote Link to comment https://forums.phpfreaks.com/topic/273212-is-this-actually-works/#findComment-1406000 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.