budder Posted April 26, 2010 Share Posted April 26, 2010 In this script I want to take som values out of the url. I want the script to discover what the action= and the type= is. But i seems to be an error: Notice: Undefined index: action in C:\wamp\www\billet\commit.php on line 12 It is not taking the value edit/add out of the url part:action=edit/add Some clues? commit.php <?php $db = mysql_connect('localhost', 'root') or die('unable to connect..'); mysql_select_db('my_db', $db) or die(mysql_error($db)); ?> <html> <head> <title>Commit</title> </head> <body> <?php switch ($_GET["action"]) { case "add": switch ($_GET["type"]) { case "billet": $query = "INSERT INTO ticket_user (order_nr,navn,billet_type,antal,afsendt) VALUES ('" . $_POST['order_nr'] . "', '" . $_POST['navn'] . "', '" . $_POST['billet_type'] . "', '" . $_POST['antal'] . "', '" . $_POST['afsendt'] . "')"; break; } break; case "edit": switch($_GET['type']) { case "billet": $query = "UPDATE movie SET order_nr ='" . $_POST['order_nr'] . "', navn ='" . $_POST['navn'] . "', billet_type = '" . $_POST['billet_type'] . "', antal = '" . $_POST['antal'] . "', afsendt ='" . $_POST['afsendt'] . "' WHERE order_id ='" . $_POST['id'] . "'"; break; } break; } if (isset($query)) { $result = mysql_query($query) or die(mysql_error($db)); echo "<p>Færdig!</p>"; } ?> </body> </html> the billet.php if needed: <?php $db = mysql_connect('localhost', 'root') or die('unable to connect..'); mysql_select_db('my_db', $db) or die(mysql_error($db)); if($_GET['action'] == 'edit') { $query = "SELECT order_nr, navn, billet_type, antal, afsendt FROM ticket_user WHERE order_id = " . $_GET['id']; $result = mysql_query($query, $db) or die(mysql_error()); extract(mysql_fetch_assoc($result)); } else { $order_nr = 0; $navn = ""; $billet_type = ""; $antal = 0; $afsendt = ""; } ?> <html> <head> <title><?php ucfirst($_GET['action']); ?> Movie</title> </head> <body> <form action="commit.php?action=<?php echo $_GET['action']; ?>&type=billet" methode="post"> <table> <tr> <td width="97">Order Nummer</td> <td width="149"><input type="text" name="order_nr" value="<?php echo $order_nr; ?>" /></td> </tr><tr> <td>Navn</td> <td><input name="navn" type="text" value="<?php echo $navn; ?>" /></td> </tr><tr> <td>Billet Type</td> <td><input name="billet_type" type="text" value="<?php echo $billet_type; ?>" /></td> </tr><tr> <td>antal</td> <td><input name="antal" type="text" value="<?php echo $antal; ?>" /></td> </tr><tr> <td>Afsendt</td> <td><input name="afsendt" type="text" value="<?php echo $afsendt; ?>" /></td> </tr> <td colspan=”2” style="text-align: center;"> <?php if ($_GET['action'] == "edit") { echo "<input type='hidden' value='" . $_GET['id'] . "' name='order_id' />"; } ?> <label> <input type="submit" name="submit" value="<?php ucfirst($_GET['action']); ?>" /> </label></td></table></form> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/ Share on other sites More sharing options...
de.monkeyz Posted April 26, 2010 Share Posted April 26, 2010 What URL are you typing when this happens? That error is caused when a value was never assigned, which will also happen when you type nothing into the query string. So you should always have a line like this: $_GET['action'] = empty($_GET['action']) ? 'default_action' : $_GET['action']; This allows you to set the default action on the page Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1048503 Share on other sites More sharing options...
developerdave Posted April 26, 2010 Share Posted April 26, 2010 What version of PHP are you using? HAve you tried $_REQUEST['action']; ? Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1048508 Share on other sites More sharing options...
budder Posted April 27, 2010 Author Share Posted April 27, 2010 Im using the newest version of php. My url is: billet.php?action=edit&id= billet.php?action=add commit.php?action=<?php echo $_GET['action']; ?>&type=billet ---------------------------------^add/edit^--------------------------- commit should find out if the link is coming from the link billet.php?action=edit&id= or billet.php?action=add But i doesn't look like it taking anything from the url. Because my submit button doesn't have any name: <input type="submit" name="submit" value="<?php ucfirst($_GET['action']); ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1049128 Share on other sites More sharing options...
developerdave Posted April 27, 2010 Share Posted April 27, 2010 Have you tried echo'ing the $_GET array to see whats in there Try these <pre> <?php print_r($_GET); ?> </pre><pre> <?php print_r($_REQUEST); ?> </pre> And see which one works, if either do or not Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1049147 Share on other sites More sharing options...
budder Posted April 27, 2010 Author Share Posted April 27, 2010 Array ( [order_nr] => 54321 [navn] => Mag [billet_type] => Wulff [antal] => 5 [afsendt] => 2010-04-22 [order_id] => 1 [submit] => ) Array ( [order_nr] => 54321 [navn] => Magnus Herhold [billet_type] => Mikael Wulff [antal] => 5 [afsendt] => 2010-04-22 [order_id] => 1 [submit] => ) As you can see submit do not return anything. Hmm. Why can't i get my information any further? edit: The information isn't getting registered in billet.php even tough the link is sending the information billet.php?action=add or edit&id= on the admin page: <?php $db = mysql_connect('localhost', 'root') or die('unable to connect..'); mysql_select_db('my_db', $db) or die(mysql_error($db)); ?> <html> <head> <title>Billet Database</title> <style type="text/css"> th { background-color: #999; } .odd_row { background-color: #EEE; } .even_row { background-color: "FFF; } </style> </head> <body> <table style="width:100%;"> <tr> <th colspan="5">Billetter <a href="billet.php?action=add">[TILFØJ]</a></th> //add </tr> <?php $query = "SELECT * FROM ticket_user"; $result = mysql_query($query, $db) or die(mysql_error($db)); $odd = true; while ($row = mysql_fetch_assoc($result)) { if ($odd == true) { echo "<tr class='odd_row'>"; } else { echo "<tr class='even_row'>"; } $odd = !$odd; echo "<td style='width:10%;'>"; echo $row['order_nr']; echo "</td>"; echo "<td style='width:75%;'>"; echo $row['navn']; echo "</td><td>"; echo " <a href='billet.php?action=edit&id=" . $row['order_id'] . "'> [REDIGER]</a>"; //edit echo " <a href='delete.php?type=billet&id=" . $row['order_id'] . "'> [sLET]</a>"; //delete echo '</td></tr>'; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1049151 Share on other sites More sharing options...
developerdave Posted April 27, 2010 Share Posted April 27, 2010 Submit indicates you want a form, but I can't see any <form> tags anywhere and I can't see where any of those $_GET variables are define, could you post all of your script so we can help debug this with you? Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1049155 Share on other sites More sharing options...
budder Posted April 27, 2010 Author Share Posted April 27, 2010 In my billet.php I got this: (look at my first post codes in there) <form action="commit.php?action=<?php echo $_GET['action']; ?>&type=billet" methode="post"> It all starts at admin. Here there links that send information by the url like <a href="billet.php?action=add">ADD</a> In the billet.php you can add or edit the information. If you wanna add information the url is billet.php?action=add if it's editing then its billet.php?action=edit&id=#id In the commit.php it should see if you have chosen to edit og add (mysql:INSERT or UPDATE) It determents what to do buy the url. If action = add then INSERT if action = edit then UPDATE. I have posted the billet.php and commit.php at the top and the admin.php some posts up. Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1049177 Share on other sites More sharing options...
firedealer Posted May 10, 2010 Share Posted May 10, 2010 That error is caused when a value was never assigned, which will also happen when you type nothing into the query string. So you should always have a line like this: $_GET['action'] = empty($_GET['action']) ? 'default_action' : $_GET['action']; This allows you to set the default action on the page I was just search on this forum for a solution and discover this post. It resolves my specific problem. I modified to this: $_GET['action'] = empty($_GET['action']) ? NULL : $_GET['action']; So, whenever URL have no $_GET available... it will result without Notice messages at all for me. Hope it'll help to resolve for people who having a trouble with this. Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1055701 Share on other sites More sharing options...
Muddy_Funster Posted May 10, 2010 Share Posted May 10, 2010 change <form action="commit.php?action=<?php echo $_GET['action']; ?>&type=billet" methode="post"> to this : <form action="commit.php?action="<?php echo $_GET['action']; ?>"&type=billet" method="post"> Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1055714 Share on other sites More sharing options...
developerdave Posted May 10, 2010 Share Posted May 10, 2010 change <form action="commit.php?action=<?php echo $_GET['action']; ?>&type=billet" methode="post"> to this : <form action="commit.php?action="<?php echo $_GET['action']; ?>"&type=billet" method="post"> I can't see that working, the PHP should work in the " with no issue I do it all the time. Although spelling method correctly should make all the difference. A post method will not append get variables to the action url. So you need to make these get variables hidden fields with the names of the get variables and set the value accordingly and just change in your scripts the $_GET to $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1055780 Share on other sites More sharing options...
Muddy_Funster Posted May 10, 2010 Share Posted May 10, 2010 It would have helped if I got the quotes right aswell: <form action="commit.php?action="<?php echo $_GET['action']; ?>&type=billet" method="post"> As far as I can see the $_GET isn't looking for any of the $_POST variables, it simply looks as though it's being used to set the destination page for the form action that is using the $_POST variables, so I don't thing there will be a problem with it being used. Personaly though I would assign the $_GET to a page specific variable with validation for use in the echo statement. Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1055820 Share on other sites More sharing options...
developerdave Posted May 10, 2010 Share Posted May 10, 2010 You cannot append a query string on a form action, these will not be passed for reference by the form. They need to be post variables in hidden fields for the same effect and the action file to be slightly ammended from $_GET to $_POST to use these variables. Or set the form method to get and ammend the action file to $_GET on all the posted fields. Google 'get variables on post form' and you'll find this (I had to check my facts) http://www.webmasterworld.com/forum88/5383.htm Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1056029 Share on other sites More sharing options...
kenrbnsn Posted May 10, 2010 Share Posted May 10, 2010 Yes you can append to the URL action. <?php if (isset($_GET)) { echo '<pre>$_GET:' . print_r($_GET,true) . '</pre>'; } if (isset($_POST)) { echo '<pre>$_POST:' . print_r($_POST,true) . '</pre>'; } $pass_get = (isset($_POST['pass_get']) && $_POST['pass_get'] != '')?$_POST['pass_get']:'default'; ?> <html> <head> <title>Test Get & Post</title> </head> <body> <form method="post" action="?get_param=<?php echo $pass_get ?>"> <p> <label for="pass_me">Pass on $_GET:</label><input type="text" name="pass_get" id="pass_get"><br> <input type="submit" value="Pass to $_GET"> </p> </form> </body> </html> [code] See it in [url=http://rbnsn.com/phpfreaks/get_post.php]action[/url] Ken Quote Link to comment https://forums.phpfreaks.com/topic/199767-cant-get-my-_getaction/#findComment-1056041 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.