How do I validate CSV file format using onclick function? - javascript

How can I validate my CSV format and I am using "required" in my html form to validate null value. I try to use onclick function to validate using alert box but alert box not function after I add php code inside javascript. Below is my code. Can anyone suggest a solution to me?
<?php
require_once "lib/base.inc.php";
?>
<script>
function storeQueEmail(){
<?php
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
$record['contact_first'] = $data[0];
$record['contact_last'] = $data[1];
$record['contact_email'] = $data[2];
$record['subject'] = $_REQUEST['subject'];
$record['message'] = $_REQUEST['message'];
$record['status'] = 0;
$oAdminEmail->insertQueEmail($record);
}
} while ($data = fgetcsv($handle,1000,",","'"));
?>;
alert('jyfkyugu');
window.location.href="cronjob_sendemail.php";
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" >
Subject : <br/>
<input type="text" name="subject" id="subject" required/> <br/>
Choose your file: <br />
<input name="csv" type="file"id="csv" required/> <br/>
Content : <br/>
<textarea name="message" cols="50" rows="10" required></textarea><br/>
<input type="submit" name="submit" value="Submit" onclick="storeQueEmail()"/>
</form>
</body>
</html>

Related

PHP Display Shell Command Output in a box in Real Time

I have the following code which asks for input then applies shell.sh $folderPath $bits $group2
And I would like to have a box where it displays the output of my script exactly how it would've looked if I ran in in a terminal.
How do I do that?
Below is my code.
<?php
if (isset($_GET['folderPath']) && !empty($_GET['folderPath']) && file_exists($_GET['folderPath'])
&& is_dir($_GET['folderPath']) && isset($_GET['bits']) && isset($_GET['group2'])) {
$folderPath = $_GET['folderPath'];
$bits = $_GET['bits'];
$group2 = $_GET['group2'];
$run = shell_exec("shell.sh $folderPath $bits $group2");
echo "shell.sh $folderPath $bits $group2";
var_dump($run);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<form action="">
<label for="folderPath">Folder Path:</label>
<input type="text" id="folderPath" name="folderPath">
<br></br>
<fieldset id="bits">
<p>Please Choose bits: </p>
<input type="radio" value="8bits" name="bits"> 8bits <br>
<input type="radio" value="16bits" name="bits"> 16bits <br>
</fieldset>
<br></br>
<fieldset id="audio-type">
<p>Please Choose Type: </p>
<input type="radio" value="C12" name="group2"> C12 <br>
<input type="radio" value="LR" name="group2"> LR <br>
</fieldset>
<input class = "submitButton" type="submit" value="Submit">
</body>
</html>
First of all, you need to send your form data somewhere. In your case, it is your same script.
<form method="GET" action="path/to/my/script.php">
Secondly, shell_exec() returns output of executed command. You already wrote the code.
$path = $_GET['folderPath'];
$bits = $_GET['bits'];
$group2 = $_GET['group2'];
$output = shell_exec("shell.sh $path $bits $group2");
Thirdly, display the output where you want.
<div><?php print $output; ?></div>

Save insert from to file

I have a input form and want to save the data that I haved insert into a file. Should I use get and set for this? Anyone have any ideas on what I should do?
function myfunction() {
if (validation()) // Calling Validation Function
{
// Serializing Form Data And Displaying It In <p id="wrapper"></p>
document.getElementById("wrapper").innerHTML = serialize(document.forms[0]); // Serialize Form Data
document.getElementById("form").reset(); // Reset Form Fields
}
}
function validation() {
var name = document.getElementById("namn").value;
var number = document.getElementById("number").value;
}
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Write</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="http://form-serialize.googlecode.com/svn/trunk/serialize-0.2.min.js" type="text/javascript"></script> <!-- For Serialization Function -->
<script src="hej.js"></script> <!-- Include JavaScript File Here-->
</head>
<body>
<div id="main">
<div id="login">
<hr/>
<form action="" method="post">
<label>Name:</label>
<input type="text" name="name" id="name" required="required" placeholder="fill in your name"/><br /><br />
<label>Number :</label>
<input type="text" name="number" id="number" required="required" placeholder="0876856"/><br/><br />
<input onclick="myfunction()" type="submit"id= "submit" value=" Submit " name="submit"/><br />
</form>
</div>
</div>
</body>
</html>
Thanks in advance!
If you use the normal $_POST you can access the data over $_POST and write it with fopen, fwrite to a file. Over ajax you have to send the Data to a PHP File and do the same there. You cant access the local filesystem over Javascript.
<?php
if(isset($_POST) && !empty($_POST){
// reformat your data here and format it
$yourDataToWriteInTheData = $_POST['firstname'] . '|' . $_POST['lastname'] . PHP_EOF;
// Open the file (or create it, read the fopen manual)
$fileHandler = fopen('file1.txt', 'a+');
fwrite($fileHandler, $yourDataToWriteInTheData);
fclose($fileHandler);
}

How to make a html tag id into php string

I'm now building up a project and it have some errors and I'm not able to find them so help me out.
I have created a html file name (name.html) and a php file name (result.php) I have added some of Java when we click on Submit button it will take us to the result.php and display the text on an image through php my coding is like this.
For (name.html)
<html>
<body>
<input type="text" id="name" name="myname" placeholder="E.g: abc">
<input type="button" id="next" name="submit">
</body>
</html>
For (result.php)
<? php
header("Content-type: image/jpeg");
$imgPath = 'image.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = $_GET["name"];
$fontSize = 3;
$x = 115;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
?>
The problem is that when I'm clicking over submit button the Script take me to the page result.php and the text inside the Input tag is not being displayed.
This is the example of working one
name.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="result.php" method="get">
<input type="text" id="name" name="myname" placeholder="E.g: abc">
<input type="submit" value="Submit">
</form>
</body>
</html>
result.php
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(300, 100);
$text_color = imagecolorallocate($im, 233, 14, 91);
$string = $_GET["myname"];
imagestring($im, 20, 5, 5, $string, $text_color);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
?>
Here's a little fix:
<html>
<body>
<form method="GET" action="result.php">
<input type="text" id="name" name="myname" placeholder="E.g: abc">
<input type="submit" id="next" name="submit">
</form>
</body>
</html>

Getting form value from PHP in JQuery

I want to get a form input value from a php file to a javascript one, like this:
PHP file:
<html>
<head>
<meta charset = "UTF-8">
<title> Search Customer by Field </title>
<script type="text/javascript" src="../lib/jquery-1.10.2.js"></script>
<script type="text/javascript" src="../src/search-by-invoice-no.js"></script>
<table id="results" border="1"></table>
</head>
<body>
<?php
if( isset($_GET["invoiceNo"]) && "" != $_GET["invoiceNo"])
{
}
else
{
?>
<form id = "form1">
Invoice Number <input id="invoice" name="invoiceNo" type="text" value="<?=isset($_GET['invoiceNo'])? $_GET['invoiceNo'] :""?>">
<br/>
<input id="submit_btn" type="submit">
</form>
<?php
}
?>
</body>
</html>
JS file:
$(document).ready(function() {
var invoiceNo = $('#invoice').val(); //Returns undefined
alert(invoiceNo);
//etc...
Why this behaviour? I tried saveral other methods but all of them failed.
Your if/else structure makes no sense. If $_GET['invoiceNo'] exists and isn't empty, then you do your if logic, so the form is never created. But then, in your form, you check to see if $_GET['invoiceNo'] exists. It never will, except if it's empty, so what's the point of using it there?
You have a couple errors: inverted if/else is the reason you are not getting a value and among other things you are missing a parenthesis on your php output inside the input and input elements should end with />
<html>
<head>
<meta charset = "UTF-8">
<title> Search Customer by Field </title>
<script type="text/javascript" src="../lib/jquery-1.10.2.js"></script>
<script type="text/javascript" src="../src/search-by-invoice-no.js"></script>
</head>
<body>
<?php
if( isset($_GET["invoiceNo"]) && "" != $_GET["invoiceNo"])
{ ?>
<form id = "form1" action='mypage.php' method='GET'>
Invoice Number <input id="invoice" name="invoiceNo" type="text" value="<?php echo (isset($_GET['invoiceNo']))? $_GET['invoiceNo'] :""; ?>" />
<br/>
<input id="submit_btn" type="submit" />
</form>
<?php
}
else
{
?>
<p>No value received</p>
<input id="invoice" name="invoiceNo" type="hidden" value="0" />
<?php
}
?>
</body>
</html>
Also, you don't even need to check if it is set as you already do, so something like
Invoice Number <input id="invoice" name="invoiceNo" type="text" value="<?php echo $_GET['invoiceNo']; ?>" />
Will work fine too
EDIT
<html>
<head>
<meta charset = "UTF-8">
<title> Search Customer by Field </title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$(document).ready(function(){
var invoice = $('#invoice').val();
if(invoice == 0)
{
alert('No value was passed');
}
else
{
alert(invoice);
}
});
</script>
</head>
<body>
<?php
if( isset($_GET["invoiceNo"]) && "" != $_GET["invoiceNo"])
{ ?>
<form id = "form1" action='mypage.php' method='GET'>
Invoice Number <input id="invoice" name="invoiceNo" type="text" value="<?php echo $_GET['invoiceNo']; ?>" />
<br/>
<input id="submit_btn" type="submit" />
</form>
<?php
}
else
{
?>
<p>No value received</p>
<input id="invoice" name="invoiceNo" type="hidden" value="0" />
<?php
}
?>
</body>
</html>

Converting data with commas to variables in JavaScript

I have a row of a lines that is like this, these are the lines of html output of a php code that comes from a search.
here is the browser output page source (not the server source)
**<!DOCTYPE html>
<html>
<head>
<meta`enter code here` content="text/html; charset=windows-1252" http-equiv="content-type">
<title>my page</title>
</head>
<body>
<div style="text-align: center;">
<title>My Page</title>
<form name="myform" action="systemmonitor.php" method="GET">
<div align="center">My page<br>
<br>
<input name="sea1" placeholder="customtext" size="25" type="text"> <br>
<br>
<input name="sea2" placeholder="customtext" size="25" type="text"> <br>
<br>
<input name="sea" value="submit" type="submit"><br>
<br>
data00,data01,data02,data03, , , and so on
data10,data11,data12,data13, , , and so on
data20,data21,data22,data23, , , and so on
(and so on in rows)
<br>
</div>
</form>
</div>
</body>
</html>
here is the browser server source (php)
**<!DOCTYPE html>
<html>
<head>
<meta`enter code here` content="text/html; charset=windows-1252" http-equiv="content-type">
<title>my page</title>
</head>
<body>
<div style="text-align: center;">
<title>My Page</title>
<form name="myform" action="systemmonitor.php" method="GET">
<div align="center">My page<br>
<br>
<input name="sea1" placeholder="customtext" size="25" type="text"> <br>
<br>
<input name="sea2" placeholder="customtext" size="25" type="text"> <br>
<br>
<input name="sea" value="submit" type="submit"><br>
<br>
<?php
$searchthis = $_GET["sea1"];
$matches = array();
$handle = #fopen("sources.csv", "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if(strpos($buffer, $searchthis) !== FALSE)
$matches[] = $buffer;
}
fclose($handle);
}
//show results:
$myString = implode( ', ', $matches );
print_r($myString);
?><br>
<br>
</div>
</form>
</div>
</body>
</html>
Now, in the real page where is data0,data1,data2... is a php code that make this stuff from the html forms above, now the php output i dont what to see as is, but i want to convert to variables, and show only the variables where i want to, something likes this its ok:
var parsed[0][0]=data00;
var parsed[0][1]=data01;
var parsed[0][2]=data02;
var parsed[0][3]=data03;
var parsed[1][0]=data10;
0,X (X is the number of the colume)
1,X (X is the number of the row)
and then i show only the data where I want.
for example i will put this code somewhere in the page:
document.write(parsed[0][3]);
the data00 data01 is just a custom data that php will make from before.
how I can do this convert?
Is this what you want?
// the string containing the data
var externalData = 'one, two, three\n'
+ 'four, five, six\n'
+ 'seven, eight, nine';
// split the string into rows, and split each row into cells
var parsed = externalData.split('\n').map(function(row) {
return row.split(', ');
});
console.log(parsed[0][1]); // 'two'

Categories

Resources