I am trying pass a parameter in action tag in form, just the way I am doing in href tag but this isn't working can I know why? or should I just use a href tag in form, will that overwrite action in form?
Here is my code:
<!DOCTYPE html>
<html lang="en">
<?php
$ty=$_GET['param'];
$name=$_GET['param1'];
if($ty=='teacher')
{
$web = "<a href='teacherrepute.php?a=$name'>My repute score</a>";
$rep = "<a href='teacherreported.php?a=$name'>My reported sites</a>";
$blk = "<a href='newblocktryteacher.php?a=$name'>Block this site</a>";
$unblk = "<a href='newtryunblockteacher.php?a=$name>Unblock this site";
}
else
{
$web = "<a href='pupilrepute.php?a=$name'>My repute score</a>";
$rep = "<a href='pupilreported.php?a=$name'>My reported sites</a>";
$blk = "<a href='newblocktrypupil.php?a=$name'>Block this site</a>";
$unblk = "<a href='newtryunblockpupil.php?a=$name>Unblock this site";
}
// $type=$_GET['param2'];
$courseA='A';
$courseB='B';
?>
<body>
<?php echo $rep; ?>
<FORM action = <?php echo $blk; ?> method ="POST";>
Block : <input type ="text" name = "url" /></br>
<br>
<input type="submit" value="block" />
<br>
</FORM>
</body>
</html>
1) As you already creating link based on condition then you can directly echo that variable inside markup. e.g.
<?php echo $rep; ?>
2) Instead of passing html in form action just you pass that script name. e.g
newblocktryteacher.php?a=somename or newblocktrypupil.php?a=somename
Based on these two points your code will be
<!DOCTYPE html>
<html lang="en">
<?php
$ty=$_GET['param'];
$name=$_GET['param1'];
if($ty=='teacher')
{
$web = "<a href='teacherrepute.php?a=$name'>My repute score</a>";
$rep = "<a href='teacherreported.php?a=$name'>My reported sites</a>";
$blk = "newblocktryteacher.php?a=$name";
$unblk = "<a href='newtryunblockteacher.php?a=$name>Unblock this site";
}
else
{
$web = "<a href='pupilrepute.php?a=$name'>My repute score</a>";
$rep = "<a href='pupilreported.php?a=$name'>My reported sites</a>";
$blk = "newblocktrypupil.php?a=$name";
$unblk = "<a href='newtryunblockpupil.php?a=$name>Unblock this site</a>";
}
// $type=$_GET['param2'];
$courseA='A';
$courseB='B';
?>
<body>
<?php echo $rep; ?>
<form action="<?php echo $blk; ?>" method="POST">
Block : <input type="text" name="url" /></br>
<br>
<input type="submit" value="block" />
<br>
</form>
</body>
</html>
Your $blk isn't formatted in a way proper for <form>:
$blk = "<a href='newblocktryteacher.php?a=$name'>Block this site</a>";
<FORM action = <?php echo $blk; ?> method ="POST";>
Assuming $name is bob in this example:
<FORM action = <a href='newblocktryteacher.php?a=bob'>Block this site</a> method ="POST";>
As you can see, this is not correct. All you need is the URL itself. Also, remove the semicolon after "POST".
Your $blk is not a valid action form, because it's a <a> tag(link). So, $blk should be newblocktrypupil.php?a=$name' instead.
Related
I have created a PHP programm which allows to load a random .json file on my computer. When I select the file I want to display, I get the data from the selected .json file. At the beginning, I used a default file which I replaced with a variable. Here is the PHP code :
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
$file_test = explode('.', $_FILES['file']['name']);
$file_ext= strtolower(end($file_test));
$expensions = array(".json");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a DBC or an XML file.";
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "<div style='font-size: 150%;'>". "File is valid, and was successfully uploaded.\n"."</div>";
} else {
echo "<div style='font-size: 150%;'>". "Oops, It seems you haven't uploaded a file ! Please make sure you have selected a .json file\n "."</div> <br>";
echo "<div style='font-size: 150%;'>". "Returning to Home...\n "."</div>";
header("Refresh: 3,URL=upload_file.php");
exit();
}
echo basename($_FILES['file']['name']);
?>
<?php
header("Refresh: 2,URL=json_content_extended.php?filename=".$uploadfile);
exit();
?>
This file contains the code where I declared the requested value :
<?php
$json_file = file_get_contents($_REQUEST["filename"]);
$jfo = json_decode($json_file, true);
?>
And here is the HTML part :
<!DOCTYPE html>
<html>
<head>
<title>Menu</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data" >
<div class="test">
<input type="file" accept=".json" name="file"/>
<button class="up" name="click" class="click">Submit file</button>
</div>
</form>
</body>
</html>
This is just an example ! What I actually want to do is the same thing in Javascript. I started the script with a default file, but now I would like to replace this file (default.json) with a variable. I use $.getJson. Here is the code :
$(document).ready(function() {
var files = ["default.json"];
$.getJSON(files, function(json) {
var tr0, tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9;
tr0 = $('<tr/>');
tr0.append("<td>" + '<input class="expand" name="bt0" type="button" value="+" />' + "</td>");
tr0.append("<td>" + json.messages[0].id + "</td>");
tr0.append("<td>" + json.messages[0].name + "</td>");
tr0.append("<td>" + json.messages[0].is_extended_frame + "</td>");
I wonder if there is somehow a possibility to set a variable instead of the file name. I would like to do the same thing I did in the PHP example, but this time in javascript.
I am getting a strange double quote inserted into my HTML from my PHP script. I have tried all I know to prevent it but am at a loss'
Here is the code
<?php
$Client_number =$_GET['client_number'];
$update_type =$_GET['input_type'];
$class_no = $_POST['class_name_sel'];
echo '<div class ="enter_stu_info">';
echo '<h2>Creating the following student records</h2>';
if ($update_type == "reg")
{
require_once('../connections/i_connect.php');
$stu_line_count = 1;
$stu_temp = ($_POST['stu_first_name_'.$stu_line_count]);
while($stu_temp!= ""){
$first_name = $_POST['stu_first_name_'.$stu_line_count];
$stu_surname = $_POST['stu_last_name_'.$stu_line_count];
$employer_number =$_GET['client_number'];
$reg_stu_query = mysqli_query($i_connect, "INSERT INTO is_student(first_name, last_name, class_no, employer) VALUES('$first_name', '$stu_surname', '$class_no','$employer_number')");
echo 'First Name: '.$first_name.' Last name:'.$stu_surname.' Class_number: '.$class_no.' Client_number: '.$employer_number.' ';
$stu_line_count++;
$stu_temp = ($_POST['stu_first_name_'.$stu_line_count]);
}
if ($reg_stu_query){echo ' <h3>Entered</h3><br/>';}
}
if ($update_type == "blank_stu")
{
require_once('../connections/i_connect.php');
$no_of_students = $_POST['no_of_students'];
$stu_line_count = 1;
$employer_number =$_GET['client_number'];
while($stu_line_count <= $no_of_students){
$is_entered = mysqli_query($i_connect, "INSERT INTO is_student(class_no, employer) VALUES('$class_no', '$employer_number')");
echo ' Class_number: '.$class_no.' Client_number: '.$employer_number.' Student number: '.$stu_line_count;
if ($is_entered == "true"){echo ' <h3>Entered</h3>';}
echo'<br/>';
$stu_line_count++;
}
}
$win_loc_txt = '"create_student_enter.php?client_number='.$employer_number.'&class_no='.$class_no.'"';
echo'<br/>'.$win_loc_txt.'<br/>';
echo '<input name="more_stu_button" type="button" onClick="window.location.replace('.$win_loc_txt.')" value="Add more students">';
echo '</div>';
?>
This gives me the following result(note the unwanted double quote in the "window.location.replace() script between the "client_number=" and the "7" even though the echo of the variable used does not show the unwanted quotation mark..
<!DOCTYPE html>
<html>
<head>
<body>
<div class="enter_stu_info">
<h2>Creating the following student records</h2>
First Name: testing Last name:testing Class_number: 1 Client_number: 7
<h3>Entered</h3>
<br>
<br>
"create_student_enter.php?client_number=7&class_no=1"
<br>
<input name="more_stu_button" onclick="window.location.replace(" create_student_enter.php?client_number="7&class_no=1")"" value="Add more students" type="button">
</div>
<script type="text/javascript" language="javascript">
</body>
You're getting it because you're putting it there:
$win_loc_txt = '"create[...snip..] .'"';
^--------------------^
echo '<input [..snip..] onclick="window.location.replace('.$win_loc_txt.')" value= [..snip..]
When you put them together:
echo '<input [..snip..] onclick="window.location.replace("create...");" value=[.snip..]
^------------------------^ ^--unknown/illegal attribute
Since you're prematurely terminating your onclick attribute, the browser is forced to try and GUESS what you intended and fix things as best it can.
You're dumping text from PHP directly into a Javascript context, inside an HTMl attribute, which means you have to generate valid javascript AND html, simultaneously.
Try something like
echo '<input ...' . htmlspecialchars(json_encode($win_loc_text)) . '...';
instead.
The issue was that you were using " within a ", so your code should really be:
<!DOCTYPE html>
<html>
<body>
<div class="enter_stu_info">
<h2>Creating the following student records</h2>
First Name: testing Last name:testing Class_number: 1 Client_number: 7
<h3>Entered</h3>
<br>
<br>
"create_student_enter.php?client_number=7&class_no=1"
<br>
<input name="more_stu_button" onclick="window.location.replace(' create_student_enter.php?client_number=7&class_no=1');" value="Add more students" type="button">
</div>
<script type="text/javascript" language="javascript">
</body>
I have a form in html:
<form>
<label><input type="hidden" name="pNameChange" value=""></label>
</form>
and I want to get the value of this input in php without submitting it in a form.
this is my javascript:
var pName= null;
$(document).ready(function(){
$('img').click(function(){
pName= $(this).attr("name");
console.log(pName);
});
});
My php:
$pName = isset($_POST['pNameChange']) ? $_POST['value'] : '';
what I want is. you click on the picture,
1.the value of the name attribute of the picture is going to be saved into the variable pName (javascript),
2.it then goes into the form and changes the value of the form to the variable pName (javascript),
3.php picks up the value of the form (which should now be equal to pName),
4.then stores it into a variable $pName (php).
5.I also want $pName (php) to be globally used throughout all the pages of the website.
edit
this is my index page:
<?php
$pName = isset($_POST['pNameChange']) ? $_POST['value'] : '';
$db_connection = mysqli_connect('localhost','root','',"project_online_planner");
if (!$db_connection){
die('Failed to connect to MySql:'.mysql_error());
}
$query="SELECT * FROM project limit 5 ";
$results = mysqli_query($db_connection,$query);
$intro=mysqli_fetch_assoc($results);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Project planner online</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="ppo.js"></script>
<link rel="stylesheet" href="ppo.css"/>
</head>
<body>
<div id="bgNav">
<div id="login">
Register
Log in
</div>
<nav id="nav">
Home
</nav>
</div>
<h2 class="titlePage">Home</h2>
<div id="bgTile">
<?php
while($row = mysqli_fetch_array($results))
{
$project = $row["name"];
echo nl2br("<a href='project.php'>" ."<img name=\"$project\" width='100px' alt='Procject name' height='100px' class='tile' src=". $row['image'] ."/>". "</a>");
}
?>
<div class="tile" id="tileM"><h2>Meer</h2></div>
</div>
<form>
<label><input type="hidden" name="pNameChange" value=""></label>
</form>
</body>
</html>
what I want: click on the image then you get sent to the project page where (php) $pName is equal to the value of (javascript) pName
project page:
<?php
$newRecord = null;
$pName = isset($_POST['pNameChange']) ? $_POST['value'] : '';
$db_connection = mysqli_connect('localhost','root','',"project_online_planner");
if (!$db_connection){
die('Failed to connect to MySql:'.mysql_error());
}
//insert into database
if(isset($_POST['insertComments'])){
include('connect-mysql.php');
$username = $_POST['username'];
$comment = $_POST['comment'];
$sqlinsert = "INSERT INTO user_comments (username, comment, project) VALUES ('$username', '$comment', '$pName')";
if (!mysqli_query($db_connection, $sqlinsert)){
die('error inserting new record');
}
else{
$newRecord = "1 record added";
}//end nested statement
}
//text from database
$query="SELECT * FROM user_comments where project = '$pName' ";
$results = mysqli_query($db_connection,$query);
$intro=mysqli_fetch_assoc($results);
$query2="SELECT * FROM project where name = '$pName' ";
$results2 = mysqli_query($db_connection,$query2);
$intro2=mysqli_fetch_assoc($results2);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Project planner online</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="ppo.js"></script>
<link rel="stylesheet" href="ppo.css"/>
</head>
<body>
<div id="intro">
</div>
<div id="bgNav">
<nav id="nav">
Home
<a class="rightNav" href="register.php">Register</a>
<a class="rightNav" href="login.php">Log in</a>
</nav>
</div>
<div id="projectTile">
<span id="statusCheck"><?php print_r($intro2["status"]); ?></span>
<h2 id="prTitle"><?php print_r($intro2["name"]); ?></h2>
<div id="prPic"><img width="300" height="200" src="<?php print_r($intro2["image"]); ?>"></div>
<div id="prDescription"><?php print_r($intro2["description"]); ?></div>
</div>
<div id="comments">
<?php
while($row = mysqli_fetch_array($results))
{
echo nl2br("<div class='profile_comments'>" . $row['username'] . "</div>");
echo nl2br("<div class='comment_comments'>" . $row['comment'] . "</div>");
}
?>
</div>
<div id="uploadComments">
<form method="post" action="project.php">
<label for="name"><input type="hidden" name="insertComments" value="true"></label>
<fieldset>
<legend>comment</legend>
<label>Name:<input type="text" id="name" name="username" value=""></label><br/>
<label>Comments: <textarea name="comment" id="comment"></textarea></label>
<input type="submit" value="Submit" id="submitComment">
</fieldset>
</form>
</div>
<?php
echo $newRecord;
?>
<form>
<label><input type="hidden" name="pNameChange" value=""></label>
</form>
</body>
</html>
HTML:
do you have more then 1 image on page? its better if you add ID in image. No need for form and hidden fields for what you want done.
make sure your img has ID like <img id="imageID"...
JavaScript:
var pName= null;
$(document).ready(function(){
$('#imageID').click(function(){
pName= $(this).attr("name");
$.post("project.php", { pNameChange: pName },
function(data) {
// do something here.
});
});
});
above code should work as expected. Now in project.php > $_POST['pNameChange'] should receive the value of pName (image's name attr).
I don't understand what you want when you said $pName available globally on all pages. Please elaborate further, may be look into storing it as cookie/session?
EDIT:
Consider using session to pName value... by simple starting/resuming session in start of file:
<?PHP
session_start();
and then...
to set/update value:
if(isset($_POST["pNameChange"]))
$_SESSION["pName"] = $_POST["pNameChange"];
and then use $_SESSION["pName"] instead of $pName on all pages.
Try Ajax method in jQuery , hope it solves your problem
https://api.jquery.com/jQuery.ajax/
or
https://api.jquery.com/jQuery.post/
Actually, you do not need AJAX for this, all your index.php does it passes the image's name to project.php so try:
In index.php:
<form name='form1' method="post" action='project.php'> <!-- form attributes given -->
<label><input type="hidden" name="pNameChange" value=""></label>
</form>
Javascript:
var pName= null;
$(document).ready(function(){
$('img').click(function(){
//onclick of image, we will save the image name into the hidden input
//and submit the form so that it goes to project.php
$('input[name="pNameChange"]').val($(this).attr("name"));
$('form1').submit()
});
});
And in your project.php:
//now project.php can get the posted value of 'pNameChange'
//There is no input field with `name`->`value`, so $_POST['value'] is invalid.
$pName = isset($_POST['pNameChange']) ? $_POST['pNameChange'] : '';
And if you need this value across multiple pages, global will not work, use sessions.
ok this "additional" answer is to focus on session only. use the codes for client-end from my previous answer and try this on server-end.
index.php Page:
<?php
session_start();
$pName = isset($_SESSION['pNameChange']) ? $_SESSION['pNameChange'] : '';
project.php Page:
<?php
session_start();
$newRecord = null;
$pName = isset($_SESSION['pNameChange']) ? $_SESSION['pNameChange'] : null;
if(is_null($pName) && isset($_POST['pNameChange'])) {
$_SESSION['pNameChange'] = $_POST['pNameChange'];
$pName = $_POST['pNameChange'];
}
hope it helps
I have some HTML Template and I want show them in iFrame, some of this Template have 2 to 11 colors variety.
folder name ex.
1
2
3
3-1
3-2
3-3
3-4
3-5
4
5
6-1
6-2
7
and more...
I have a botton for show NEXT template.
I want use is_dir in php to check if directory 1 or 3-2 is available then show it.
<?php
$id = $_GET['id'];
$c=$id+1;
$c1=$c."-1";
$xm=$c;
$xm1=$c1;
if (is_dir($xm)) {
$c=$c;
}
if (is_dir($xm1)) {$c=$c1;}
?>
<div id="header-bar">
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="id" value="<?php echo $c ?>">
<input type="submit" value="NEXT">
</form>
</div>
<iframe src="http://barg.ir/demo/<?php echo $id; ?>"></iframe>
Question1: I can show folder 3-1 but I cant show folder 3-2 and 3-3 and..., how do this?
Question2: can coding with php array?
Question3: can coding with JavaScript? what is equal is_dir in javascript?
Why don't you look into using glob()? Put the following into the folder you want to get the information from:
// we'll call it grabber.php
<?php
$files = glob('*', GLOB_ONLYDIR); sort($files, SORT_NATURAL);
?>
// now you can include grabber.php in your form file
<?php
session_start(); include_once 'PATH/grabber.php'; $end = count($files)-1;
<div id='header-bar'>
<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<input type='submit' value='BACK' name='back' />
<input type='submit' value='NEXT' name='next' />
</form>
if(isset($_POST['back'])){
$_SESSION['fileNum']--;
if($_SESSION['fileNum'] < 0)$_SESSION['fileNum'] = $end;
}
elseif(isset($_POST['next'])){
$_SESSION['fileNum']++;
if($_SESSION['fileNum'] > $end)$_SESSION['fileNum'] = 0;
}
else{
$_SESSION['fileNum'] = 0;
}
$f = $files[$_SESSION['fileNum']];
echo " <iframe src='http://barg.ir/demo/$f'></iframe>".
'</div>';
?>
Obviously PATH needs to be changed.
Better yet use JavaScript:
// You'll still need `grabber.php` in the same location as before, only now
// we'll actually make the PHP page into a String for JavaScript usage, like:
<?php
$dirs = glob('*', GLOB_ONLYDIR); sort($dirs, SORT_NATURAL);
$dirsJS = implode("', '", $dirs); // implode into a String for JavaScript Array
echo "//<![CDATA[
var doc = document, bod = doc.body;
bod.className = 'js'; // use .njs class in CSS for users without JavaScript
function E(e){
return doc.getElementById(e);
}
function direct(backId, nextId, iframeId, iframeSrcBase){
var dirs = ['$dirsJS'];
var dl = dirs.length-1, n = 0, f = E(iframeId), s = iframeSrcBase;
f.src = s+dirs[0];
E(backId).onclick = function(){
if(--n < 0)n = dl;
f.src = s+dirs[n];
}
E(nextId).onclick = function(){
if(++n > dl)n = 0;
f.src = s+dirs[n];
}
}
//]]>";
?>
/*
Now back to your main page. I like XHTML, but you can use whatever.
The reason for JavaScript is to avoid scrolling issues and page flashing
This is your main page again without as much PHP. There's no need to
include the other file in PHP, or use a session. We use the `script`
tag instead. Pay attention:
*/
<?php
echo "<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<style type='text/css'>
#import 'yourCSS.css';
</style>
</head>
<body class='njs'>
<div id='header-bar'>
<form method='post' action='{$_SERVER['PHP_SELF']}'>
<input type='button' value='BACK' name='back' id='back' />
<input type='button' value='NEXT' name='next' id='next' />
</form>
<iframe id='ifr' src=''><noscript>Your Browser Does Not Support JavaScript</noscript></iframe>
</div>
<script type='text/javascript' src='PATH/grabber.php'></script>
<script type='text/javascript'>
direct('back', 'next', 'ifr', 'http://barg.ir/demo/');
</script>
</body>
</html>";
?>
Once again, this time in the script tag, change PATH accordingly.
I have a problem in my code, I want to put $meaning variable into innerhtml of my di,here is my code:
searchinput.php:
<form action = "search.php" method = "post">
<input name="word" id="word" type="text" maxlength="255" />
<label for="word">word:</label></br></br>
<input type="submit" value="search" />
</form>
<div id = "meaning"> </div>
search.php:
<?php
$db = mysql_connect("localhost", "root", "");
mysql_select_db("project",$db);
$word = isset($_POST["word"]) ? $_POST["word"] : "";
$result = mysql_query("select meaning from vocabulary where word = '$word'");
$fetchmeaning = mysql_fetch_array($result);
$meaning = $fetchmeaning['meaning'];
?>
now I want to have this:
document.getElementById('meaning').innerhtml = $meaning; !!!!
how can I impelemt this?
Yes. Start a script tag after the definition of $meaning and put the below code in it.
Like.
document.getElementById('meaning').innerHTML = "<?PHP echo $meaning; ?>" ;
Also Can't you think of directly echo ing the value of $meaning inside the div without even using javascript? like
<div id = "meaning"><?PHP echo $meaning; ?></div>
You can also use the short form <?=$meaning?>.
<div id="errorMessage"></div>
<?php
if (isset($_GET['q'])) {
echo '<script type="text/javascript">
document.getElementById("errorMessage").innerHTML = "User name or Password is incorrect";
</script>';
}
?>