hello i have a problem everytime i submitted a form . This is script i made
<?php include 'rumahtable.php';
include 'connect.php';
$result = $conn -> query("select kodhomestay from rumah");
?><br><br>
<form action="" method="POST">
<label>Memadam data rumah : </label>
<select name="rumah">
<option>--Pilih Kod Rumah--</option>
<?php while ($rows = $result->fetch_assoc()){
$jrumah = $rows['kodhomestay'];
echo "<option value='$jrumah'>$jrumah</option>";
} ?>
</select><br><br>
<button type="submit" name="submit" >Delete</button>
<button type="submit" name="update">Update</button>
</form>
<?php if(isset($_POST["submit"])){
include 'deletedata.php';
}
if(isset($_POST["update"])){
ob_clean();
$rumah = $_POST["rumah"];
include 'updatedata.php';
}?>
and this is the deletedata.php:
<?php include 'connect.php';
$rumah=$_POST["rumah"];
$query = "delete from rumah where kodhomestay = '$rumah'";
$delete= mysqli_query($conn,$query);
if($delete){ ?>
<script type="text/javascript">
window.alert("Data Berjaya Dipadam.");
</script>
<?php header("Refresh:0");
}else{ ?>
<script type="text/javascript">
window.alert("Maaf data tidak dapat dipadamkan atau data tidak wujud.");
</script>
<?php header("Refresh:0");
}
and this is updatesenarai.php:
<?php $rumah = "RH007";
if(empty($_POST['submit'])){ ?>
<h2>Kemaskini Data Rumah</h2>
<?php include 'updaterumahtable.php'; ?>
<form action="" method="POST">
<label>Kod Rumah : </label><?php echo $rumah ?><br><br>
<label>Nama Rumah : </label><input type="text" name="jrumah"><br><br>
<label>Harga : </label><input type="number" name="price"><br><br>
<input type="submit" name="submit" value="Kemaskini">
<form>
<?php }else{
include 'connect.php';
$jr = $_POST['jrumah'];
$price = $_POST['price'];
$query = "update rumah set jenishomestay='$jr', harga=$price where kodhomestay='$rumah'";
$update= mysqli_query($conn,$query);
if($update){?>
<script type="text/javascript">
window.alert("Data Berjaya Dikemaskini.");
</script>
<?php header("Refresh:0");
}else{ ?>
<script type="text/javascript">
window.alert("Maaf data tidak dapat dikemaskini atau data tidak wujud.");
</script>
<?php header("Refresh:0");
}
mysqli_close($conn);
}?>
everytime i click the update button. it uses the updatesenarai.php but when i want to update the data in sql. it wont update and keep saying the first window alert in delete data. But when i used only updatedata.php it works just fine. If i change the submit button name it the whole file refreshes even without the header(). I tried changing the $conn into something else but it still saying the first window alert from deletedata.php. Im still new.
Check your updatesenarai.php. Your deletedata.php and updatesenarai.php are exactly same. Please write the code for updating in updatesenarai.php.
Related
I want to build a website that can make people post things and comments on it
now I have some problems
I hope that I could submit the comment without refreshing the page (I found a way to solve this problem but the input won't be cleared)
I already search a lot of information about preventing refresh page on submit but there is no one works for me
and I hope that someone can help me with another problem
I want to make the website show the comment immediately after posting it (without refreshing the page)
directly edit my code and paste on here will be appreciated
below is my index.php
<?php
session_start();
require_once 'db.php';
require_once 'new_tdb.php';
//$datas = get_publish_article();
if(isset($_COOKIE["mcsh"])){
}else{
header("Location: ../mcsh/login/index.php");
exit();
}
?>
<html>
<head>
<title>
website
</title>
<link rel="shortcut icon" href="icon.png" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
<?php include 'menu.css' ?>
</style>
</head>
<body>
<?php include 'menu.php' ?>
<center>
<font class="corn-blue">
</font>
</center>
<p> </p>
<?php
$bij = "SELECT * FROM `post` WHERE `comment` != 1 order by id DESC";
$result = mysqli_query($conn, $bij);
$rows = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)){
$post_id = $row['id'];
$ins = $row['ins'];
$s_uid = $row['s_uid'];
$p_id = $row['id'];
//$selc = "SELECT * FROM `post` WHERE `post_id` = $p_id";
?>
<center>
<table>
<tr>
<th>
Poster: <?php echo $s_uid;?>
</th>
</tr>
<tr>
<td>
<?php echo $ins;?>
</td>
</tr>
<tr>
<td>
<details>
<summary>Comment</summary>
<form id="worker" action="comment.php" method="post" target="the_iframe">
<input type="hidden" name="post_id" value="<?php echo $p_id; ?>">
<p><input type="text" name="comment" placeholder="Comment......"> <input type="submit" hidefocus="true" name="submit"></p>
</form>
<iframe id="is_iframe" name="the_iframe" style="display: none;"></iframe>
<?php
$sel = "SELECT * FROM `post` WHERE `post_id` = $post_id order by id DESC";
$result2 = mysqli_query($conn, $sel);
$rows2 = mysqli_num_rows($result2);
while($row2 = mysqli_fetch_array($result2)){
$ins2 = $row2['ins'];
$poster2 = $row2['s_uid'];
?>
<p><?php echo $poster2 . ': ' . $ins2; ?></p>
<?php } ?>
</details>
</td>
</tr>
</table>
</center>
<h1>
-
</h1>
<?php
}
?>
<?php if (empty($row['file'])){
}else{
echo('<img src="image/'); echo $row['file']; echo('"width="100%" height="auto">');
}
?>
</body>
</html>
As #Barmar said you want to use AJAX for Asynchronous actions in your website. Submit event must refresh the page to resend the new form data.
PHP is synchrony, what means if you want to do something with synchronous codes you will need to refresh the whole page.
Lear Further : Javascript documentation of Ajax by mozila here
I am trying to get the value of the selected option from my select. And I am trying to see it's output through a javascript echo. Here's what I've got so far. I am not getting the value
<form method="post" action="">
<select class="form-control" name="empSel" id="empSel">
<?php
$sql2 = "SELECT * FROM employee";
$result = mysql_query($sql2) or die("Couldn't execute sql2");
while ($row2 = mysql_fetch_assoc($result)) {
?>
<option value="<?php echo $row2['lastname'] ?>"><?=
/*$row2['user_surname']." ".*/
$row2['id']."-".$row2['lastname'] ?></option>
<?php
}
?>
</select>
<Label> Confirm</Label>
<div class="form-group col-md-6">
<input type="submit" class="btn btn-block btn-info"
name="submit"/>
</div>
</form>
<?php
if (isset($_POST['submit'])){
$userid = $_POST['empSel'];
echo '<script type="text/javascript"> alert('.$userid.')</script>';
$userid = preg_replace('/\D/', '', $userid);
$sql2 = "SELECT * FROM employee where id ='userid'";
$result = mysql_query($sql2) or die("Couldn't execute sql2");
while ($row = mysql_fetch_assoc($result)) {
echo '<script type="text/javascript"> alert("")</script>';
}
}
?>
An javascript alert doesn't pop out on this code. However, when I switch the value in the echo to a different variable an alert pops up. What does it mean? Do I properly get the value of my select and the page refreshed instantly that I didn't get to see it? Thanks
Edit:
An example of the option value would be 1-Lastname
And here's what I've tried.
<?php
if (isset($_POST['empSel'])){
$userid = $_POST['empSel'];
$userid = intval($userid);
echo '<script type="text/javascript"> alert('.$userid.')</script>';
}
?>
Now the javascript alert shows, but it echo 0. I think I am still not getting the value of my selected option
<option value="<?php echo $row2['id'] ?>">
This fixed it. In my previous code the option value was the surname...
I want to make a multiple choice and the question is coming from database. I am sure that my database name is correct, but when i click next, the question is not changing, it is directly going to the end/result of question and the question is not random too. The last question that I inserted into the database is displayed in webpage. Please help
this is my code:
<?php
require_once('includes/db_conn.php');
$query = "select * from question";
$query_result = $dbc->query($query);
$num_questions_returned = $query_result->num_rows;
if ($num_questions_returned < 1){
echo "There is no question in the database";
exit();}
$questionsArray = array();
while ($row = $query_result->fetch_assoc()){
$questionsArray[] = $row;
}
$correctAnswerArray = array();
foreach($questionsArray as $question){
$correctAnswerArray[$question['question']] = $question['correct_answer'];
}
$questions = array();
foreach($questionsArray as $question) {
$questions[$question['question']] = $question['question'];
}
$choices = array();
foreach ($questionsArray as $row) {
$choices[$row['question']] = array($row['wrong_answer1'], $row['wrong_answer2'], $row['wrong_answer3'], $row['correct_answer']);
}
error_reporting(0);
$address = "";
$randomizequestions ="yes";
$a = array(
1 => array(
0 => $question['question'],
1 => $row['wrong_answer1'],
2 => $row['wrong_answer2'],
3 => $row['wrong_answer3'],
4 => $row['correct_answer'],
6 => 4
),
);
$max=1;
$question=$_POST["question"] ;
if ($_POST["Randon"]==0){
if($randomizequestions =="yes"){$randval = mt_rand(1,$max);}else{$randval=1;}
$randval2 = $randval;
}else{
$randval=$_POST["Randon"];
$randval2=$_POST["Randon"] + $question;
if ($randval2>$max){
$randval2=$randval2-$max;
}
}
$ok=$_POST["ok"] ;
if ($question==0){
$question=0;
$ok=0;
$percentage=0;
}else{
$percentage= Round(100*$ok / $question);
}
?>
<HTML><HEAD>
<SCRIPT LANGUAGE='JavaScript'>
<!--
function Goahead (number){
if (document.percentaje.response.value==0){
if (number==<?php print $a[$randval2][6] ; ?>){
document.percentaje.response.value=1
document.percentaje.question.value++
document.percentaje.ok.value++
}else{
document.percentaje.response.value=1
document.percentaje.question.value++
}
}
if (number==<?php print $a[$randval2][6] ; ?>){
document.question.response.value="Correct"
}else{
document.question.response.value="Incorrect"
}
}
// -->
</SCRIPT>
</HEAD>
<BODY BGCOLOR=FFFFFF>
<CENTER>
<H1><?php print "$title"; ?></H1>
<TABLE BORDER=0 CELLSPACING=5 WIDTH=500>
<?php if ($question<$max){ ?>
<TR><TD ALIGN=RIGHT>
<FORM METHOD=POST NAME="percentaje" ACTION="<?php print $URL; ?>">
<BR>Percentaje of correct responses: <?php print $percentage; ?> %
<BR><input type=submit value="Next >>">
<input type=hidden name=response value=0>
<input type=hidden name=question value=<?php print $question; ?>>
<input type=hidden name=ok value=<?php print $ok; ?>>
<input type=hidden name=Randon value=<?php print $randval; ?>>
<br><?php print $question+1; ?> / <?php print $max; ?>
</FORM>
<HR>
</TD></TR>
<TR><TD>
<FORM METHOD=POST NAME="question" ACTION="">
<?php print "<b>".$a[$randval2][0]."</b>"; ?>
<BR> <INPUT TYPE=radio NAME="option" VALUE="1" onClick=" Goahead (1);"><?php print $a[$randval2][1] ; ?>
<BR> <INPUT TYPE=radio NAME="option" VALUE="2" onClick=" Goahead (2);"><?php print $a[$randval2][2] ; ?>
<?php if ($a[$randval2][3]!=""){ ?>
<BR> <INPUT TYPE=radio NAME="option" VALUE="3" onClick=" Goahead (3);"><?php print $a[$randval2][3] ; } ?>
<?php if ($a[$randval2][4]!=""){ ?>
<BR> <INPUT TYPE=radio NAME="option" VALUE="4" onClick=" Goahead (4);"><?php print $a[$randval2][4] ; } ?>
<BR> <input type=text name=response size=8>
</FORM>
<?php
}else{
?>
<TR><TD ALIGN=Center>
The Quiz has finished
<BR>Percentage of correct responses: <?php print $percentage ; ?> %
<p>Home Page
<?php } ?>
</TD></TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
this is my process add data to database:
<?php
include('includes/header.html');
error_reporting(-1);
ini_set('display_errors', 'On');
//Check for empty fields
if(empty($_POST['question'])||
empty($_POST['correct_answer']) ||
empty($_POST['wrong_answer1']) ||
empty($_POST['wrong_answer2']) ||
empty($_POST['wrong_answer3']))
{
echo "Please complete all fields";
exit();
}
//Create short variables
$question = $_POST['question'];
$correct_answer = ($_POST['correct_answer']);
$wrong_answer1 = ($_POST['wrong_answer1']);
$wrong_answer2 = ($_POST['wrong_answer2']);
$wrong_answer3 = ($_POST['wrong_answer3']);
//connect to the database
require_once('includes/db_conn.php');
//Create the insert query
$query = "INSERT INTO question VALUES ('$question', '$correct_answer', '$wrong_answer1','$wrong_answer2','$wrong_answer3')";
$result = $dbc->query($query);
if($result){
echo "Your quiz has been saved";
} else {
echo '<h1>System Error</h1>';
}
$dbc->close();
?>
Your first mistake is making array of $a why are you not using $choices which you had made in last for loop and for random questions handle $choices array and your submit button of next is outside the form so you will not get form values on next button.take it inside the form tag.
Another thing is on radio button click don't write anything just write your function on submit click event and then handle the next question after saving the previous answer.
These are what my suggestions.. for more help put here your database back up and its connected files so that I can help you in coding.
In the following code, I've generated a php form that takes in values from a user for an arp network attack I'm working on. However, I'm trying to display the number of packets sent "$loops" in the javascript alert, when I run the code the number of packets is not displayed in the alert, and I don't know what went wrong so if someone could go through and flow of my code I'd appreciate it very much.
<form method ='post' action ="<?php echo $_SERVER['PHP_SELF'];?>">
<?php $loops = "";?>
<b> <p>Number of packets to be sent:</p>
<input type="text" name="loops" value="<?php echo $loops;?>">
<br><br></b>
<?php
$php_var = $loops;?>
<p style="text-align: center;">
<button onclick="myFunction()">Submit</button></p><br>
<script>
function myFunction() {
var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
alert(js_var);
}
</script>
</font>
</form>
<?php
if($_SERVER["REQUEST_METHOD"]== "POST"){
if(isset($_POST["loops"])){
$loops=$_REQUEST['loops'];
}
?>
You are assigning $php_var to blank here.
<?php $loops = "";?>
<?php $php_var = $loops;?>
So, its displaying no value.
I think you need to take value input by user in textbox.
<input type="text" name="loops" value="<?php echo $loops;?>" id="loops"> // Observe the added `id` attribute.
You can do it like:
<script>
function myFunction() {
//var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
var val = document.getElementById('loops').value;
var js_var = "The number of packets sent is: " + val;
alert(js_var);
}
</script>
try this code
<form method ='post' action ="<?php echo $_SERVER['PHP_SELF'];?>">
$loops = isset($_POST["loops"])?$_POST["loops"]:"";
<b> <p>Number of packets to be sent:</p>
<input type="text" name="loops" value="<?php echo $loops;?>">
<br><br></b>
<?php
$php_var = $loops;?>
<p style="text-align: center;">
<button onclick="myFunction()">Submit</button></p><br>
<script>
function myFunction() {
var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
alert(js_var);
}
</script>
</font>
</form>
<?php
if($_SERVER["REQUEST_METHOD"]== "POST"){
if(isset($_POST["loops"])){
$loops=$_REQUEST['loops'];
}
?>
You should use PHP at the starting of the page always. Its required so that you can get the variables available in HTML.
<form method ='post' action ="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php $loops = ''; ?>
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["loops"])) {
$loops = $_REQUEST['loops'];
}
}
?>
<b>
<p>Number of packets to be sent:</p>
<input type="text" name="loops" value="<?php echo $loops; ?>">
<br><br>
</b>
<?php $php_var = $loops; ?>
<p style="text-align: center;">
<button onclick="myFunction()">Submit</button>
</p>
<br>
<script>
function myFunction() {
var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
alert(js_var);
}
</script>
</form>
you should check if there is any $_REQUEST['loops'] after $loop variable initialization.
so the code should be
<?php
$loops = "";
if($_SERVER["REQUEST_METHOD"]== "POST"){
if(isset($_POST["loops"])){
$loops=$_REQUEST['loops'];
}
}
?>
<form method ='post' action ="<?php echo $_SERVER['PHP_SELF'];?>">
<b> <p>Number of packets to be sent:</p>
<input type="text" name="loops" value="<?php echo $loops;?>">
<br><br></b>
<?php
$php_var = $loops;?>
<p style="text-align: center;">
<button onclick="myFunction()">Submit</button></p><br>
<script>
function myFunction() {
var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
alert(js_var);
}
</script>
</font>
</form>
try like
<?php $loops = "123";?>
function myFunction() {
var js_var = "The number of packets sent is: <?php echo $php_var; ?> ";
alert(js_var);
}
Good morning! Please some body help or give some sample code about how to read text value (per line)
<body onLoad="displayResult()">
<table align="center">
<tr>
<td>
<?php $query2="SELECT * FROM upload1 WHERE NAME='xmlsample1.xml'";
$result1=mysql_query($query2);
$row = mysql_fetch_array($result1);
?>
<form action="">
<textarea id="validxml" rows="50" cols="100">
<?php echo $row['CONTENT']; ?>
</textarea>
<br><br>
<input type="button" value="Verify XML" onClick="validateXML()" />
</form>
</td>
</tr>
</table>
</body>
Access the value of textareaand split newline
console.log(document.getElementById('validxml').value.split("\n"));
JSFiddle
connect.php should be kept on a separate, secure web page.
<?php
function db(){
return new mysqli('host', 'username', 'password', 'database');
}
?>
Now on your other page:
<?php
include 'connect.php'; $db = db();
$sq = $db->query("SELECT * FROM upload1 WHERE name='xmlsample1.xml'"); $ta = '';
if($sq->num_rows > 0){
while($row = $sq->fetch_object()){
$ta .= "<textarea class='validxml' name='{$row->name}'>{$row->content}</textarea>";
}
}
$sq->free(); $db->close();
?>
Now just echo $ta into your HTML. Of course, I wouldn't even use a <textarea> to just output code, unless you want your user to be able to edit it.