I have this problem:
on the following code i made a form with a condition, where if the "profileid" is in array friends then print the button "add to friends" else "remove to friends" but the second condition don't work it don't prints anything and when i load the page for the first time, there is ever the button "add to friends" either if there's already the "friend id" in the array.
Here is my code:
<?php
$userid = $_SESSION['userid'];
$profileid = $_SESSION['profileID'];
$compressed_friends=mysql_query("SELECT friends FROM users WHERE id LIKE '$userid'");
$friends = explode (',',$compressed_friends);
if(isset($_POST['addFriends']))
{
$compressed_friends=$profileid.','.$compressed_friends;
mysql_query("UPDATE users SET friends='$compressed_friends' WHERE id='$userid'");
}
elseif(isset($_POST['removeFriends']))
{
array_filter($friends,$profileid);
$compressed_friends=implode(',', $friends);
mysql_query("UPDATE users SET friends='$compressed_friends' WHERE id='$userid'");
}
else
{
?>
<form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<?php
if(!in_array($profileid, $friends))
{
echo' <button type="submit" name="addFriends" class="btn btn-primary col-lg-3">Add to friends</button>';
}
elseif(in_array($profileid, $friends))
{
echo '<button type="submit" name="removeFriends" class="btn btn-danger col-lg-3">Remove to Friends</button>';
}
?>
</form>
<?php } ?>
Let me try to answer as I am a beginner in PHP.
I found there is the problem on echo. You used ' to cover the ".
echo' <button type="submit" name="addFriends" class="btn btn-primary col-lg-3">Add to friends</button>';
You can try this out, perhaps it will solve your problem.
echo "<button type='submit' name='addFriends' class='btn btn-primary col-lg-3'>Add to friends</button>";
Thanks!
Related
I have a form which can submited.and have list,that listed all submited form details.
I tried it in different ways.I want to fill the form with the corresponding details when I clicked the edit button.
Here is my php file
<div class="row">
<div class="col-sm-9">
<b>Leader Name : </b><?php echo($row["lead_name"]); ?><br>
<b>Phone Number : </b><?php echo($row["phone_number"]); ?><br>
<b>Email : </b><?php echo($row["email"]); ?><br>
<b>Part Created Time : </b><?php echo($row["create_date_and_time"]); ?>
<br>
</div>
<div class="col-sm-3 ">
<form role="form" action='index.php' method='POST'>
<input type='hidden' name='party_id' value='<?php echo($row["party_id"]); ?> '>
<input type="submit" class="btn btn-sm btn-success btn-block" id="edit" name="edit" value="Edit" style="font-size: 12px; padding: 3px;">
<input type="submit" class="btn btn-sm btn-danger btn-block" id="delete" name="delete" value="Delete" style="font-size: 12px; padding: 3px;">
</form>
<?php
if (isset($_POST['delete'])) {
print("<script> alert('delete'); </script>");
$party_id = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$queryDelete = "DELETE FROM party_details WHERE party_id='$party_id'";
if ($conn->query($queryDelete)) {
$_SESSION['party'] = "";
$_SESSION['trips'] = [];
print("<script>
alert('Party removed');
window.location.href='../tripCreate';
</script>");
} else {
print("<script>alert('Error when remove ! ');</script>");
}
$_POST = array();
}
if (isset($_POST['edit'])) {
$party_id1 = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$query1 = "SELECT * FROM party_details WHERE party_id='$party_id1'";
$result1 = $conn->query($query1);
$row1 = $result1->fetch_assoc();
}
?>
</div>
first of all, you should specify not only result you want to achieve, but also what kind of problem you are facing.
is it php error, or information not being displayed in resulted page?
one thing i spotted is that you got $row1 = $result1->fetch_assoc(); but in form you echo $row[] (instead of $row1[]), which i dont see being created anywhere.
also, did you try var_dump($row) in php and check its content (or $row1...)?
Situation
I have 3 buttons that are designed to be able to switch between different company's.
I have put those buttons in a form:
echo '<form action="?" method="post">';
foreach ($_SESSION['bedrijf'] as $value) {
echo '<button type="submit" class="btn btn-default" name="bedrijf_keuze[]" value="bedrijf_keuze_'.$value.'"><img src="images/logo_'.$value.'_small.png" height="40"></button> ';
}
echo'</form>';
Thru POST I am using this as an session variable into the rest of the system:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST["bedrijf_keuze"] as $key=>$value)
{
$bedrijf_keuze = trim(mysqli_real_escape_string($mysqli, $value));
$_SESSION['bedrijf_keuze'] = $bedrijf_keuze;
}
}
Now when an button is clicked the form is send and when I echo I see the correct value of $_SESSION['bedrijf_keuze']
To be able to see which company is chosen I have replaced class="btn btn-default" with if($_SESSION['bedrijf_keuze'] == "bedrijf_keuze_'.$value.'") { echo 'class="btn btn-default active"'; } else { echo 'class="btn btn-default"'; }
Problem
The button that is clicked and where the session value is set for is not shown as active.
The final code of the form is now:
echo '<form action="?" method="post">';
foreach ($_SESSION['bedrijf'] as $value)
{
echo '<button type="submit" ';
if($_SESSION['bedrijf_keuze'] == "bedrijf_keuze_'.$value.'") {
echo 'class="btn btn-default active"'; }
else {
echo 'class="btn btn-default"';
}
echo ' name="bedrijf_keuze[]" value="bedrijf_keuze_'.$value.'"><img src="images/logo_'.$value.'_small.png" height="40"></button> ';
}
echo'</form>';
When I echo $_SESSION['bedrijf_keuze'] and bedrijf_keuze_'.$value.' the values are corresponding. So what went wrong and how to solve this?
The problem you are facing is that you are comparing values that never can correspond.
"bedrijf_keuze_'.$value.'" != '"bedrijf_keuze_'.$value.'"'
Code
<?php
$value = 'foo';
echo "bedrijf_keuze_'.$value.'";
echo '"bedrijf_keuze_'.$value.'"';
Output
bedrijf_keuze_'.foo.'
"bedrijf_keuze_foo"
Actually i want to develop a application from where user can set attendance for student. so the html form for attendance will come from my db query. and it's coming too but the prob is that how can i insert that form information to my db . actually i searched lot but i didn't get any result for this as perfect as i want i mean please can anyone help me . thanks in advance
<form action="attendance.php" method="post">
<?php include '../database-config.php';
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row){
echo "<div>
<label>".htmlentities($row['student_id'])."</label>
<input type='radio' name='atten".htmlentities($row['student_id'])."' checked='checked'>Present
<input type='radio' name='atten".htmlentities($row['student_id'])."'>Absent
</div></br>";
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
<form action="attendance.php" method="post">
<?php include '../database-config.php';
$result = mysql_query("SELECT * FROM student WHERE active_class='VII'");
foreach($result as $row)
{
?>
<div>
<label><?php echo $row['student_id']?></label>
<input type="radio" name="attend" value="present" checked>Present
<input type="radio" name="attend" value="absent">Absent
</div>
</br>
<?php
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
so in php you can get value like this
<?php
$attend = $_POST['attend'];
echo $attend;
?>
So in $attend it contain value(value="present") of radio button.
it may be present or either absent
damn getting tired xD
this should work though but you have to add the column attendency to the database table by yourself cheers
<form action="" method="post">
<?php
include '../database-config.php';
if(isset($_POST['attendency']) && isset($_POST['id']))
{
$id_to_update = $_POST['id'];
$status = $_POST['attendency'];
$ar = array('p','a');
$attend = !empty($status) && in_array($status,$ar) ? $status : 'p';
//you have to create a column named attendency for this to work
$sql = "INSERT INTO student(attendency) VALUES ('$attend ') WHERE user_id = '$id_to_update '";
$dbh->query($sql);
}
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row)
{
if($row['attendency'] == 'p')
{
$p = 'checked="checked"';
$a = '';
} else {
$a = 'checked="checked"'
$p = '';
} ?>
<div>
<input type="hidden" name="id" value="<?=$row['student_id']?>">
<label><?=$row['student_id']?></label>
<input type='radio' name='attendency' <?=$p?>>Present
<input type='radio' name='attendency' <?=$a?>>Absent
</div></br>
<?php } ?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
I am trying to make a very basic quiz in PHP and mySQL.
Essentially, I have a user "Post quiz" page that will allow the user to post 3 questions with 3 options and one solution. If the user presses a radio button that has the value equal to the answer of the question (which was made in the "post quiz" page) then it will throw back text saying they got it right, other wise it will say it is wrong.
My Problem:
I have gotten everything to work bar one thing: When I click one of the radio buttons in the form, they all seem to post and throw back the same condition.
Maybe my code is just fundamentally wrong and I don't see it, but I have been at this for a week, can someone find the error for me?
Quiz Page:
Code saying what each button is equal to,
$_POST['q2o1'] = $tresult['q2_opt1'];
$_POST['q2o2'] = $tresult['q2_opt2'];
$_POST['q2o3'] = $tresult['q2_opt3'];
$q2answer = $tresult['q2answer'];
$question2 = $tresult['question2'];
$_POST['q3o1'] = $tresult['q3_opt1'];
$_POST['q3o2'] = $tresult['q3_opt2'];
$_POST['q3o3'] = $tresult['q3_opt3'];
$q1answer = $tresult['q1answer'];
$question1 = $tresult['question1'];
$_POST['q1o1'] = $tresult['q1_opt1'];
$_POST['q1o2'] = $tresult['q1_opt2'];
$_POST['q1o3'] = $tresult['q1_opt3'];
$q3answer = $tresult['q3answer'];
$question3 = $tresult['question3'];
$q1_opt1 = $_POST['q1o1'];
$q1_opt2 = $_POST['q1o2'];
$q1_opt3 = $_POST['q1o3'];
$q2_opt1 = $_POST['q2o1'];
$q2_opt2 = $_POST['q2o2'];
$q2_opt3 = $_POST['q2o3'];
$q3_opt1 = $_POST['q3o1'];
$q3_opt2 = $_POST['q3o2'];
$q3_opt3 = $_POST['q3o3'];
HTML code for the radio buttons and question layout:
<?php echo '<form action="quiz.php?id='.$id.'" method ="post">';?>
<h3 id="question2"><u>Question 1: </u></h3><p> <i><?php echo $question1;?></i></p>
<br>
<p>A: <?php echo $q1_opt1;?></p>
<p>B: <?php echo $q1_opt2;?></p>
<p>C: <?php echo $q1_opt3;?></p>
<br>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="q1o1" value =<?php echo "".$q1o1."";?> id="option1" autocomplete="off"> A
</label>
<label class="btn btn-primary">
<input type="radio" name="q1o2" value =<?php echo "".$q1o2."";?>id="option2" autocomplete="off"> B
</label>
<label class="btn btn-primary">
<input type="radio" name="q1o3" value =<?php echo "".$q1o3."";?> id="option3" autocomplete="off"> C
</label>
</div>
<button type="submit" name ="submitquiz" class="btn btn-success">Submit!
<span class="glyphicon glyphicon-ok"></span></button>
PHP saying if the POST is submitted for each button:
<?php
if(isset($_POST['submitquiz'])){
if(isset($_POST['q1o1']) && $_POST['q1o1'] == $q1answer ){
$c1= '<p><font color="#62FD01">That is the correct answer!</font></p>';
} else{
$w1= '<p> <font color="red">That is the wrong answer!</font></p>';
}if(isset($_POST['q1o2']) && $_POST['q1o2'] == $q1answer ){
$c1= '<p><font color="#62FD01">That is the correct answer!</font></p>';
} else{
$w1= '<p> <font color="red">That is the wrong answer!</font></p>';
}
if(isset($_POST['q1o3']) && $_POST['q1o3'] == $q1answer ){
$c1= '<p><font color="#62FD01">That is the correct answer!</font></p>';
} else{
$w1= '<p> <font color="red">That is the wrong answer!</font></p>';
}
}
else {
echo '<p><font color="orange">You did not answer this question</font></p>';
}
?>
<?php
if(isset($w1)){
echo $w1;
}else{
echo $c1;
}
?>
I have this php code which hide the download button after clicking on it one time by changing the ID from 0 to 1 .. after that if another time the user signed in , it removes the button using a simple css hide code.
here is my code :
<?php
$result = #mysql_query("SELECT * FROM scode WHERE updated= 1 and coden ='$username'");
if ($_POST[downloadTheFile]== "downloadTheFile")
{
$upd_art = "update scode set downloaded='".$_POST[t11] ."' where id='$_SESSION[userid]'";
mysql_query($upd_art) or die(mysql_error());
}
if($row['downloaded']==1)
{
echo "<style>
.thedownloadbutton {display:none;}
</style>";
}
?>
<form class="thedownloadbutton" method="get" action="<? echo '../download/'.$item_downloadlink .'.zip' ; ?>">
<button type="submit" name="downloadTheFile" value="downloadTheFile">Download </button>
<input name="t11" type="hidden" size="2" value="1">
</form>
(just for clarifying: updated =1 is a field that'll open the download page.. if the updated =1 then there is a download page )
I don't know why it doesn't work ..
can you help me please and till me which part is the wrong part ?
I know it's a bad way to hide an element using css , is there another suggestion ?
$_POST[downloadTheFile] should be $_POST['downloadTheFile']
if($row['downloaded']!=1)
{
echo '<button type="submit" name="downloadTheFile" value="downloadTheFile">Download </button>';
}
so there were some problems in this code .. it wasn't professional at all so I used this new code which has a button that update the number from 0 to 1 on the database
(that will tell the download button to not show after that)
then shows the download button
<?
$res = mysql_query("SELECT downloaded FROM scode WHERE id='$_SESSION[userid]'");
$row = mysql_fetch_array($res);
// echo $row['downloaded'];
echo '<br>';
?>
<form method="POST" action=''>
<?
if($row['downloaded'] ==='0')
{
echo "<input type='submit' name='button1' value='click to show the download link' onclick ='validatea(); return false;' />";
//echo '<button type="submit" formmethod="post" name="downloadTheFile" value="downloadTheFile">Download </button>';
?>
</form>
<?
if (isset($_POST['button1']))
{
?> <form class="aaa" method="POST" action="<? echo "../download/".$item_downloadlink .".zip" ; ?>">
<? ;
?> <input type='submit' name='submit' value='download' onclick ='validate(); return false;' /> <? ;
echo"</form>";
$upd_art = "update scode set downloaded='1' where id='$_SESSION[userid]'";
mysql_query($upd_art) or die(mysql_error());
}
}
else {
echo "you already downloaded the file , if you have any problem please contact us";
echo"<br>";
}
?>