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);
}
Related
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.
I am trying to pass hidden value from page to another page and it work fine only for first record however for other records it's showing error
Here is the code:
$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<input type="hidden" id="hidden_user_id" value="<?php echo $row["id"] ?>">
<h3><?php echo $row["title"] ?>:</h3>
<p class="lead">
<?php echo $row["jdes"] ?>
</p>
<button type="button" id="requestthis" class="btn btn-primary">
Request
</button>
<?php
}
} else {
echo "Nothing to display Yet";
}
?>
jobs-inner.php
<?php
echo $_GET['hidden_id'];
?>
Javascript:-
$(function() { //ready function
$('#requestthis').on('click', function(e){ //click event
e.preventDefault();
var hidden_id = $('#hidden_user_id').val();
var url = "jobs-inner.php?hidden_id="+hidden_id;
window.location.replace(url);
})
})
Error:-
Undefined index: hidden_id in C:\wamp64\www\project\jobs-inner.php on line 3
It might be a simple problem but I am a beginner and I can't figure it out.
Your value is unique but the id isn't. Make the id of the input unique something like below.
<input type="hidden" id="hidden_user_<?php echo $row["id"] ?>" value="<?php echo $row["id"] ?>">
but you would have to do a count on code below to make it display base on how many rows you have.
<?php
echo $_GET['hidden_id'];
?>
Without JavaScript
$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
$count = 1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<input type="hidden" id="hidden_user_<?php echo $count ?>" value="<?php echo $row["id"] ?>">
<h3><?php echo $row["title"] ?>:</h3>
<p class="lead"><?php echo $row["jdes"] ?></p>
<form id="<?php echo $count ?>" action="jobs-inner.php?hidden_id=<?php echo $row["id"] ?>" method="post">
<input type="submit" vaule="Request">
</form>
<?php
$count++;
}
} else {
echo "Nothing to display Yet";
}
?>
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.
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.
I'am trying to pass the value of the textbox (id = msg) to js but the output is always the first textbox value on the foreach loop. Please Help me. Here is the code.
JS:
function sub() {
var name = document.getElementById('msg').value;
alert(name);
}
HTML:
<?php
foreach($messages->result() as $msg):
foreach($agentname->result() as $agnt):
if($msg->message_status == 1):
$message_status = "<font class='icon-exclamation-sign'></font> <font class='purple'>New message!</font>";
else:
$message_status = "";
endif;
?>
<div class="accordion-wrapper" style="margin-top:0">
<a id="message" onclick="return sub();" rel="msg<?php echo $msg->message_id;?>" id = "button_id" style="background-color:#C2E4CD" href="javascript:void(0)" class="accordion-title blue"><span><?php echo $message_status; ?> <font class="icon-comment"></font> <font class="orange" >From:</font> <?php echo $agnt->agent_shortname;?> | <font class="icon-envelope-alt"></font> <font class="orange">Subject:</font> <?php echo $msg->message_title;?></span></a>
<div class="accordion-content">
<input type="text" id="msg" value="<?php echo $msg->message_id;?>" />
<p><?php echo $msg->message;?></p>
</div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
You are putting value of outer loop in the textbox.
As per my knowledge, you should put inner loop's value in the textbox.
<input type="text" id="msg" value="<?php echo $msg->message_id;?>" />
Did you mean $agnt instead of $msg?
I have added name attribute to your input and a button clicking on which it will display the values of the inputs one by one, so now you can try this code
JS:
function check() {
var inputs = document.getElementsByName('messages[]');
alert(inputs.length);
for (var x = 0; x < inputs.length; x++) {
inp_val = inputs[x].value;
alert(inp_val);
}
}
HTML :
<?php
foreach($messages->result() as $msg):
foreach($agentname->result() as $agnt):
if($msg->message_status == 1):
$message_status = "<font class='icon-exclamation-sign'></font> <font class='purple'>New message!</font>";
else:
$message_status = "";
endif;
?>
<div class="accordion-wrapper" style="margin-top:0">
<a id="message" onclick="return sub();" rel="msg<?php echo $msg->message_id;?>" id = "button_id" style="background-color:#C2E4CD" href="javascript:void(0)" class="accordion-title blue"><span><?php echo $message_status; ?> <font class="icon-comment"></font> <font class="orange" >From:</font> <?php echo $agnt->agent_shortname;?> | <font class="icon-envelope-alt"></font> <font class="orange">Subject:</font> <?php echo $msg->message_title;?></span></a>
<div class="accordion-content">
<input type="text" id="msg" name="messages[]" value="<?php echo $msg->message_id;?>" />
<p><?php echo $msg->message;?></p>
</div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
<input type="button" value="check" onclick="check()">