Question

It's a personal project, not going online, I don't need to worry about mysql depreciation nor SQL injections

I keep getting:

Parse error: syntax error, unexpected 'return' (T_RETURN), expecting ',' or ';' in C:\wamp\www\StudentExams\examisud.php on line 64

My code in the order it's written (All in 1 php file):

My Delete query:

if (isset($_POST['delete'])) //DELETE QUERY
{
$DeleteQuery = "DELETE FROM Exam WHERE exam_id='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};

My form:

echo "<form action=examisud.php method=post>"; //HTML FORM ECHOED OUT BY PHP
echo "<tr>";
echo "<td>" . "<input type=text name=exam_id value=" . $record['exam_id'] . " </td>";
echo "<td>" . "<input type=text name=subject value=" . $record['subject'] . " </td>";
echo "<td>" . "<input type=text name=exam_date value=" . $record['exam_date'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['exam_id'] . " </td>";
echo "<td>" . "<input type=image name=update value=update id=submit src=images/update.png" . " </td>";
echo "<td>" . "<input type=image name=delete value=delete id=submit onClick="return deleteconfig()" src=images/delete.png" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=examisud.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uexam_id></td>";
echo "<td><input type=text name=usubject></td>";
echo "<td><input type=text name=uexam_date></td>";
echo "<td>" . "<input type=image name=insert value=insert id=submit src=images/insert.png" . " </td>";
echo "</form>";
echo "</table>";
echo '</div>';

My javascript:

<html>
<header>
<script>
function deleteconfig(){

var del=confirm("Are you sure you want to delete this record?");
if (del==true){
   alert ("record deleted")
}else{
    alert("Record Not Deleted")
}
return del;
}
</script>
</header>
</html>

How can I get this code to work right? I need a pop up asking me to confirm if I want to delete the field values or not. I'm quite a newbie at php AND javascript.

Was it helpful?

Solution

you didnt escaped quotes in your echo.. :

echo "<td>" . "<input type=image name=delete value=delete id=submit onClick=\"return deleteconfig()\" src=images/delete.png" . " </td>";

OTHER TIPS

Just a cosmetic tip: Instead of a echo on each line, you could just concatenate the whole string with ., and still use line breaks for each line.

Also, it appears you've forgotten to close a couple of the input elements.

I would also recommend using quotes in the html's properties. On short properties like type=text, it's not mandatory, but longer values like an src really can't do without.

Last, try to use single quotes (') instead of double quotes ("), and avoid string concatenation where it isn't necessary (combining 2 static strings), like this:

echo '<form action="examisud.php" method="post">'. //HTML FORM ECHOED OUT BY PHP
        '<tr>'.
            '<td><input type="text" name="exam_id" value="' . $record['exam_id'] . '" </td>'.
            '<td><input type="text" name="subject" value="' . $record['subject'] . '" </td>'.
            '<td><input type="text" name="exam_date" value="' . $record['exam_date'] . '" </td>'.
            '<td><input type="hidden" name=hidden" value="' . $record['exam_id'] . '" </td>'.
            '<td><input type="image" name="update" value="update" id="submit" src="images/update.png" ></td>'.
            '<td><input type="image" name="delete" value="delete" id="submit" onClick="return deleteconfig()" src="images/delete.png" ></td>'.
        '</tr>'.
    '</form>';
}
echo '<form action="examisud.php" method="post">'.
        '<tr>'.
            '<td><input type="text" name="uexam_id"></td>'.
            '<td><input type="text" name="usubject"></td>'.
            '<td><input type="text" name="uexam_date"></td>'.
            '<td><input type="image name="insert" value="insert" id="submit" src="images/insert.png" ></td>'.
        '</tr>'.
    '</form>'.
'</table>'.
'</div>';

Using single quotes tells the php interpreter not to bother checking for variables in the strings. This, and the reduced amount of concatenation make for more efficient, and in my opinion, more readable code.

As for your JavaScript:

if (del==true){

Can be replaced with

if (del){

An if checks whether it's value is true(-ish) or not. Since confirm returns a boolean output, there's no need to check if true == true or false == true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top