example.php ~ when i check the checkbox in example.php webpage.. then i click the print button, the value will be pass on example1.php
if($items) {
foreach($items as $i)
{
print"<input name='checkbox[]' type='checkbox' id='checkbox[]' value='$i->logi_id'>";
}
}
echo "<a href='example1.php?checkbox=" . $checkbox . "'><input type='button' name='print' id='delete' value='Print'></a>"
?>
example2.php
<?php
$keyword = $_REQUEST['keyword'];
$keycateg = $_REQUEST['keycateg'];
$print = $_REQUEST['print'];
$checkbox = $_REQUEST['checkbox'];
$count = count($_REQUEST['checkbox']);
if($print){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "select * FROM sampleDB WHERE logi_id='$del_id'";
$result = mysql_query($sql);
}
}
?>
Simple words ---
checkbox=<?php echo $checkbox; ?>
but dont rely on user inputs,...sanitize the data
If i understand your question correctly you want to submit the form using a link, prefered way would be to style a submit button as a link, but you could ofc use javascript to submit the form using a link. you can find a more indepth explanation here
http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
I guess his question is how he can evaluate the current value of the checkbox and use it in a a href "GET" encoded style.
I would recommend not using a href, but using another form with post method "get" (that encodes the url for you).
Else your are stuck with javascript I believe.
$checkbox_string = htmlentities(http_build_query(array('checkbox'=>$checkbox)));
echo "<a href='example1.php?$checkbox_string'>...</a>"
Related
I have a searchresult.php file where I pull all the menus names out of my database and print them with a button close to them where the button has a input type hidden where I associated the name of the menu through the variable $nameofmenu . Something like
Menu1 --> Button (See Menu)
Menu2 --> Button (See Menu)
Menu3 --> Button (See Menu)
<?PHP
while($myrow = pg_fetch_assoc($result)) {
$nameofmenu = $myrow[name];
echo $nameofmenu; //It prints correctly the name of the menu
echo '<form name"formname" method="post" action="resultsmenu.php">';
echo "<input type='hidden' name='menuname' value=' $nameofmenu ' />";
echo "<input type='submit' name='submit' value='See Menu' />";
}
?>
Then I have my resultsmenu.php file that will open when I click in any of the buttons.
<?php
$nameofmenu = $_POST['menuname'];
$db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
$query = "SELECT * FROM menu where name='$nameofmenu'";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
$myrow = pg_fetch_assoc($result);
$description = $myrow[description];
?>
I have 2 Issues:
First issue: in the searchresult.php, even it prints out correctly all the different menu names, whenever I click any of the buttons, all the buttons return my the same menu (The last one, in the example: Menu 3 in the $nameofmenu = $_POST['menuname']; of the resultsmenu.php file.
Second issue: this issue happend in the resultsmenu.php file. Even for Menu 3, it seams that the recognized the value in the variable. Meaning I don't get back any result or value from the query: $query = "SELECT * FROM menu where name='$nameofmenu'";
However, if I set up the value Manually like $nameofmenu = "Menu3"; then It works.
I even tried this code to see if there is any different in the value passed from $_POST['menuname'] and the value typed in manually and it seams to be the same value because it prints "Both variables are the same"
$nameofmenu2 = "Menu3";
$nameofmenu = $_POST['menuname']; //where the value in menuname is Menu3
if ($nameofmenu2 = $nameofmenu){echo "Both variables are the same";}
Thank you so much
You overwrite the same input field (=name) over and over again, so only the "last" overwrite is returned/available. To avoid this, put the data as POST-ARRAYs like this (also switch your quotes):
echo '<input type="hidden" name="menuname[]" value="'.$nameofmenu.'" />';
After submitting, you now will see that $_POST['menuname'] is an array! e.g.
var_dump($_POST['menuname']);
As mentioned by David, your are assigning nameofmenu2 to nameofmenu. But furthermore, when you echoed:
echo "<input type='hidden' name='menuname' value=' $nameofmenu ' />";
you included whitespace on either side of $nameofmenu. This could cause problems when you go to make your query. Try using:
var_dump($_POST['menuname']);
to check your expected post data.
I have the following button echo'd:
<input type='submit' name='delete_$id' value='x' title='Delete message' />
$id is gathered from here:
$get_messages = mysqli_query ($connect, "SELECT * FROM private_messages WHERE message_to = '$username' OR message_from='$username' AND
DELETED ='no' ORDER BY id DESC");
while ($get_msg = mysqli_fetch_assoc($get_messages)){
$id = $get_msg['id'];
}
I want to get the id of the message the x is assigned to and then execute this:
if (#$_POST['delete_' . $id . '']){
echo "Deleted";
}
But the echo is not displaying on my page, meaning the code is faulty. I have checked numerous times, and cannot figure out why it isn't working?
Edit:
I am simply trying to add a delete button which on click will SET the deleted column in my table to yes.
Here is the complete code where the delete button is found:
<?php
echo "
<div class='parent'>
<div class='msg_prof'>
<img class='img-rounded' src='/user_data/profile_pics/$my_pro_pic'/>
</div>
<div class='new_msg_from_user'>
<p><b style= 'color: red;'> You said:</b> $msg_body</p>
<span class='faded'>$date </span>
<input type='submit' name='delete_$id' value='x' title='Delete message' />
</div><hr/>
</div>";
?>
My thinking behind this is that, I can get the id of a post using the $id variable, and assign that to the name. Then by using if (#$_POST['delete_' . $id . '']) I can perform the query based on the $id - for now I am trying to echo a message, just for testing purposes. This is all the code I have for this functionality.
you forgot the php tag.
<input type='submit' name='delete_<?php echo $id ?>' value='x' title='Delete message' />
First of all, look what you are displaying the line:
<input type='submit' name='delete_$id' value='x' title='Delete message' />
e.g. name should/must be displayed as name='delete_1' or name='delete_9' or any other string having a number/digit at the end of its name. Generically said: name='delete_anyNumber'
For that you should have the Fetched Records of messages(those which you want to displayed and have a cross button just for a good presentation of NORMS) in PHP File and iterate through each result and in iteration-loop, echo all the messages with X buttons like:
echo "<input type='submit' name='delete_" . $ResultVariableHoldingRows['id'] . "' value='x' title='Delete message' />";
THE REASON BEING: (You'll have to display it correctly so that when you POST the form, you must be able to get the exact POSTED value that needs to be DELETED, so that you don't have to iterate to check which row needs to be DELETED.)
After you get the above problem resolved, follow below instructions:
Access the value, in PHP file that is called after Submitting:
$_POST['\'delete_' . $id . '\'']
SUGGESTION: Use AJAX-Calls to DELETE the respective messages as the many forms on a single page will not be a good an approach and do some good search on AJAX before posting a question
As in the comment mentioned you have to iterate through the global POST array.
<?php
function getRecordIdFromKey($sKey)
{
$sId = str_replace("delete_", "", $sKey);
$iReturn = -1;
if (strlen($sId) > 0 && is_numeric($sId))
{
$iReturn = (int) $sId;
}
return $iReturn;
}
foreach ($_POST as $sKey => $mValue)
{
$iId = getRecordIdFromKey($sKey);
if ($iId >= 0)
{
// fire up your delete query
}
}
I have made a connection to mysql database and echoing values from a table.
while($data = mysql_fetch_array($res))
{
?>
<a href="nextpage.php"<?php echo $data['rowname'] ?></a>
<?php
}
?>
Problem is when I click on the particular link, on the nextpage.php, it should display only the result of the value of a href clicked. So on the nextpage.php, I have defined something like SELECT * from tablename where rowname = 'a href value'.
What's happening now is that it displays only the last rowname value regardless of whichever link I click on, very obvious!
I have tried forms, arrays, SESSIONS but to no avail. How do I fix this?
the href should be like this
<?php echo $data['rowname']; ?>
and then on next page you can use $_GET['val'] and pass it to SELECT query
Try example as below
page1.php
while($data = mysql_fetch_array($res))
{
echo "<a href='nextpage.php?id=".$data['rowname']." >". $data['rowname'] ."</a>";
}
?>
your href will look like nextpage.php?id=[your value]
nextpage.php
$qry = mysql_query("select * from [table_name] where id = ".$_GET['id']."",$con);
while($data = mysql_fetch_array($res))
{
echo $data[0];
}
on nextpage pass value using $_GET['id'] to sql query.
Scenario:
I have a radio button where when the user click the radio button it will redirect to another page....
So here's my code:
<?php
$radio = mysql_query("SELECT fldNetname FROM tbldata WHERE fldMonth = '$get_month' AND fldWeek = '$get_week' GROUP BY fldNetname ORDER BY fldNetname");
while ($row = mysql_fetch_array($radio)){
echo "<div><input type='radio' name='playRadio' class='chk_boxes1' value='" . $row['fldNetname']."' " .($_POST['playRadio'] == $row['fldNetname'] ? 'checked' : '') . " onClick=\'this.form.action='looptime_utilization_process.php'; this.form.submit()\' >";
echo $row['fldNetname'];"</div>";
}
?>
When I try to run it there is no error yet its not directing to the page I want.
IS there a way that the radio button will be also like a button that can be proceed to another form?
Thanks.
I have corrected all your HTML and PHP mistakes:
<?php
$radio = mysql_query("SELECT fldNetname FROM tbldata WHERE fldMonth = '" . mysql_real_escape_string($get_month) . "' AND fldWeek = '" . mysql_real_escape_string($get_week) . "' GROUP BY fldNetname ORDER BY fldNetname");
while ($row = mysql_fetch_array($radio)) {
?>
<div>
<input type="radio" name="playRadio" class="chk_boxes1" value="<?php echo $row['fldNetname']; ?>"<?php echo ($_POST['playRadio'] == $row['fldNetname'] ? 'checked=" checked"' : ''); ?> onclick="this.form.action='looptime_utilization_process.php'; this.form.submit();">
<?php echo $row['fldNetname']; ?>
</div>
<?php
}
What I changed:
added mysql_real_escape_string(); - better security if your variables are not already escaped
change echo string concatening to close / open <?php tags
changed onClick to onclick (all attributes should be written in lower case in most modern standarts)
changed checked to checked="checked"
corrected wrong quotes in onclick attribute
<form action='looptime_utilization_process.php'>
<input type='radio' name='playRadio' class='chk_boxes1' onClick='form.submit();' >
<form>
Use Javascript
if(document.getElementsByID("playradio").value==<ur value>)
{
param="<ur parameters>";
xmlhttp.open("GET", "looptime_utilization_process.php?".param ,false);
xmlhttp.send();
}
Try this
$trgt=$_POST['playRadio'];
$radio = mysql_query("SELECT fldNetname FROM tbldata WHERE fldMonth = '$get_month' AND fldWeek = '$get_week' GROUP BY fldNetname ORDER BY fldNetname");
while ($row = mysql_fetch_array($radio)){
$v=$row['fldNetname'];
echo "<div><input type='radio' name='playRadio' class='chk_boxes1' value='$v'"
.($trgt == $v ? 'checked' : '')
.' onclick="o=document.forms[0];o.action=\'looptime_utilization_process.php\'; o.submit()" >';
echo "$v</div>";
}
You had some issues with the quotes in your onClick section. Also this does not work there in the way you wanted. I replaced it by document.forms[0], assuming that it is the first form on that page you want to submit and of course, also assuming that the radio buttons are actually inside <form> tags! Change this as required ...
And, of course: placing the action=... context in the onclick section of the radio buttons really only makes sense if the original action of the form pointed to something else which needs to be there for other use cases. Otherwise follow Gary Hayes' advice and put the action attribute into the <form>...</form> tags.
I'm currently using php to populate a form with selections from a database. The user chooses options in a select style form and submits this, which updates a summary of the selections below the form before a second submit button is used to complete the interaction.
My issue is that every time a user uses the first submit, the selections that were there previously do not stick. They have to go through the whole form again.
Is there anyway to keep these selections present without resorting to php if statements? There are a ton of options so it would be a pain to use php for each one. Also, form is being submitted via POST.
Sample from form:
<?php
// GRAB DATA
$result = mysql_query("SELECT * FROM special2 WHERE cat = 'COLOR' ORDER BY cat")
or die(mysql_error());
echo "<div id='color'><select id='color' name='product_color'>";
while($row = mysql_fetch_array( $result )) {
$name= $row["name"];
$cat= $row["cat"];
$price= $row["price"];
echo "<option value='";echo $name;echo"'>";echo $name;echo" ($$price)</option>";}
echo "</select>";
echo "<input type='hidden' name='amount_color' value='";echo $price;echo"'></div>";
?>
I tried using this js snippet to repopulate the selections, but it does not seem to work properly...
<script type="text/javascript">document.getElementById('color').value = "<?php echo $_GET['proudct_cpu'];?>";</script>
This does not seem to work. Any suggestions other than php if statements?
Thanks!
edit: This is basically the form set up I'm using, though I've shortened it significantly because the actual implementation is quite long.
// Make a MySQL Connection
<?php mysql_connect("localhost", "kp_dbl", "mastermaster") or die(mysql_error());
mysql_select_db("kp_db") or die(mysql_error());
?>
<br />
<form action="build22.php" method="post">
<input type="hidden" name="data" value="1" />
<br />
<br />
<?php
// GRAB DATA
$result = mysql_query("SELECT * FROM special2 WHERE cat = 'color' ORDER BY cat")
or die(mysql_error());
echo "<div id='color'><select id='color' name='product_color'>";
while($row = mysql_fetch_array( $result )) {
$name= $row["name"];
$cat= $row["cat"];
$price= $row["price"];
echo "<option value='";echo $name;echo"'>";echo $name;echo" ($$price)</option>";}
echo "</select>";
echo "<input type='hidden' name='amount_color' value='";echo $price;echo"'></div>";
?>
<input type="submit" value="Update Configuration">
</form>
The selections from the form above get echoed after submission to provide the user with an update as such:
<div id="config" style="background-color:#FFF; font-size:12px; line-height:22px;">
<h1>Current Configuration:</h1>
<?php echo "<strong>Color:</strong>    ";echo $_POST['product_color']; ?>
</div>
I assume you're storing the user's selections in a separate table. If that's the case, you'll need to add some logic to determine if you should display the form values or what's already been stored.
<?php
// form was not submitted and a config id was passed to the page
if (true === empty($_POST) && true === isset($_GET['config_id']))
{
// make sure to properly sanitize the user-input!
$rs = mysql_query("select * from saved_configuration where config_id={$_GET['config_id']}"); // make sure to properly sanitize the user-input!
$_POST = mysql_fetch_array($rs,MYSQL_ASSOC); // assuming a single row for simplicity. Storing in _POST for easy display later
}
?>
<div id="config" style="background-color:#FFF; font-size:12px; line-height:22px;">
<h1>Current Configuration:</h1>
<?php echo "<strong>Color:</strong>    ";echo $_POST['product_color']; ?>
</div>
So after storing the user's selections in the database, you can redirect them to the page with the new config_id in the URL to load the saved values. If you're not storing the selected values in a table, you can do something similar with cookies/sessions.
echo the variables into the value tag of the form elements. If you post all your code I'm sure I can help you.
UPDATE
ah, so they are dropdown lists that you need to remember what was selected? Apologies, I read your post in a rush yesterday and thought it was a form with text inputs.
I just did a similar thing myself but without trying your code let me see if I can help.
Basically what you need to do is set one value in the dropdown to selected="selected"
When I had to do this I had my dropdown values in an array like so:
$options = array( "stack", "overflow", "some", "random", "words");
// then you will take your GET variable:
$key = array_search($_GET['variablename'], $options);
// so this is saying find the index in the array of the value I just told you
// then you can set the value of the dropdown to this index of the array:
$selectedoption = $options[$key];
This is where it might be confusing as my code is different so if you want to use it you will probably need to restructure a bit
I have a doSelect function to which I pass the following parameters:
// what we are passing is: name of select, size, the array of values to use and the
// value we want to use as the default selected value
doSelect("select_name", 1, $options, $selectedoption, "");
// these are the two functions I have:
// this one just processes each value in the array as a select option which is either
// the selected value or just a 'normal' select value
FUNCTION doOptions($options, $selected)
{
foreach ($options as $option)
{
if ($option == $selected)
echo ("<option title=\"$title\" id=\"$value\" selected>$option</option>\n");
else
echo ("<option title=\"$title\" id=\"$value\">$option</option>\n");
}
}
// this is the function that controls everything - it takes your parameters and calls
// the above function
FUNCTION doSelect($name, $size, $options, $selected, $extra)
{
echo("<select class=\"\" id=\"$name\" name=\"$name\" size=\"$size\" $extra>\n");
doOptions($options, $selected);
echo("</select>\n");
}
I know that's a lot of new code that's been threw at you but if you can get your select values from the db into the array then everything else should fall nicely into place.
The only thing I would add, is at the start where we call doSelect, I would put that in an if statement because you don't want to set something as selected which hasn't been set:
if (isset($_GET['variable']))
{
$key = array_search($_GET['variablename'], $options);
$selectedoption = $options[$key];
doSelect("select_name", 1, $options, $selectedoption, "");
}
else
{
doSelect("select_name", 1, $options, "", "");
}
I hope that helps!