Changing text to image with php variables - php

I am building a slot machine and am trying to create a shop where you can buy images to replace the text. If I click the button it just resets my money to zero and doesn't replace any text with images. How can I fix this without compromising any functionality or adding hundreds of lines of code?
<html
<?php
//shop
if (isset($_POST['shop']))
{
if ($credit = 50 or $credit > 50)
{
$credit = $credit - 50;
strtr($str, array(
'Zero' => "<img src=img/Zero.png />",
'Bar' => "<img src=img/Bar.png />",
'Double Bar' => "<img src=img/DoubleBar.png />",
'Triple Bar' => "<img src=img/TripleBar.png />",
'Cherry' => "<img src=img/Cherry.png />",
'Seven' => "<img src=img/Seven.png />",
'Diamond' => "<img src=img/Diamond.png />",
'Clover' => "<img src=img/Clover.png />",
));
}
else
{
$message = "You don't have enough money!";
}
}
<!-- display -->
<?php if (!$bust) { ?>
<div id="slots">
<?=join(' | ', $slot_result)?>
</div>
<br/>
<br/>
<?=$message?>
<?php } else { ?>
<?=$message?>
<?php } ?>
<br/>
<br/>
Total cash: €<?=$credit?>
<form method='post'>
<input type='submit' name='spin' value='Spin' />
<input type='hidden' name='credit' value='<?php echo $credit; ?>' />
<input type='submit' name='reset' value='Reset' />
<br>
<br>
<input type='submit' name='shop' value='Change Style - €50' />
</form>
</body>
</html>

Related

checkboxes when checked, delete checked messages

