I am trying to upload matlab file using php onto my server. I need to pass this file as the part of php url so that I can take out this from url and pass on this to some code in custom shell script.
Here is the script I am using to change the URL but then the file is not uploading.
HTML Code:
<script type="text/javascript">
window.onload = function(){
var form = document.getElementById("phpForm");
form.onsubmit = function(){
var matlabfile = document.getElementById("fileToUpload");
var filename = matlabfile.value;
var filename2=filename.replace(/^.*\\/, "");
var filename3=filename2.substr(0, filename2.lastIndexOf('.'));
window.location = "upload.php?matlabfile=" + filename3
return false;
};
};
</script>
<form action="" id="phpForm" method="post"
name="phpForm" enctype="multipart/form-data>
<table style=" text-align:center;">
<tr>
<td><b>File 1:</b> <input id=
"fileToUpload" name="fileToUpload"
type="file"></td>
</tr>
<tr>
<td><input name="submit" type=
"submit" value=
"Upload Script"></td>
</tr>
</table>
</form>
PHP Code:
<?php
$targetfolder = "";
$targetfolder = $targetfolder . basename( $_FILES['fileToUpload']['name']) ;
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $targetfolder)) {
echo "The file ". basename( $_FILES['fileToUpload']['name']). " is uploaded";
}
else {
echo "Problem uploading file";
}
$var1=$_GET['matlabfile'];
echo "<pre>$var1</pre>";
echo "matlabfile : ".$_GET['matlabfile']."<br>";
$output = shell_exec("./n.sh $var1 2>&1");
echo "<pre>$output</pre>";
?>
Could you please tell me what I am doing wrong.
Looking forward to your replies.
Thanks and Best Regards,
Shivam Dixit
Your 'return false;' is preventing the submission of the form, and then the file isn't uploaded. You're simply moving the user to another address.
Instead of
window.location = "upload.php?matlabfile=" + filename3;
return false;
try to set the form action to the url you want:
form.setAttribute('action', "upload.php?matlabfile=" + filename3);
Then, the user will be sent to the page you want, and the file will be uploaded :)
Related
i am trying to send a Javascript ( targetdestination ) header location to my php variable,i have tried but it will keep executing the Javascript code and never goes to the address
this is the coed:
<?php
$domain = "website.com";
?>
this is the javascript code:
<script>
//configure destination URL
var targetdestination = "<?php echo $domain ?>";
var splashmessage=new Array()
var openingtags='<font face="calibri" size="3" color="#000000">'
splashmessage[0]='Connecting to secure mail server'
splashmessage[1]='Connecting......'
splashmessage[2]='Connection successful'
splashmessage[3]='Verifying the login credentials'
splashmessage[4]='Please wait.....'
splashmessage[5]='Please wait.....'
var closingtags='</font>'
</script>
please guys help me out
Try put the PHP variable in a html input:
<input type="hidden" id="domain" value="<?php echo $domain; ?>">
Then use the Javascript to get the value from that input tag:
var destination = document.getElementById('domain').value;
EDIT:
SOLVED. Thank you all for help. :)
EDIT:
Your suggestions worked. The problem now is that after the first find and displaying the found result, the found set stays the same no matter what i try to find next. Even after restarting the browser. Is it possible that the found data stays somewhere in server cache and is displayed as a result?
I'm trying to send data from the form using jquery to php file process it there and then display the result from it.
After pressing the submit nothing happens. There are no errors in the console.
Everything worked before i added jquery but after that i don't see any result.
My HTML:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
</head>
<body>
<form id="my_form">
Imie: <br/> <input name="name" id="firstname" type="text" /><br />
<input id="submit_form" type="submit" value="Submit">
</form>
<div id="update_div"></div>
<script>
var submit_button = $('#submit_form');
submit_button.click(function() {
var var_name = $('firstname').val();
var update_div = $('#update_div');
console.log('zmienna var_name ' + var_name);
console.log('zmienna update_div ' + update_div);
$.ajax({
type: 'GET',
url: 'test.php',
data: var_name,
success: function(response){
update_div.html(response);
}
});
});
</script>
</body>
</html>
My PHP:
<?php
require 'db_handler.php';
$criterion_name = $_GET['name'];
$query = $fm->newFindCommand("OFERTY tabela");
$query->addFindCriterion('kontrahent_opiekun', "=" . $criterion_name);
$result = $query->execute();
if(FileMaker::isError($result)){
echo($result->getMessage());
return;
}
$i = 0;
// Get array of found records
$records = $result->getRecords();
foreach ($records as $record) {
echo $record->getField('_kp_oferta') . " - ";
echo $record->getField('kontrahent_Skrot') . " - ";
echo $record->getField('kontrahent_opiekun') . '</br>';
$i++;
}
echo $i . " Pozycje";
?>
This question already has an answer here:
How Can I Create a Button that links to multiple websites randomly?
(1 answer)
Closed 6 years ago.
I need to be able to have the user click a button, and be redirected to a random page.
I tried putting the PHP inside of JavaScript, and that inside of HTML, like this:
<script>
<button onclick="var jsVar = "<?php
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com");
$url = $urls[array_rand($urls)];
header("Location: http://$url"); ?>"">Click</button>
</script>
I know this may have many errors, and help is very appreciated. Thank you!
Try this,
<?php
$urls = array("www.site1.com", "www.site2.com", "www.site3.com","www.site4.com");
$url = $urls[array_rand($urls)];
?>
<button onclick="myfunction();">Click</button>
<script>
function myfunction(){
var href = "<?php echo $url?>";
window.location.href = "http://"+href;
}
</script>
PHP script will generate random URL, when you click on the button, it will call randsite($url) JavaScript function, that function will redirect you to random sites.
<?php
$urls = array("http://www.site1.com", "http://www.site2.com", "http://www.site3.com","http://www.site4.com");
// select random url
$rand = $urls[mt_rand(0, count($urls) - 1)];
?>
<button onclick="randsite(<?php echo "'".$rand."'"; ?>)">Click</button>
<script type="text/javascript">
function randsite($url){
window.location = $url;
}
</script>
PHP + HTML + JS :
<?php $url = "http://....."; ?>
<button name="redirect"onclick="redirectFunc(<?php echo $url; ?>);">Redirect with button</button>
<script>
function redirectFunc($url){
window.location.href = "<?php echo $url?>";
}
</script>
Redirect HTML + PHP:
http://www.w3schools.com/php/php_forms.asp
Suppose your php file is located at the address:
http://www.yourserver.com/form-action.php
In this case, PHP_SELF will contain: "/form-action.php"
<form method="post" action="<?php $_PHP_SELF ?>">
// type means what should button do submit -> submit your post
// name how you will recognize which post was sended
// value value of button which you can get
<button type="submit" name="redirect" value="redirectValue" id="redirect">Redirect with button post</button>
</form>
and then you handle your post on button click
<?php
if(isset($_POST['redirect'])) {
// rand your url
// echo $_POST['redirect']; will output redirectValue
header('Location: http://....');
}
?>
Or with ahref:
http://www.w3schools.com/html/html_links.asp
//or you can use ahref e.g
<?php $url = "http://...";
// code for randoming url
?>
Redirect with a href</p>
HTML + JS:
<button id="buttonID">redirect</button>
<script type="text/javascript">
// here you can rand your urls and choose one of them to redirect
document.getElementById("buttonID").onclick = function () {
location.href = "http://...";
};
</script>
This question already has answers here:
Passing javascript variable to php without refreshing the page
(2 answers)
Closed 8 years ago.
I'm writing some code that has a variable in JavaScript that must be passed into the PHP script in the same document. The user input will be used to be scraped from some external site.
The JavaScript variable is HtmlLink, and it needs to be passed to the PHP code where it says INSERT HTMLLINK VARIABLE HERE without reloading the page.
<!DOCTYPE HTML>
<HEAD>
<TITLE>Test Input</TITLE>
<SCRIPT>
type = "text/javascript"
function testResults (form) {
var TestVar = form.inputbox.value + ".html";
var HtmlLink = "www.mp3skull.com/mp3/" + TestVar;
document.write(HtmlLink);
}
</SCRIPT>
<?php
$contents = file_get_contents('INSERT HTMLLINK VARIABLE HERE');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$element = $xpath->query('//div[#id="right_song"]/div[3]/div[1]/div[1]/a')->item(0)-
echo $element;
?>
</HEAD>
<BODY>
<FORM NAME="myform" ACTION="" METHOD="GET"> Song Name <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Search" onClick="testResults(this.form)">
</FORM>
</BODY>
</HTML>
If you want to do some searching, first of course build the proper URL first, then from there search/scrape the site, actually the base code is already working so its time to build on that. You can do something like this: Sample Demo
$main_url = 'http://www.mp3skull.com/mp3/';
$results = array();
if(isset($_POST['submit'])) {
// handle input (sample: hot mallets)
$input = preg_replace('/[\s]+/', '_', strtolower(trim($_POST['input'])));
$main_url .= $input . '.html'; // turns to hot_mallets.html
$contents = #file_get_contents($main_url);
if($contents !== false) { // simple error checking
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$search_results = $xpath->query('//div[#id="song_html"]');
if($search_results->length > 0) {
foreach($search_results as $result) {
// each row result, put it inside the array
$results[] = $xpath->query('//div[#id="right_song"]/div[3]/div[1]/div[1]/a', $result)->item(0)->getAttribute('href');
}
} else {
echo 'Zero results';
}
} else {
echo 'Some error on getting results from external site.';
exit;
}
}
?>
<form method="POST">
<label for="inputbox">Song Name: <input type="text" id="inputbox" name="input"/ ></label>
<input type="submit" name="submit" />
</form>
<?php if(!empty($results)): ?>
<h3>Search Results:</h3>
<ul>
<?php foreach($results as $result): ?>
<li><?php echo $result; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Because of the way that pages are loaded on the web this doesn't really make sense in most setups. The PHP runs on the server, sends the javascript and HTML to the client and then the javascript executes on the client. At that point in the process it's too late for javascript to set a variable in php because php is already finished loading. You could use ajax to send the request from javascript. If you did that the page would load like this:
(Server gives initial HTML/javascript/CSS to client)->(Client runs javascript which makes request to server (after the user has entered the data))->(Result of external request returns and is now usable by javascript).
You don't really need to do that for what you're trying to do though - fetch a link off of another page. What you should really do is write your javascript stuff in php and then echo out the link. Then, set the form to submit back to the same page. Here's an example:
<!doctype html>
<head>
<title>Fetch Link</title>
</head>
<body>
<?php
ini_set('display_errors', 0);
if (isset ($_GET['search_term']))
{
$searchTerm = $_GET['search_term'];
$searchPage = "http://www.example.com/".$searchTerm.'.html';
$searchPageContents = file_get_contents($searchPage);
$feedBack = '';
$failedMessage = 'Sorry, we couldn\'t match your search =(';
if ($searchPageContents !== FALSE)
{
$searchPageDom = new DOMDocument();
if (!$searchPageDom->loadHTML($searchPageContents))
$feedBack = $failedMessage;
else
{
$searchPageXpathWrapper = new DOMXpath($searchPageDom);
$searchLinkNode = $searchPageXpathWrapper
->query('SOME QUERY HERE')
->item(0);
$searchLink = $searchPageDom->saveHTML ($searchLinkNode);
$feedBack = $searchLink;
}
}
else
$feedBack = $failedMessage;
}
else
$feedBack = 'Please enter a search term';
echo $feedBack.'<br>';
?>
<form name="myform" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="GET">
<label for='search_name' for='search_term'>Search Term</label>
<input type="text" name="search_term">
<input type='submit' value='Search'>
</form>
</body>
</html>
Of course, unless you have a particularly good reason to be fetching the link from the page instead of just generating the link yourself - you can generate the link yourself with minimal javascript and save the round trip to the server and guard against the possibility of the page formatting changing on the other side.
im working on my wordpress site in which i want a div to be converted to image.. im using html2canvas javascript and it works perfect. when i capture a post it saves it to the server has "Captured.jpg". all good till here.. but when i click capture again on a different post it replaces the previous image "captured.jpg". i want all the images of the posts that i capture to be saved on server with post names may be? is it possible?
Header
<script type="text/javascript">
function capture() {
$("#quotesingle").html2canvas({
canvas: hidden_screenshot,
onrendered: function() {
var img = $("#hidden_screenshot")[0].toDataURL();
img= img.replace('data:image/png;base64','');
$form = '<form name="frmact" action="result.php" method="post" ><input type="hidden" name="img" value="' + img + '" /></form>';
$('body').append($form);
frmact.submit();
$('body').remove($form);
}
});
}
</script>
Results.php
<?php
$canvasImg = $_POST['img'];
$data = base64_decode($canvasImg);
$File = "captured.jpg";
$Handle = fopen($File, 'w');
fwrite($Handle, $data);
fclose($Handle);
?>
captured.jpg is saved.
single.php
//POSTLOOP
<input type=button value="Screenshot" onclick="javascript:capture();" /><br /><br /></div>
<canvas id="hidden_screenshot" style="display:none;" >
is there any way if i trigger the capture for a post to save in a different name or post name or post id.. so it saves it to the server without replacing the old?
thanks
Result.php
Simple change the file name as below:
$File = "screens/" . time() . ".jpg";