Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/273212-is-this-actually-works/
Share on other sites

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?

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>';
}

 

 

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 :)

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.

@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 :)

I have all the code but it's actually 300 line so I wont actually display it all :P

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 :)

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.

@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 :)

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:)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.