I wonder whether someone could help me please.
I've been looking through this, and many other sites and tutorials to find out how to add a button to a form which opens a PHP file, in this case, a pop up form that allows a user to upload a file to a mySQL database.
In addition to the opening of the file, I'd like to carry over the 'id' field value from the main form to the pop 'File Upload' form.
From the research I've carried out there seems to be a number of ways to do this, but from a beginners perspective I'm not sure what is the best way to do this.
Could someone perhaps please advise on what is the best way to go about this.
Many thanks and kind regards
To pass values between pages:
Main form:
<form action="myuploadform.php" method="get">
ID: <input type="text" name="id">
<input type="submit" value="Open Form">
</form>
The value of the ID text box will be accessible as $_GET['id'] in myuploadform.php.
Using GET parameters is the simplest way of passing values. Another way to pass in this GET value would be in the URL:
.../myuploadform.php?id=35 where the ID then becomes 35.
Here's a sample from my site. All it does is allow the uploading of files to the server. It should serve as a tutorial.
<html>
<head>
<script type="text/javascript">
var form_object = null;
var button_object = null;
function submit_form(obj)
{
form_object = obj.parentNode;
form_object.submit();
form_object.disabled = true;
button_object = obj;
button_object.disabled = true;
}
function enable_form()
{
form_object.disabled = false;
button_object.disabled = false;
}
function Add_HTML(html)
{
if(navigator.appName == 'Microsoft Internet Explorer')
{
document.body.insertAdjacentHTML('beforeEnd', html);
}
//Firefox uses the Netscape engine (the Netscape version that really sucked)
if(navigator.appName == 'Netscape' && parseInt(navigator.appVersion) == 5)
{
var freaky_object = document.createRange();
freaky_object.setStartBefore(document.body);
html = freaky_object.createContextualFragment(html);
document.body.appendChild(html);
}
}
</script>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" target="upload">
<label>File:</label> <input type="file" name="file" />
<br />
<label>File:</label> <input type="file" name="swntic" />
<br />
<input type="button" value="SUBMIT"
onclick="submit_form(this);" />
</form>
<iframe src="about:blank" style="display:none;" id="upload" name="upload"></iframe>
</body>
</html>
server side code:
<?
$confirmation = "";
while(list($name) = each($HTTP_POST_FILES)) {
?>
<? if(is_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"])) { ?>
<?= $HTTP_POST_FILES[$name]["name"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["type"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["tmp_name"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["error"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["size"] ?>
<br /><br />
<? } ?>
<?
if(is_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"]))
{
move_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"], "./uploads/" . $HTTP_POST_FILES[$name]["name"]);
chmod("./uploads/" . $HTTP_POST_FILES[$name]["name"], 0644);
$confirmation .= "<a href=\"./uploads/" . $HTTP_POST_FILES[$name]["name"] . "\">" .
$HTTP_POST_FILES[$name]["tmp_name"] . "</a> " . $HTTP_POST_FILES[$name]["type"] . ", " . $HTTP_POST_FILES[$name]["size"] . " bytes<br />";
}
}
?>
<html>
<script>
var confirmation = '<?= $confirmation ?>';
</script>
<body onload="parent.enable_form(); parent.Add_HTML(confirmation);">
</body>
</html>
It's not perfect, but can be used as a learning tool.
Related
I am working on a program that has the user type in their course, first name, last name, and description of a program. The code is mostly done except for getting the clear the array button to work. When I use the unset array to clear the array on its own, it works but then the user cant enter in more data. I want to have the user be able to clear the data. Here is my code:
<?php
session_start();
?>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "gethint.php?q="+str, true);
xmlhttp.send();
}
}
</script>
<?php
function clear(){ //this is the problem
unset($_SESSION['courses']);
return true;
}
?>
</head>
<body>
<form method="POST">
Course: <input type="text" name="courses" />
<br /><br />
First Name: <input type="text" name="firstname" />
<br /><br />
Last Name: <input type="text" name="lastname" />
<br /><br />
Description: <input type="text" name="description" />
<br /><br />
<input type="submit" name="submit" value="Submit">
</form>
<?php
// First we check if the form has been sent and we have a value
if (!empty($_POST['courses'])) {
if (!isset($_SESSION['courses']))
$_SESSION['courses'] = array(); // Initialize the array if it doesn't exist
// Add the value to our array
$_SESSION['courses'][] = array("course" => $_POST['courses'],
"firstname" => $_POST['firstname'],
"lastname" => $_POST['lastname'],
"description" => $_POST['description']);
}
// If there are values to show, print them!
if (!empty($_SESSION['courses'])) {
foreach ($_SESSION['courses'] as $course) {
echo $course['course']." ".
$course['firstname']." ".
$course['lastname']." ".
$course['description']." ".
"<br />";
}
}
?>
<input type="submit" name="Clear" value="Clear" onclick="clear()"> //this is the problem
<?php
?>
</body>
</html>
Can someone please help?
<?php
// there is nothing wrong with this function.
function clear() {
unset($_SESSION['courses']);
return true;
}
?>
Okay, this function is fine, there is nothing wrong with it. But, you can't use this function like:
<input type="submit" name="Clear" onclick="Clear()" /> <!-- this is the problem -->
You see that onclick="Clear()", and that php function clear()? Yeah, you can't execute php functions with a html onclick="". You can only do that with javascript functions.
But you can do something like this:
<?php
if(isset($_POST['Clear']))
{
// if the user submits the form, then the following code will be executed.
clear();
}
?>
<input type="submit" name="Clear" value="Clear" onclick="clear()">
clear() would be calling a javascript function. You have correctly written a php function.
Check the value of the submit button "Clear" to be "clear" and if true run the PHP function clear().
if ($_POST['Clear'] === 'clear') {
clear();
}
So I want to replace the form submission with a thank you message after you submit, I need the PHP because this will eventually deal with databases in that php, however right now... The only way it works, is that is Type in a name, press submit. It goes back to a blank form, enter nothing (nothing in address) and submit again and it works...
right now the only way i could think of making it work would be some dummy checkbox where when checked value changes the post is sent. However i don't think that will pass with my groupmates
wondering how i can make it only have to submit once.
Index.PHP
<!DOCTYPE HTML>
<html>
<head>
<?php include_once "thankyou.php"; ?>
<script type="text/javascript" src="jquery-3.1.0.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$("#ContactUs_Submit").click(function(evt) {
<?php $inputName = $_POST["ContactUs_Name"]; ?>
$("#ContactUs_CommentsDiv").replaceWith("<?php
thankyou($inputName); ?>");
return false;
});
});
</script>
</head>
<body>
<div id="ContactUs_CommentsDiv">
<form method="post">
<!-- WITH JQUERY USE SINGLE URL, WITH PAGES-->
<label for="ContactUs_Name">Name: </label>
<input type="text" name="ContactUs_Name" id="ContactUs_Name" />
<br/>
<Label for="ContactUs_Email">Email: </Label>
<input type="email" name="ContactUs_Email" id="ContactUs_Email" />
<br/>
<input id="ContactUs_Submit" type="submit">
</form>
</div>
</body>
</html>
thankyou.php
<?php
function thankyou($name) {
echo "<p> Thank you for your input ";
//if ($_POST["ContactUs_Name"] != "") {
// echo " " . $_POST["ContactUs_Name"];
// }
if ($name != ""){
echo $name;
}
echo "!";
}
?>
Can you not just use AJAX to do this?
I made this simple chat site for my website but I don't know how to make it auto refresh every time a message has been sent.
Site that sends and prints out all messages:
<form action="messages.php" method="POST">
<input name="chat_box" /><br>
<input type="submit" value="Send" />
</form>
<?php
include "messages.txt";
?>
Site that sends text input to a text file:
<?php
$messages = $_POST["chat_box"];
$handler=fopen("messages.txt", 'a');
fwrite($handler,$_SERVER["REMOTE_ADDR"].":".$messages."<br>");
fclose($handler);
header("Location: chat_box.php");
?>
Can anyone help me?
Try this code :
This is a messages.php :
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
<form action="messages.php" method="POST">
<input name="chat_box" /><br>
<input type="submit" value="Send" />
</form>
<?php
include "messages.txt"; //Uncomment this to check the autorefresh
echo "Auto refresh in 10 second!";
$messages = $_POST["chat_box"];
$handler=fopen("messages.txt", 'a');
fwrite($handler,$_SERVER["REMOTE_ADDR"].":".$messages."<br>");
fclose($handler);
?>
</body>
</html>
Hope this help you out... :)
If you mean to get new messages, your best bet is probably to re-load the text file every 10 seconds. to do so replace the php in the bottom of your 1st code set with this:
<div id="messages"></div>
<script type="text/javascript">
$(document).ready(function() {
function functionToLoadFile(){
jQuery.get('messages.txt', function(data) {
$("#messages").html(data)
});
}
setInterval(functionToLoadFile, 10000);
});
</script>
I have done a conditional redirect with javascript (depending of referer). However now I need to detect if user got redirected and then he clicked back button and got again to the page he has been redirected from. In this case I need no to redirect him again.
I have found a solution here - but I am failed to combine php and javascript properly, so there is always an error - How do I detect if a user has got to a page using the back button?
Code:
<form name="ignore_me">
<input type="hidden" id="page_is_dirty" name="page_is_dirty" value="0" />
</form>
<script language="javascript" type="text/javascript">
var dirty_bit = document.getElementById('page_is_dirty');
if (dirty_bit.value == '1') {
document.write("<p>Do Not Redirect</p>");
}
else {
<?php if((stristr($_SERVER['HTTP_REFERER'],"thoughts") != FALSE) { ?>
<?php $setupform = '<form id="form1" method="post" action="http://yahoo.com">'; ?>
<?php $submitform = 'document.getElementById(\'form1\').submit(); </form>'; ?>
<?php echo $setupform; ?>
<?php echo $submitform; ?>
<?php } ?>
}
function mark_page_dirty() {
dirty_bit.value = '1';
}
</script>
What is wrong here?
The error is that it simply doesn't redirect - it gives a blank page with code on it:
<form name="ignore_me">
<input type="hidden" id="page_is_dirty" name="page_is_dirty" value="0" />
</form>
<script language="javascript" type="text/javascript">
var dirty_bit = document.getElementById('page_is_dirty');
if (dirty_bit.value == '1') {
document.write("<p>My First JavaScript</p>");
}
else {
<form id="form1" method="post" action="http://yahoo.com">document.getElementById('form1').submit(); </form>
}
function mark_page_dirty() {
dirty_bit.value = '1';
}
I took a look at your code and here are some points:
When mixing PHP and JS, remember, that PHP code will be executed first, it will not read any of JS statements
You put a block of PHP code into if statement of JS.
Here is a possible solution for you:
PHP logic first:
$redir = false ;
if((stristr($_SERVER['HTTP_REFERER'],"thoughts"))) {
$redir = true ;
}
HTML form:
<form id="check" action="a.php" method="post">
<input type="hidden" name="durty_bit" value="1" />
</form>
JS check:
var check = getElementById("check") ;
if (check && check.durty_bit == 1){
document.write("<p>Do Not Redirect</p>");
} else {
<?php if ($redir){ ?>
document.write('<form id="form1" method="post" action="http://yahoo.com"> </form>');
document.getElementById("form1").submit() ;
<?php } ?>
}
I am trying to implement a very basic AJAX upload progress bar using the PECL uploadprogress extension. I have found this sample code which works across all browsers: http://svn.php.net/viewvc/pecl/uploadprogress/trunk/examples/. It uses iframes to write the updates to. I would like to get the updates and do some jquery to build a progress bar. Here is my code (I know I did not write in code to account for when the upload ends) client.php:
<?php
$id = md5(microtime() . rand());
?>
<!DOCTYPE html>
<html>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function getProgress(){
$.get("progress.php", {"ID":'<?php echo $id ?>'}, function(data){
console.log(data);
});
window.setTimeout(getProgress(), 5000);
}
</script>
<body>
<form onsubmit="getProgress()" target="_self" enctype="multipart/form-data" method="post">
<input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $id;?>" />
<label>Select File:</label>
<input type="file" name="file" />
<br/>
<label>Select File:</label>
<input type="file" name="file2" />
<br/>
<label>Upload File:</label>
<input id="submitButton" type="submit" value="Upload File" />
</form>
</body>
</html>
And progress.php:
<?php
if (function_exists("uploadprogress_get_info")) {
$info = uploadprogress_get_info($_GET['ID']);
} else {
$info = false;
}
$progress = ($info['bytes_uploaded']/$info['bytes_total'])*100;
echo $progress;
I error out and all that prints is 0's. Any ideas?
Try replacing
$progress = ($info['bytes_uploaded']/$info['bytes_total'])*100;
with
$progress = ($info['bytes_uploaded']*100)/$info['bytes_total'];
Both $info['bytes_uploaded'] and $info['bytes_total'] are integers, so division is not a float but is rounded down to a integer.