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.
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
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.
Objective Inserting name(input-text) and info(textarea-contains multiple lines) into the database, and after submission of the form, at the same page, 2 columns are for displaying the same data in columns, name & info, but under info column. I have made buttons for each row in front of the names, which is used as slideToggle for showing/hiding which contains the data retrieved from the 'info' column
Problem - when I am clicking the button of 1st row, instead of displaying the info related to 1st entry only, it is sliding and showing all info(s) related to all entries at only click.
*others - one more input has been added to the form but as hidden used for id(auto increment)
----index.php-----
<?php include('php_code.php'); ?>
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM records WHERE id=$id");
if (count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$name = $n['name'];
$acc = $n['acc_no'];
$info = $n['info'];
}
}
?>
<html>
<head>
<title>JSK</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('form').hide();
$('p').hide();
$('#sp').hide();
$("#inf").click(function(){
$("p").slideToggle();
$('#sp').slideToggle();
});
$("#fo").click(function(){
$("form").slideToggle();
});
});
</script>
</head>
<body>
<div class="container">
<div class="left">
<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif ?>
<?php $results = mysqli_query($db, "SELECT * FROM records"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Account No</th>
<th>Info</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['acc_no']; ?></td>
<td><button id="inf" onclick="myFunction()">show</button></td>
<td>
<a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>
</td>
<td>
Delete
</td>
</tr>
<tr id="sp"> <td colspan="4"><p> <?php echo $row['info']; ?> </p></td>
</tr>
<?php } ?>
</table>
<div id="fotable" align="center">
<button id="fo">Add New/Edit</button>
</div>
<form method="post" action="php_code.php" >
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="input-group">
<label>Name</label>
<input type="text" autocomplete="off" name="name" value="<?php echo $name; ?>">
</div>
<div class="input-group">
<label>Account No</label>
<input type="text" name="acc" value="<?php echo $acc; ?>">
</div>
<div class="input-group">
<label for="info">Info</label>
<textarea class="form-control" rows="8" name="info" id="info"><?php echo $row['info']; ?></textarea>
</div>
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Add Account</button>
<?php endif ?>
</div>
</form>
</div><!-- left closed -->
<div class="right">
hello
</div> <!-- right closed -->
</div> <!-- container closed -->
</body>
</html>
---php_code.php-----
<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'jskrecords');
// initialize variables
$name = "";
$acc = "";
$info = "";
$id = 0;
$update = false;
if (isset($_POST['save'])) {
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];
mysqli_query($db, "INSERT INTO records (name, acc_no, info) VALUES ('$name', '$acc', '$info')");
$_SESSION['message'] = "Account saved";
header('location: index.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];
mysqli_query($db, "UPDATE records SET name='$name',acc_no='$acc',info='$info' WHERE id=$id");
$_SESSION['message'] = "Account updated!";
header('location: index.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM records WHERE id=$id");
$_SESSION['message'] = "ACC deleted!";
header('location: index.php');
}
?>
Concept:
If you are creating multiple form from same mysql table using php script, You need to give each form a unique id.
e.g.
<form method="post" action="php_code.php" id="form<?= $id ?>">
Then add data-target="#form" to button with class 'inf'. It will store id of form.
<button class="inf" data-target="#form<?= $id ?>">show</button>
When button is clicked we know which form to open from data-target.
<script>
$('.container').on('click','button.inf',function(e){
e.preventDefault();
var formid=$(this).attr('data-target'); //get value of data-target attribute
//...proceed to play toggle with this form 'formid'
Hi actually when i execute the code i get an output it will be spitted in echo statement but if i put echo in textarea i get only last value of string from my database
$sql = mysqli_query($con, "SELECT naveen from kumar where id<5");
while ($row = mysqli_fetch_array($sql)) {
$myString = $row['admin'];
$myArray = explode(',', $myString);
foreach ($myArray as $my_Array) {
echo $my_Array.'<br>';
}
}
?>
<form method="post" action="">
<textarea name="valid" cols="60" rows="5"><?php echo $my_Array;?></textarea>
</form>
<?php ?>
This is my result
You need to concatenate your result with previous one. Delcare $my_Array = ""; before while loop For break line in textarea you have to use
foreach ($myArray as $my_Array) {
$my_Array .= '
';
echo $my_Array; //IF you also want to echo here
}
You should paint a <textarea> inside of first bucle (while).
echo '<form method="post" action="">';
$sql = mysqli_query($con, "SELECT naveen from kumar where id<5");
while ($row = mysqli_fetch_array($sql)) {
$myString = $row['admin'];
$myArray = explode(',', $myString);
echo '<textarea name="valid" cols="60" rows="5">';
foreach ($myArray as $my_Array) {
echo = $my_Array.'<br>';
}
echo '</textarea>';
}
?>
</form>
I hope this example will solve your issue. Please let me know if you still have any queries.
<?php
$myvalue = array('one','two','three');
$myterm = implode('-', $myvalue);
$mynewvalue = explode('-',$myterm);
?>
<form method="post" action="">
<textarea name="valid" cols="60" rows="5">
<?php
foreach($mynewvalue as $mvn)
{
echo $mvn.',';
}
?>
</textarea>
</form>
You have to do it like this:
<form method="post" action="">
<?php $sql = mysqli_query($con, "SELECT naveen from kumar where id<5");
while ($row = mysqli_fetch_array($sql)) {
$myString = $row['admin'];
$myArray[] = explode(',', $myString);
}
?>
<textarea name="valid" cols="60" rows="5"><?php echo implode(" ",$my_Array);?></textarea>
</form>
<?php ?>
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.