How to make that at the push of a button
"<input type ='submit' name='delete' value='Delete messages'>"
deleted messages selected by a checkbox? All messages and checkboxes appear through the counter "for" as messages are received. What im doing wrong? I think i have mistake with foreach and form action. Please, help me
my code is:
<php function display_list($auth_user, $accountid, $messageid, $fullheaders) {
if(!$accountid) {
echo "<p style=\"padding-bottom: 100px\">No mailbox selected.</p>";
} else {
$imap = open_mailbox($auth_user, $accountid);
if($imap) {
$headers = imap_headers($imap);
$messages = count($headers); ?>
<div class="view-mailbox-block">
<div class="new-message">
<form action="index.php?action=new-message" method="post">
<input type ='submit' value='Write a letter'>
</form>
</div>
<form action="" method="post">
<input type ='submit' name='delete' value='Delete messages'>
</form>
</div>
<div class="incoming-sent-messages-mail">
<p>Почта</p>
</div>
<div class="incoming-sent-messages">
<div class="incoming-sent-messages-p">
<p>
<?php echo "<a href='index.php?action=view-mailbox'>"; ?> View </a>
</p>
<p>
<?php echo "<a href='index.php?action=view-mailbox-sent'>"; ?> Sent </a>
</p>
</div>
</div>
<div>
<div class="senders overflow">
<form class="sender-checkbxes">
<?php echo '<input type="checkbox" name="deleteall" value="checkbox-all">'; ?>
<?php for ($i = 0; $i < $messages; $i++) {
echo "<div><div>";
echo '<input type="checkbox" name="delete[]" value="<?php echo $delete ?>">';
echo "</form>";
echo "</div></div>\n";
} ?>
<form action='index.php?action=delete&messageid='<?php echo $check;?>'>
<?php if(!empty($_POST['delete'])) {
foreach($_POST['delete'] as $check) {
imap_delete($imap, $check);
imap_expunge($imap);
imap_close($imap);
return true;
}
} ?>
</form>
</div>

Use a single textbox to keep adding values to array after post

So I'm currently working on a little project that allows a user to input a number into a textbox, after clicking a button that says "add" it should store that value into an array and then allow the user to input another value into that array. There is also a button on the page when the user is finished and wants to sum the values called "Submit". The problem I'm running into is everytime the form posts back, it recreates a new blank array. Any tips?
See the code below:
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
</p>
<?php
$array = array();
if (isset($_POST['submit']))
$num = $_POST['strNumber'];
$array[] = $num;
foreach($array as $num)
echo $num . ' + ';
if(isset($_POST['calculate']))
foreach($array as $num)
echo $num . ' + ';
?>
</form>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
<input type='submit' name='clear' value='clear' />
</p>
<?php
if (isset($_POST['submit'])) {
if(!array_key_exists("numbers", $_SESSION)) {
$_SESSION["numbers"] = array();
}
array_push($_SESSION["numbers"], $_POST["strNumber"]);
}
if(isset($_POST['clear'])) {
$_SESSION["numbers"] = array();
}
if(array_key_exists("numbers", $_SESSION)) {
echo implode("+", $_SESSION["numbers"]);
}
if(isset($_POST['calculate'])) {
if(array_key_exists("numbers", $_SESSION)) {
$expression = implode("+", $_SESSION["numbers"]);
eval( '$result = (' . $expression . ');' );
echo "=" . $result;
}
}
?>
</form>
</body>
</html>
Start a session
When the action is "submit"
Check if the session which will store the numbers is initialized
If it's not initialize it as an array
Finally push the number into the array
Check if there is a session initialized if there is print all the numbers ( you can use implode to do that)
if the action is calculate .. just make the calculation ( check eval function )

my other if else statement is executing php

I have 3 forms with 3 radios each but executed one at a time because of random generator. If I click a radio, it will go to next page and will be checked by the if else statement. Now my problem is, when it gets answered and it's correct, it will output yes. However, my other if else statement of my other radio executes. Since it's not answered, it assumed that it was wrong, so it outputted no.
here's my code:
<!doctype html>
<html>
<head>
<title>Question and Answer</title>
</head>
<body>
<?php
//Creating random numbers
$rid = rand(1,3);
?>
<?php
if ($rid == 1){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does HTML Stands For ?</h3>
<div>
<input type='radio' name='answerOne' id='answerOne' value='A' />
<label for='answerOneA'>A) Hyper text markup language</label>
</div>
<div>
<input type='radio' name='answerOne' id='answerOne' value='B' />
<label for='answerOneB'>B) Hyper turn mark lingo</label>
</div>
<div>
<input type='radio' name='answerOne' id='answerOne' value='C' />
<label for='answerOneC'>C) Happy tissue mahatma life</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose..'>
</form>";
}
if ($rid == 2){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does CSS Stands For ?</h3>
<div>
<input type='radio' name='answer2' id='answer2' value='A' />
<label for='answer2A'>A) College Computer Studies</label>
</div>
<div>
<input type='radio' name='answer2' id='answer2' value='B' />
<label for='answer2B'>B) Cascading Style Sheet</label>
</div>
<div>
<input type='radio' name='answer2' id='answer2' value='C' />
<label for='answer2C'>C) Cascaded Style Sheet</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose..'>
</form>";
}
if ($rid == 3){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does PHP Stands For ?</h3>
<div>
<input type='radio' name='answer3' id='answer3' value='A' />
<label for='answerOneA'>A) Hyper text markup language</label>
</div>
<div>
<input type='radio' name='answer3' id='answer3' value='B' />
<label for='answerOneB'>B) Hyper turn mark lingo</label>
</div>
<div>
<input type='radio' name='answer3' id='answer3' value='C' />
<label for='answerOneC'>C) Happy tissue mahatma life</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose'>
</form>";
}
?>
</body>
</html>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<!doctype html>
<html>
<head>
<title>Question and Answer</title>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
$answer = array('A','B','C');
$answer1= $_POST['answerOne'];
$answer2= $_POST['answer2'];
if($answer1 == $answer[0]){
echo 'yes';
}
else if ($answer1 != $answer[0]){
echo 'no';
}
else{
}
if($answer2 == $answer[1]){
echo 'yes';
}
else if ($answer2 != $answer[1]){
echo 'no';
}
?>
</body>
</html>
Here is a cleaner way to do it. Inside is_correct function, it check if particular answer exist in $_POST, if so it returns if the answer is correct:
function is_correct()
{
$answer = array(
'answerOne' => 'A',
'answer2' => 'B',
'answer3' => 'C',
);
foreach ($answer as $k => $v) {
if (array_key_exists($k, $_POST)) {
return $v == $_POST[$k];
}
}
return false;
}
if (is_correct()) {
echo "yes";
} else {
echo "no";
}

Delete button to delete database table row

