I have this code, I have removed some code that has nothing to do with my problem.
while ($row_pl = mysql_fetch_array($res_pl))
{
// Uurtarief
$sql_uur = "SELECT
aantal_kop,
tarief_min,
tarief_max,
tijd_min,
tijd_max,
tijd_extra
FROM uurtarief
WHERE machine = '".$row_pl['machine']."'
ORDER BY aantal_kop ASC";
if(!$res_uur = mysql_query($sql_uur))
{
include('includes/errors/database_error.php');
}
else
{
while ($row_uur = mysql_fetch_array($res_uur))
{
?>
<input type="text" name="tar_kop[]" size="8" value="<?php echo $row_uur['aantal_kop']; ?>" />
<input type="text" name="tar_tarief[]" size="8" value="<?php echo $uurtarief; ?>" />
<input type="text" name="tar_tijd_extra[]" size="8" value="<?php echo $row_uur['tijd_extra']; ?>" />
<br />
<?php
}
}
?>
<tr>
<td>
<input type="text" name="pl_aantal_kop[]" size="3" onChange="uur_tarief(this, <?php echo $i ?>)" value="" />
</td>
<td>
<input type="text" name="pl_tarief_ph[]" size="4" value="" />
</td>
</tr>
<?php
}
With javascript I'm trying to find the value in pl_aantal_kop[] and send the corresponding parameter to pl_tarief_ph[]
function uur_tarief(selectVeld, nr)
{
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 1)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[0].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[0].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 2)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[1].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[1].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 3)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[2].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[2].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 4)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[3].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[3].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 5)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[4].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[4].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 6)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[5].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[5].value;
}
}
The problem that occurred is that the javascript keeps starting counting at the first line of the while ($row_uur = mysql_fetch_array($res_uur)) loop instead of the field that belongs to the first loop. Below an screenshot of this loop.
Any suggestions?
You can condense your JS to this:
function uur_tarief(selectVeld, nr)
{
var index = document.getElementsByName('pl_aantal_kop[]')[nr].value;
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[index - 1].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[index - 1].value;
}
Can you try this code and post back? Where are you initializing and calling the JavaScript code? The code you posted looks fine (although bloated severely), so it might be an issue elsewhere.
Related
EDIT: When I print the $answer variable by itself it always returns the correct answer of the current question.
I am currently coding a PHP script that produces a simple, but completely random math quiz. How it works is that is gets two random numbers between 0-9 and a random operator from '-', '+', '*'. The user must enter into the text box the answer of the shown question. From here it's pretty straightforward to understand.
However, the issue I am having is that no matter what the user enters the only questions that are validated as correct are ones where the answer is 0.
Here is my code so far.
<?php
require 'functions.php';
$body = "";
$score = 0;
$count = 0;
if(isset($_POST['submit']))
{
$firstDigit = $_POST['lho'];
$secondDigit = $_POST['rho'];
$operator = $_POST['op'];
$userAnswer = $_POST['answer'];
$count = $_POST['count'];
$score = $_POST['score'];
$answer = evaluate($firstDigit, $secondDigit, $operator);
if($answer == $userAnswer)
{
$count++;
$score++;
$body .= "\n<h1>Congratulations!</h1>\n\n";
$body .= "$score out of $count";
}
else
{
$count++;
$body .= "\n<h1>Sorry!</h1>\n\n";
$body .= "$score out of $count";
}
}
header( 'Content-Type: text/html; charset=utf-8');
print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
?>
<?php
include("./header.php");
?>
<h1>Math Quiz</h1> <br /> <br />
<?php
print $body;
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h3><?php echo $firstDigit; ?> <?php echo $operator; ?> <?php echo $secondDigit; ?> = ?
<input type="text" name="answer" size="2" /></h3>
<p><input type="submit" name="submit" value="Try It!" />
<input type="hidden" name="lho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="rho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="op" value="<?php echo randop(); ?>" />
<input type="hidden" name="score" value="<?php echo $score++; ?>" />
<input type="hidden" name="count" value="<?php echo $count++; ?>" /></p>
</form>
<?php
include("./footer.php");
?>
My evaluate function is this:
function evaluate($d1, $d2, $op) {
switch($op) {
case '+' : // addition
$result = $d1 + $d2;
break;
case '-' : // subtraction
$result = $d1 - $d2;
break;
case '*' : // multiplication
$result = $d1 * $d2;
break;
default : // Unidentified, return safe value
$result = 0;
}
return $result;
}
Here is the randop() function and the randdigit() function:
/* Return a number in the range 0-9 inclusive
*/
function randdigit() {
return mt_rand(0,9);
} // end functionranddigit()
function randop(){
$ops = array('+', '-', '*');
// pick a random index between zero and highest index in array.
$randnum = mt_rand(0,sizeof($ops)-1);
return $ops[$randnum]; // Use the index to pick the operator
}
First on the 48th line :
<h3><?php echo $firstDigit; ?> <?php echo $operator; ?> <?php echo $secondDigit; ?> = ?
When you first load the form and until it has been submitted once,
$firstDigit, $operator, $secondDigit
Aren't set.
Then, this line wich is the equation to solve is filled with the old equation that needed to be solved AND your hidden fields are filled with new numbers, invisible to the user using randdigit() and randop().
<input type="hidden" name="lho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="rho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="op" value="<?php echo randop(); ?>" />
Here is the code that works well for me :
<?php
require 'functions.php';
$body = "";
$score = 0;
$count = 0;
$newFdigit = randdigit();
$newSdigit = randdigit();
$newOperator = randop();
if(isset($_POST['submit']))
{
$firstDigit = $_POST['lho'];
$secondDigit = $_POST['rho'];
$operator = $_POST['op'];
$userAnswer = $_POST['answer'];
$count = $_POST['count'];
$score = $_POST['score'];
$answer = evaluate($firstDigit, $secondDigit, $operator);
if($answer == $userAnswer)
{
$count++;
$score++;
$body .= "\n<h1>Congratulations!</h1>\n\n";
$body .= "$score out of $count";
}
else
{
$count++;
$body .= "\n<h1>Sorry!</h1>\n\n";
$body .= "$score out of $count";
}
}
header( 'Content-Type: text/html; charset=utf-8');
print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
?>
<h1>Math Quiz</h1> <br /> <br />
<?php
print $body;
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h3><?php echo $newFdigit; ?> <?php echo $newOperator; ?> <?php echo $newSdigit; ?> = ?
<input type="text" name="answer" size="2" /></h3>
<p><input type="submit" name="submit" value="Try It!" />
<input type="hidden" name="lho" value="<?php echo $newFdigit ?>" />
<input type="hidden" name="rho" value="<?php echo $newSdigit; ?>" />
<input type="hidden" name="op" value="<?php echo $newOperator; ?>" />
<input type="hidden" name="score" value="<?php echo $score++; ?>" />
<input type="hidden" name="count" value="<?php echo $count++; ?>" /></p>
</form>
I want to print a text field 6 times, however if all of them are filled then the loop should not continue. Here's my code:
<fieldset class="keywords">
<?php
$fkeywords = get_the_terms($pid, 'fkeywords');
if (is_array($fkeywords)) {
foreach ($fkeywords as $keyword) {
echo '<input type="text" name="fkeywords[]" id="'.$keyword->slug.'" value="'.$keyword->slug.'">';
}
}
?>
<ol>
<?php for ($i=0; $i<6; $i++ ){ ?>
<li><input type="text" size="20" name="foodir_keywords[]" /></li>
<?php } ?>
</ol>
</fieldset>
For HTML:
<input type="text" size="20" name="keywords[]" id="valueCheck" />
You can use this function for checking purpose:
function checkInput() {
var valueCheck = document.getElementById('valueCheck').value;
if(!valueCheck.match(/\S/)) {
alert ('EMPTY');
return false;
} else {
alert("Filled");
return true;
}
}
I somehow figured it out myself:
$keyCount = count($fkeywords);
for ($i=0; $i<6-$keyCount; $i++)
I have several radio buttons which can be chosen and then the form can be sent without an issue. However a user can easily select the "Other" radio button and submit without giving any context or reason on why they chose it leaving the people receiving the "completed" form guessing what the issue is.
What I want to happen is that if a person selects the "Other" radio button and try and submit without a note/message/comment they will be interrupted with a requirement massage.
The snippet of the form that I want this to happen to is:
<label>
<input name="category" type="radio" value="Other" checked>Other
</label><br><br><br>
Note: <br> <textarea name="comment" rows="10" cols="70" placeholder="More detail... (Is there a way to recreate the error? What happened?)"></textarea>
<br><br>
<input type="submit" name="submit" value="Submit" class="userFriendly">
I have tried many variations of if statements and the required function given with HTML5 but can not seem to get what I need.
Any help will be appreciated and thanks in advance.
Edit 1:
Here is the full code of my form:
<form action="send_form_email.php?OperationID=<?php print ($OperationID) ?>&title=<?php print ($title) ?>" method="post" onsubmit="return this.users.value != ''">
<table>
<tr>
<td>Name:</td>
<td>
<select required name="users">
<option value=""></option>
<?php
foreach($users as $key => $value){
echo "<option value=\"$key\">$key</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td <?php print $hiddenJobDiv ?>>Job Number:</td>
<td><input type="text" name="jobid" value="<?php echo ($jobid) ?>" <?php echo $disabledInput ?>></td>
</tr>
<tr>
<td <?php print $hiddenPartDiv ?>>Part Number:</td>
<td><input type="text" name="partid" value="<?php echo ($part_id) ?>" <?php echo $disabledInput ?>></td>
</tr>
<?php if ($OperationID == 20){ ?>
<tr>
<td>Machine:</td>
<td><input type="text" name="mach" value="<?php echo ($machCode) ?>" <?php echo $disabledInput ?>></td>
<tr>
<?php } ?>
</table><br>
Error:<br><br><br> <!-- Display of dynamic list. -->
<?php
$html = customErr($OperationID);
foreach ($html as $oneError):?> <!-- foreach used to find the next iteration of the array. -->
<label> <!-- Beginning of the dynamic radio button list. -->
<input name="category"
type="radio"
value="<?php echo $oneError; ?>"> <!-- Dynamic value to be used in Slack API and email. -->
<?php echo $oneError; ?> <!-- Dynamic value as a visual representation for user. -->
</label><br><br><br>
<?endforeach;?> <!-- Stops foreach and goes to next object if avaliable. -->
<label> <!-- A permanent radio button labeled "Other" for (cont.) -->
<input name="category" type="radio" value="Other" checked>Other <!-- all report error forms. -->
</label><br><br><br>
<?php
if (isset($_POST['category'])=="Other" && isset($_POST["comment"])=="")
{
$required[] = ("You must write a note if you choose \'other\'.");
}
return $required;
?>
Note: <?php echo $required ?> <br> <textarea name="comment" rows="10" cols="70"
placeholder="More detail... (Is there a way to recreate the error? What happened?)"></textarea> <!-- Allows the user to type in a custom message/note. -->
<br><br>
<input type="submit" name="submit" value="Submit" class="userFriendly"> <!-- A large 'submit' button for touch screen. -->
<input type="submit" name="close" value="Close" class="userFriendly"> <!-- A large 'close' button for touch screens. -->
</form>
Edit 2:
Here is the full code:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/css/main_style.css">
<style>
.error {color: #FF0000;}
table, th, td {border: 1px solid white;}
</style>
</head>
<body>
<script>
function close_window() {
close();
}
</script>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include("includes/classes.php");
include("includes/classes_monitoring.php");
$link = open_v8_db();
$users = get_clocked_in_users();
$OperationID = #$_REQUEST['OperationID'];
$title = "";
$grayedOut = false;
$disabledInput = "";
$hiddenJobDiv = "";
$hiddenPartDiv = "";
$ID = "";
$html = "";
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
if ($OperationID == 20)
{
$title = "Punching Machine";
$grayedOut = true;
}
elseif ($OperationID == 30)
{
$title = "Folding Machine";
$grayedOut = true;
}
elseif ($OperationID == 40 || $OperationID == 140)
{
$title = "Powder Coating";
$grayedOut = true;
}
elseif ($OperationID == 50 || $OperationID == 150)
{
$title = "Assembly";
$grayedOut = true;
}
elseif ($OperationID == 60 || $OperationID == 160)
{
$title = "Inspection";
$grayedOut = true;
}
elseif ($jobid != "" && $part_id == "")
{
$title = "Job";
$OperationID = 70;
}
else
{
$title = "General";
$OperationID = 80;
$grayedOut = false;
}
if ($greyedOut = true)
{
$disabledInput = "readonly";
}
function customErr($ID)
{
$html = "";
$issueReport_folder = 'document/Production System/';
$issueReporting = $issueReport_folder.'IssueReporting.csv';
$file_handle = fopen($issueReporting, "r");
if ($ID == 20)
{
while (!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[2] == "Punch")
{
$html[] = $line_of_text[1];
}
}
}
if ($ID == 30)
{
while (!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[2] == "Fold")
{
$html[] = $line_of_text[1];
}
}
}
if ($ID == 40 || $ID == 140)
{
while (!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[2] == "Powder")
{
$html[] = $line_of_text[1];
}
}
}
if ($ID == 50 || $ID == 150)
{
while (!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[2] == "Assembly")
{
$html[] = $line_of_text[1];
}
}
}
if ($ID == 60 || $ID == 160)
{
while (!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[2] == "Inspectoin")
{
$html[] = $line_of_text[1];
}
}
}
if ($ID == 70)
{
while (!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[2] == "Job")
{
$html[] = $line_of_text[1];
}
}
}
if ($ID == 80)
{
while (!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[2] == "General")
{
$html[] = $line_of_text[1];
}
}
}
fclose($file_handle);
return $html;
}
$jobErr = $partErr = $machErr = "";
$job = $part = $mach = $note = "";
if ($jobid == "")
{
$hiddenJobDiv = "style=\"display:none;";
}
if ($part_id == "")
{
$hiddenPartDiv = "style=\"display:none;";
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div class="reportForm">
<h2>Report <u><?php echo $title; ?></u> Error</h2>
<form action="send_form_email.php?OperationID=<?php print ($OperationID) ?>&title=<?php print ($title) ?>" method="post" onsubmit="return this.users.value != ''">
<table>
<tr>
<td>Name:</td>
<td>
<select required name="users">
<option value=""></option>
<?php
foreach($users as $key => $value){
echo "<option value=\"$key\">$key</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td <?php print $hiddenJobDiv ?>>Job Number:</td>
<td><input type="text" name="jobid" value="<?php echo ($jobid) ?>" <?php echo $disabledInput ?>></td>
</tr>
<tr>
<td <?php print $hiddenPartDiv ?>>Part Number:</td>
<td><input type="text" name="partid" value="<?php echo ($part_id) ?>" <?php echo $disabledInput ?>></td>
</tr>
<?php if ($OperationID == 20){ ?>
<tr>
<td>Machine:</td>
<td><input type="text" name="mach" value="<?php echo ($machCode) ?>" <?php echo $disabledInput ?>></td>
<tr>
<?php } ?>
</table><br>
Error:<br><br><br> <!-- Display of dynamic list. -->
<?php
$html = customErr($OperationID);
foreach ($html as $oneError):?> <!-- foreach used to find the next iteration of the array. -->
<label> <!-- Beginning of the dynamic radio button list. -->
<input name="category"
type="radio"
value="<?php echo $oneError; ?>"> <!-- Dynamic value to be used in Slack API and email. -->
<?php echo $oneError; ?> <!-- Dynamic value as a visual representation for user. -->
</label><br><br><br>
<?endforeach;?> <!-- Stops foreach and goes to next object if avaliable. -->
<label> <!-- A permanent radio button labeled "Other" for (cont.) -->
<input name="category" type="radio" value="Other" checked>Other <!-- all report error forms. -->
</label><br><br><br>
<?php
if (isset($_POST['category'])=="Other" && isset($_POST["comment"])=="")
{
$required[] = ("You must write a note if you choose \'other\'.");
}
return $required;
?>
Note: <?php echo $required ?> <br> <textarea name="comment" rows="10" cols="70"
placeholder="More detail... (Is there a way to recreate the error? What happened?)"></textarea> <!-- Allows the user to type in a custom message/note. -->
<br><br>
<input type="submit" name="submit" value="Submit" class="userFriendly"> <!-- A large 'submit' button for touch screen. -->
<input type="submit" name="close" value="Close" class="userFriendly"> <!-- A large 'close' button for touch screens. -->
</form> <!-- End of form. -->
</div>
</body>
</html>
you can't use required method like that. I think your solution is add dynamically textbox when user clicked the option button like that:
Add this javascript function in your file:
<script language="javascript">
function activateNote()
{
var i = 1;
note_div.innerHTML = "Note: <br> <textarea required id='comment' name='comment' rows='10' cols='70' placeholder='More detail... (Is there a way to recreate the error? What happened?)' ></textarea><br><br>"
}
</script>
Change your elements like that, "delete note textarea" than add div element:
<label>
<input onClick="activateNote()" type="radio" name="category" value="Other">Other
</label>
<div id="note_div"></div>
One possible solution is to conditionally add the required HTML5 attribute to the required field when the "Other" radio button is selected.
You can do this with a javascript method attached to your radio button that is (de/)activated on select.
I have an error that I can't figure out...
Om my webpage there is a form that the user has the ability to add some new input fields to. If the user is submitting the form, then the optional fields is empty when the php-file is handing them, why?
HTML:
<form method="post" action="newRequest.php">
<input type="text" name="title" />
<input type="hidden" name="fname" value="0" />
<input type="checkbox" name="fname" value="1"/>
<input type="hidden" name="ename" value="0" />
<input type="checkbox" name="ename" value="1" />
<input type="hidden" name="seat" value="0" />
<input type="checkbox" name="seat" value="1" />
<input type="hidden" name="fields" value="0" />
<input type="text" id="fields" name="fields" />
<input type="submit" />
</form>
PHP:
if (strlen($_POST[title]) > 2) {
$toDb[title] = $_POST[title];
} else {
error('title');
}
$toDb[fname] = $_POST[fname];
$toDb[ename] = $_POST[ename];
$toDb[seat] = $_POST[seat];
if ($_POST[fields] > 0) {
$i = 0;
while ($i < $_POST[fields]) {
$toDb[optional][$i] = $_POST[optional-$i];
$i++;
}
$toDb[optional] = serialize($toDb[optional]);
} else {
$toDb[optional] = 0;
}
newEvent($toDb,$dbh);
JQuery that is adding dynamical fields:
$(document).ready(function() {
$('#fields').focusout(function(){
var fields = $('#fields').val();
var i = 0;
while(i < fields) {
$('#fields').after("Valfritt fält "+(i+1)+":<input type='text' name='optional"+i+"' />");
i++;
}
})
})
You should quote array indexes. It should be
$toDb['optional'][$i] = $_POST['optional'.$i];
You are missing commas in $_POST
$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];
Here is your modified code
if (strlen($_POST['title']) > 2) {
$toDb['title'] = $_POST['title'];
} else {
error('title');
}
$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];
if (count($_POST) > 0) {
$i = 0;
while ($i < count($_POST)) {
$toDb['optional'][$i] = $_POST['optional-'.$i];
$i++;
}
$toDb['optional'] = serialize($toDb['optional']);
} else {
$toDb['optional'] = 0;
}
newEvent($toDb,$dbh);
Also use count() to check if $_POST has values > 0.
I faced the same problem and I solved it using Javascript, like this :
add a new text field every time a button is pressed
This is really bugging me and I can't figure it out. I have a form with a few options being sent by POST:
<form method="POST" action="scripts/submit.php"><strong>
To User: <input type="text" name="ID" size="21" /><br />
Short Description: <input type="text" name="Item" size="21" /><br />
Link: <input type="text" name="Link" size="21" /><br />
Points: <select name="Points">
<option value="1" selected="selected">1</option>
<option value="0">0</option><option value="-1">-1</option>
</select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
<input type="submit" value="Send" />
</form>
And here is the portion of submit.php that is giving me trouble:
<?php
include('functions.php');
Connect();
if(!isset($_SESSION))
{
session_start();
}
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);
Does anyone see an issue here? I am getting undefined index's from all of the variables except the session one.
Thanks in advance.
edit: If i just have this:
<?php
include('functions.php');
Connect();
if(!isset($_SESSION))
{
session_start();
}
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points = Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);
?>
The variables populate just fine. If I add:
$id_query=mysql_query("SELECT ID FROM tbl_users WHERE Username = '$id_to'");
$count=mysql_num_rows($id_query);
$id_row=mysql_fetch_array($id_query);
$id_to=$id_row['ID'];
if ($points> 1 || $points< -1) {
echo "Nice try";
exit();
} else {
if(!($id_to == $id_from))
{
if($count==1)
{
mysql_query("INSERT INTO tbl_data (Item, Link, Points, Text, ID_To, ID_From) VALUES ('$item', '$link', '$points', '$text', '$id_to','$id_from')");
header('Location:?id=submit');
}
else
{
echo "Nice try1";
}
}
else
{
echo "Nice try2";
}
}
I just took your code over at my dev server at tried to test run it. Since I don't know what your Sanitize() do, I can't be sure what is going on inside this function.
If you try to remove the Sanitize(), I'm pretty sure it would work and you will have to look inside this to find the bug.
I'm guessing you might be missing something like ($var, str) for sanitize a string. Can you please tell a little more about this function ?
edit: some minor spelling errors.
Edit: Did some more test and made the error happen and the two codes shows it. The 1st works, while the 2nd gives me a empty var_dump.
This one gives me a full var_dump();
<?
function Sanitize($String) {
$output = mysql_real_escape_string(stripslashes($String));
return $output;
}
if(!isset($_SESSION))
{
session_start();
}
?>
<form method="post" action=""><strong>
To User: <input type="text" name="ID" size="21" /><br />
Short Description: <input type="text" name="Item" size="21" /><br />
Link: <input type="text" name="Link" size="21" /><br />
Points: <select name="Points"><option value="1" selected="selected">1</option><option value="0">0</option><option value="-1">-1</option></select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
<input type="submit" value="Send" />
</form>
<?
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);
var_dump($_POST);
echo $text;
?>
This one gives me an empty var_dump
<?
if(!isset($_SESSION))
{
session_start();
}
?>
<form method="post" action=""><strong>
To User: <input type="text" name="ID" size="21" /><br />
Short Description: <input type="text" name="Item" size="21" /><br />
Link: <input type="text" name="Link" size="21" /><br />
Points: <select name="Points"><option value="1" selected="selected">1</option><option value="0">0</option><option value="-1">-1</option></select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
<input type="submit" value="Send" />
</form>
<?
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);
var_dump($_POST);
echo $text;
?>
based on your comment that you have this code:
$id_query = mysql_query("SELECT ID FROM tbl_users WHERE Username = '$id_to'");
$count = mysql_num_rows($id_query);
$id_row = mysql_fetch_array($id_query);
$id_to = $id_row['ID'];
if ($points > 1 || $points < -1) {
} else {
if (! ($id_to == $id_from)) {
if ($count == 1) {
mysql_query("INSERT INTO tbl_data (Item, Link) VALUES ('$item', '$link')");
header('Location:?id=submit');
} else {}
} else {}
}
I think the problem is on the line that says:
header('Location:?id=submit');
Perhaps you're testing with something that somehow make $points either greater than 1 or less than -1, $count is 1, and $id_to is different from $id_from, which then make the else block executed and (especially the line) header() executed, and user get redirect immediately.
To check if this is true, try var_dump($_GET) to see if you got something like:
array (size=1)
'id' => string 'submit' (length=6)
If you do, and perhaps the database isn't updated, then it's the mysql_query that sits right before the header is the one that you need to check.
Hope this helps.