I am trying to delete something from the Database. After that didn't work, I tried to simply echo $id. But this did not work either.
if (isset($_POST['aktion']) and $_POST['aktion']=='loeschen') {
$id = "";
if (isset($_POST['nummer'])) {
$id = trim($_POST['nummer']);
echo 'Hello '.$id.'!';
}
}
I use the same method to fill the database which works.
This is the code for the button:
<input type="hidden" name="aktion" value="loeschen">
<input type="submit" value="loeschen">
I used the same code on the buttons to add data to the database and it works great.
Related
I am trying to create a 'submit' input type button that when clicked will call up a switch case action that I've defined in PHP. When the 'submit' button is clicked, I want the form action to essentially create a link and display in the URL form action so that it is called properly by the PHP file.
However when I click on the submit button, the URL does not properly display the desired link and action.
PHP:
$getuserid_result = mysql_query($getuseridsql, $conn);
$userid_array = mysql_fetch_array($getuserid_result);
$userid = $userid_array['uid'];
// Above code works fine and retrieves the current user's ID
// PHP Form code is below
// Variables used for creating the id of the input since the submit button is displayed
// in an HTML table with multiple rows. These variables are working fine.
$time_cell_row = 1;
$time_cell_column = 1;
echo "<form action='enter_time.php?uid='" . $userid . "?action=timesubmit method='get'>";
echo "<td><input name=submit_time type=submit id=submit_time" . $time_cell_row . $time_cell_column . "></input></td>";
echo "</form></tr>";
// PHP Action code
/* This is currently commented out and will eventually be filled with code to handle
the 'timesubmit' action
if (isset($_GET['action'])) {
switch (strtolower($_GET['action'])) {
case 'timesubmit':
}
}
*/
The problem now is when I click on the 'submit' button, the URL displayed enter_time.php?submit_time=Submit" instead of "enter_time.php?uid=3?action=timesubmit
You might want to add in the final apostrophe after timesubmit
echo "<form action='enter_time.php?uid=" . $userid . "?action=timesubmit' method='post'>";
You have a quote after uid that should not be there:
"<form action='enter_time.php?uid=" . $userid . "?action=timesubmit method='get'>";
If you are using a form to submit GET variables into a url you could do something like
<a id="submit_time<?= $time_cell_row . $time_cell_column ?>" href="enter_time.php?uid=<?= $userid ?>">Submit Time</a>
If you prefer to use a form, writing it this way looks clearer to me
PHP
<?php
$getuserid_result = mysql_query($getuseridsql, $conn);
$userid_array = mysql_fetch_array($getuserid_result);
$userid = $userid_array['uid'];
// Above code works fine and retrieves the current user's ID
// PHP Form code is below
// Variables used for creating the id of the input since the submit button is displayed
// in an HTML table with multiple rows. These variables are working fine.
$time_cell_row = 1;
$time_cell_column = 1;
?>
<form action='enter_time.php' method='get'>
<input type="hidden" name="action" value="<?= $timesubmit ?>">
<input type="hidden" name="uid" value="<?= userid ?>">
<input name="submit_time" type="submit" id="submit_time<?= $time_cell_row . $time_cell_column ?>" />
</form>
<?php
// PHP Action code
/* This is currently commented out and will eventually be filled with code to handle
the 'timesubmit' action
if (isset($_GET['action'])) {
switch (strtolower($_GET['action'])) {
case 'timesubmit':
}
}
*/
?>
Just off top of my head it should be:
echo "<form action='enter_time.php?uid=" . $userid . "?action=timesubmit' method='get'>";
Single quote after "uid=" was in the wrong place. Shouldn't be until after "timesubmit".
im trying to change the value of a field in $_POST so that it will load the correct content for whatever has been posted.
When a user goes onto my website $_POST is initially set as
$_POST['order_page_content'] = 'list'
I then have a form in my view.content.php file that looks like this:
<td><form action='http://localhost/magento_soap_client/fulfilment' method='POST'>
<input type='hidden' name='order_page_content' value='info'/>
<input type='hidden' name='order_id' value='$order_info->order_id'/>
<input type='submit' value='View'/>
</form></td></tr>";
So the value of $_POST['order_page_content'] should change to info when the button is clicked. Therefore the content of the page should change...
Once the button is clicked, i can see via firebug that the value has changed in the post, its just my page isn't getting set to it
So how do i make the content change based on the post value that got submitted? Or what is going wrong?
EDIT
Ok guys ive tried what you said, but its still not happening, i dont know if i have put it in the wrong place or what... i think the easiest way is to show you my code.
The script to decide which content it is is here:
link
and the script that calls it is here:
public function get_html() {
$m_html = null;
if($_POST['order_page_content'] == 'list'){
$m_obj_html = new content();
$m_html = $m_obj_html->get_page_content('list');
}
else if($_POST['order_page_content'] == 'info'){
$m_obj_html = new content();
$m_html = $m_obj_html->get_page_content('info');
}
return $m_html;
}
The post['order_page_content'] is getting set in the bootstrap initially to list
Thanks
<input type='hidden' name='order_id' value='<?php echo $order_info->order_id; ?>'/>
if it's already in php use
<input type='hidden' name='order_id' value='". $order_info->order_id ."'/>
$_POST is set globally from PHP.
If you have something in your script like:
$_POST['order_page_content'] = 'list';
So your variable is on each page load overwritten.
Try to check for it before you set it.
if (!array_key_exists('order_page_content', $_POST)
{
$_POST['order_page_content'] = 'list';
}
$_POST is set with post data sent from the client when they access the site. You can access it if they send any. Your form looks like it should work. What I do see though is that you make them send this against a page on the localhost. Is localhost running an apache or similar server with PHP? If it is, then on that page add this:
if(!empty($_POST['order_id'])){
print_r($_POST);
}
If that form is on the page you send them to, and you want the value to be remembered (still be there after they send in the form/post data, then do this:
<input type='hidden' name='order_id' value='<?php if(!empty($_POST['order_id'])) echo $_POST['order_id']; ?>' />
Tom You can check it with the use of if condition
if($_POST['order_page_content']=="info"){some code} else { some code}
Seems like the problem was this line:
<form action='http://localhost/magento_soap_client/fulfilment' method='POST'>
The url in action was resetting everything, so i have now taken it out and left action blank and works fine
Thanks for your help though guys
I am trying to setup a website that converts a Steam user ID into an auth ID. It will ask for the visitor to input their regular Steam ID and then hit a button to convert it to auth ID. Steam provides us with the function for ID conversion from one type to the other.
Steam function for converting IDs:
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
Below is my attempt at setting up a basic HTML page that gets user input and then uses the function to convert that input to something else.
<INPUT TYPE = "Text" VALUE ="ENTER STEAM:ID" NAME = "idform">
<?PHP
$_POST['idform'];
$steamid = $_POST['idform'];
?>
Also, this is what the default Steam user ID looks like:
STEAM_0:1:36716545
Thank you for all the help!
If you can make it into two seperate files, then do so.
foo.html
<form method="POST" action="foo.php">
<input type="text" value="ENTER STEAM:ID" name="idform" />
<input type="submit" />
</form>
foo.php
<?php
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
$id = $_POST['idform'];
convert_steamid_to_accountid($id)
?>
if you don't have an option of making two seperate files, you can add the php code to 'foo.html' file and make the form to submit to the same file. However if you do this, check if the file is getting requested the first time, or it is requested because the form is submitted, BEFORE you call convert_steamid_to_accountid() function.
You can do this by:
if ($_SERVER['REQUEST_METHOD']=='POST'){
// your php code here that should be executed when the form is submitted.
}
Okay, I'm sure I'm missing something simple, but I'm populating a hidden field on a form with data from a MySQL While statement. the data shows up properly when on the page, but when submitted, the data submitted is always from the last loaded id.
while ($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
echo '<form name="voting" action="thanks_for_voting.php" method="POST">
<align="right"><input type="hidden" name="id" value="'.$id.'"
<br><input type="submit" value="VOTE FOR '.$id.'"><br>';
}
What am I doing wrong? I'm trying to keep a single call to my DB to prevent overloading, I assume there must be a simple way to achieve my goal? But what?
Machineadict solved my problem below by noticing I hadn't been closing my form. I closed it, and the problem was solved. Thanks!
The form tag is not closed. Lose the html tags, you don't need them. And it's alright to use forms in loops, you don't have to change the names of inputs.
As machineaddict has mentioned, if you want to use multiple forms you must close the tag of the preceding one before you open the next.
You can also do this with a single form with a single hidden field assigning it a specific value depending on which button is clicked:
echo '<html><form name="voting" action="thanks_for_voting.php" method="POST"><align="right">';
echo '<input type="hidden" name="id" value="-1">';
while ($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
echo '<input type="button" value="VOTE FOR '.$id.'" onclick="javascript:document.forms[\'voting\'].elements[\'id\'].value='.$id.'; document.forms[\'voting\'].submit();">';
}
echo '</form></html>';
Here you have a button for each of the IDs from the database. Each button executes the following JavaScript code in its onclick handler:
document.forms['voting'].elements['id'].value = YOUR_ID;
document.forms['voting'].submit();
This sets the ID to the desired value and then submits the form. You may want to put this code aside in a function and put the name of the function into the onclick handler.
Also, you probably don't want to generate multiple <html> tags.
Your <form> is inside the while loop. And as Adam Zalcman says, use a unique name.
Try
echo '<html><form name="myform" action="thanks_for_voting.php" method="POST"><align="right">';
while ($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
echo '<input type="hidden" name="id[]" value="'.$id.'"><br><input type="submit" value="VOTE FOR '.$id.'">';
}
echo '<br></html>';}
This will post an array of values, which you can retrieve as $_POST['id'][0], $_POST['id'][1] etc
I want a function that prints those 2 "print" in the database ( insert intro ) when a button is pressed.
Here's the code:
<?php
$id2name=array();
$x=mysql_query("SELECT id,name FROM products WHERE id IN(".implode(',',array_keys($_SESSION['cart'])).")");
while($y=mysql_fetch_assoc($x)){
$id2name[$y['id']]=$y['name'];
}
foreach($_SESSION['cart'] as $k=>$v){
print "<br>[".$id2name[$k]."]\t".$v."\n <br>";
}
print "<br>$total<br>";
?>
How can I make that a function, to print it in the database when a button is pressed?
Not sure if I got you right, but as far as I understand, you want to write something to the database by pressing a button, right?
Well, to trigger an action by pressing a button, you need a form:
<form action="page_to_process_the_db_request.php" method="post">
<input type="hidden" value="<?php echo $total;?>" name="total" />
<input type="submit" value="Wirite to DB!" />
</form>
In this example, I assume you want to write the variable $total to DB.
So you post the data to the processing page (which also can be the same one you're on) and there, you look if there's something in the $_POST-array:
<?php
if(isset($_POST['total'])) {
mysql_query("UPDATE products SET total = ". $total); //or something like that
}
?>
Not sure though if this is what you're looking for...
//edit
referring to your comment, I guess you want to write the output of the loop to DB...
At first, you have to create an appropriate structure, like an array:
foreach($_SESSION['cart'] as $k=>$v){
$id2name[$k]['name'] = $v;
}
now you can turn the array into a simple string with
$serialized_array = serialize($id2name);
And now you can write this string to db. And when you read it from db, you can turn it back into an array again with:
$id2name_array = unserialize($serialized_array);