I want to give the users of my website the ability to add, edit and remove questions of a FAQ that's stored on a database. They select the title from a form and then they can press 'select' (to edit the q&a) or 'delete'. Deleting doesn't work.
The first problem is: when they click delete, it leads them to the 'edit' form instead of deleting the database table row.
The second problem: when I make them click the button, I ask to confirm the delete. Whether they press 'Ok' or 'Cancel' doesn't matter, they are always lead to the 'edit' form.
The code in the admin panel (index.php):
if(isset($_POST['actie']) && $_POST['actie'] != "")
{
$actie = $_POST['actie'];
if($actie == "csd_faq") { verwerk_csd_faq($DB); }
if($actie == "csd_faq_edit") { verwerk_edit_csd_faq($DB); }
}
if($GLOBALS['logged_in'] && isset($_GET['actie']) && $_GET['actie'] != "")
{
$actie = $_GET['actie'];
if($actie == "csd_faq") { toon_csd_faq($DB); }
if($actie == "csd_faq_edit") { edit_csd_faq($DB); }
}
The code for the functions:
//ADD FAQ
function toon_csd_faq($DB)
{?>
<h2>Een FAQ toevoegen:</h2>
Gebruik het formulier hieronder om een FAQ toe te voegen.
<br /><br />
<form action='index.php?actie=csd_faq' method='post'>
<input type='hidden' name='actie' value='csd_faq' />
<input type='hidden' name='naam' value='<? echo $GLOBALS['nickname']; ?>' />
Vraag (Q): <br />
<input type='text' name='q' size='80' /><br /><br />
Antwoord (A): <br />
<textarea name='a' cols='80' rows='10'></textarea><br /><br />
<input type='submit' value='Toevoegen' />
</form>
<?}
function verwerk_csd_faq($DB)
{
$naam = $_POST['naam'];
$q = clean($_POST['q']);
$a = clean($_POST['a']);
$DB->q("INSERT INTO `csd_faq` (`datum`, `naam`, `q`, `a`) VALUES (NOW(), '$naam', '$q', '$a')");
echo "<b class='roze'>FAQ succesvol toegevoegd!</b>"
."<br /><br />";
}
// EDIT FAQ
function edit_csd_faq($DB)
{
echo "<h2>Een FAQ bewerken:</h2>";
if(isset($_GET['faq_id'])) //bewerken
{
$faq_id = $_GET['faq_id'];
$faq = $DB->q1("SELECT * FROM `csd_faq` WHERE `faq_id` = '$faq_id'");
?>
Gebruik het formulier hieronder om de FAQ te bewerken
<br /><br />
<form action='index.php?actie=csd_faq_edit' method='post'>
<input type='hidden' name='actie' value='csd_faq_edit' />
<input type='hidden' name='faq_id' value='<? echo $faq_id; ?>' />
<input type='hidden' name='naam' value='<? echo $GLOBALS['nickname']; ?>' />
Vraag (Q): <br />
<input type='text' name='q' size='80' value='<? echo $faq[3]; ?>' /><br /><br />
Antwoord (A): <br />
<textarea name='a' cols='80' rows='10'><? echo $faq[4]; ?></textarea><br /><br />
<input type='submit' name='update' value='Update' />
</form>
<br />
« <a href='javascript:history.go(-1)'>Kies een andere FAQ</a>
<?}
else { //selectie box tonen ?>
Selecteer de te bewerken FAQ.
<br /><br />
<form action='index.php' method='get'>
<input type='hidden' name='actie' value='csd_faq_edit' />
Titel: <br />
<select name='faq_id' size='10' style='width:500px;'>
<?
$result = $DB->q("SELECT * FROM `csd_faq` ORDER BY `datum` DESC");
$lijst = "";
while($faq = $DB->fa($result))
{
$lijst .= "<option value='".$faq[0]."'>".$faq[3]."</option>";
}
echo $lijst;
?>
</select>
<br /><br />
<input type='submit' name='selecteer' value='Selecteer' />
<input type='submit' name='delete' value='Verwijder' onClick='confirm("Zeker dat je deze FAQ wilt verwijderen?")' />
</form>
<?}
}
function verwerk_edit_csd_faq($DB)
{
if(isset($_POST['update'])) {
$faq_id = $_POST['faq_id'];
$naam = $_POST['naam'];
$q = clean($_POST['q']);
$a = clean($_POST['a']);
$DB->q("UPDATE `csd_faq` SET `datum` = NOW(), `naam` = '$naam', `q` = '$q', `a` = '$a' WHERE `faq_id` = '$faq_id'");
echo "<b class='roze'>FAQ succesvol bewerkt!</b>"
."<br /><br />";
}
else if(isset($_GET['delete'])) {
$faq_id = $_GET['faq_id'];
$DB->q("DELETE FROM `csd_faq` WHERE `faq_id` = '$faq_id'");
echo "<b class='roze'>FAQ succesvol verwijderd!</b>"
."<br /><br />";
}
}
You probably want to check for $_POST['delete'] and $_POST['faq_id'], not $_GET['delete'] and $_GET['faq_id'] as the method of the form is POST.

