Getting form value from PHP in JQuery - javascript

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>

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 do I validate CSV file format using onclick function?

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>

Send data to served with Jquery ajax

I am learning jquery.ajax but I cant understand something
<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#driver").click(function(event){
var cacat = $("#nam").val();
$("#stage").load('/jquery/result.php', {"name":cacat} );
alert(cacat);
});
});
</script>
</head>
<body>
<p>Enter your name and click on the button:</p>
<input type="input" id="nam" size="40" /><br />
<div id="stage" style="background-color:blue;">
STAGE
</div>
<input type="button" id="driver" value="Show Result" />
</body>
</html>
and here is the Php FIle
<?php
if( $_REQUEST["name"] )
{
$name = $_REQUEST['name'];
echo "Welcome ". $name;
}
?>
I don't understand what is this $("#stage").load('/jquery/result.php',{"name":cacat});
because it's also working with $("#stage").load('result.php', cacat );
So here comes the question:
What's the difference between {"name":cacat} and cacat? Also both scripts do the same thing, the jQuery gets the value of the input and the PHP also gets the value of the input, why so?
The difference is that the first one sends a JSON object which gets to $_REQUEST whereas the other one sends the value, without the JSON form.

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