how to pre-load an existing form with a data from db?

i have a problem here,so here's my code
<div id="educmaininfo">
<?php
foreach($cv->getEducation($_GET['cvid']) as $r){
echo "<a href='#' id='editeducation'>Edit</a> | ";
echo "<input type='hidden' name='cvid' value='".$r['ResumeID']."' />";
echo "<input type='hidden' name='educid' value='".$r['EducationID']."'/>";
echo "<a href='#' id='deleteeducation'>Delete</a><br />";
echo "Date From = ".$r['DateFrom']."<br />";
echo "Date To = ".$r['DateTo']."<br />";
echo "Title =".$r['Title']."<br />";
echo "Summary =".$r['Summary']."<br />";
echo "Place Organization =".$r['PlaceOrganization']."<br />";
echo "Emphasis of Study =".$r['EmphasisOfStudy']."<br />";
echo "Study Details =".$r['StudyDetails']."<br />";
echo "Qualifications =".$r['Qualifications']."<br /><br />";
}
?>
</div>
as you can see that code above, I added 2 hidden stuff, the cvid and the educid.
my question is, how to load this form below
<div id="educajaxinfo" style="display:none">
<table>
<form id="educdetails" method="post" action="">
<input type="hidden" name="cvid" id="cvid" value="<?php echo $v['ResumeID']; ?>" />
<tr><td>Date From:</td><td><input type="text" name="datefromeduc" id="datefromeduc" value="" /></td></tr>
<tr><td>Date To:</td><td><input type="text" name="datetoeduc" id="datetoeduc" value="" /></td></tr>
<tr><td>Title:</td><td><input type="text" name="titleeduc" id="titleeduc" value="" /></td></tr>
<tr><td>Summary:</td><td><textarea name="summaryeduc" id="summaryeduc" rows="10" cols="50"></textarea></td></tr>
<tr><td>Place Organization:</td><td><input type="text" name="pog" id="pog" value="" /></td></tr>
<tr><td>Emphasis of study:</td><td><input type="text" name="eos" id="eos" value=""></td></tr>
<tr><td>Study Details:</td><td><textarea name="studyeduc" id="studyeduc" rows="10" cols="50" ></textarea></td></tr>
<tr><td>Qualifications:</td><td><textarea name="qualificationseduc" id="qualificationseduc" rows="10" cols="50"></textarea></td></tr>
<tr><td><input type="submit" name="update" value="<?php if($count < 3){ echo 'Add';}else{ echo 'Update';}?>" /></td></tr>
</form>
</table>
</div>
with the existing data from the db table, that matches the cvid and educid ?
the flow goes like this, if user clicks the edit link, it should redirect him to the
form with the values loaded in the input forms...if in pure php i can do this by just
doing something like
e.g edit.php?cvid=cvid&educid=educid
how to do it in ajax way?
Use jQuery to call a PHP script that returns JSON encoded data. Then use the “success” callback function to inject the data into your form.
Use the jquery function like following
a href='#' onClick=test
(echo $cvid ,echo $educid ) id='editeducation'>Edit
function test(cvid,educid)
{
jQuery.post('edit.php?cvid=cvid&educid=educid',function(response){
jQuery('#educajaxinfo').html(response);
});
}
include the appropriate jquery min file to use above function.
on edit.php get the two id's and prepare a whole html then it will be shown in #educajaxinfo div

Categories