I have tried making a html and php page for taking input and processing them respectively. In the HTML page, user is given a option of either give a text or upload the file.
<form action="target.php" method='POST' enctype="multipart/form-data" id="form">
<table width="950px" align="center">
<tr>
<table width="100%" align="center" cellpadding="0" cellspacing="0" class="table1" border="0">
<tr valign="middle" align="left"><td width="15" height="10"></td><td></td></tr>
<tr valign="middle" align="left">
<td width="15"></td>
<td><font color="White">input1</font>
<td width="15"></td>
<td>
<font color="Red"><input type="file" name="file" size="42"><br></font><font color="White">or paste below:</font><br>
<textarea name="sequence1" cols="96" rows="7"></textarea><br>
</font>
</td>
</tr>
</table> </form>
Similarly in the PHP page, i could succesfuly transfer the uploaded file from the html page to a varible and run the shell script but unable to process the content from textbox.
<?php
$a = $_FILES['file']['tmp_name'];
$c = (isset($_POST['sequence1'])) ? $_POST['sequence1'] : false;
if($a!=NULL)
{
$output=shell_exec("sh server.sh $a");
}
elseif ($c!=NULL){
$output=shell_exec("sh server.sh $c");}
else{
echo "No input";}
?>
Can anybody help me to solve this problem or other way you could help me would be text input variable to converted to a file for the shell script
If I understand you well, this is what you need to do:
<?php
if(isset($_POST['sequence1'])){
//get the input content
$textInput = $_POST['sequence1'];
//temp file name
$file = 'temp.txt';
// Write the contents back to the file
file_put_contents($file, $textInput);
$output=shell_exec("sh server.sh $file");
}else{
echo "No input";
}
?>
Parse error in your php code.
echo No input;
It should be
echo "No input";
Related
I'm trying to update the two locked textboxes with information that I get from my database. I enter a phone number in the "Telefon" checkbox, and I want it to get the firstname and lasttname for that phone number. Which works by the way, but it's not the way I want it. I want the information to be automatically put into the textboxes without refreshing the page. and for some odd reason my code got split in two here. I've tried to look for a solution for hours. I'm very new to coding, and I would love some help!
<?php
SESSION_START();
$output = NULL;
if(isset($_POST['btn_checkTelefon'])) {
require 'connectdb.php';
$telefon_Search = $connect_DB->real_escape_string($_POST['telefon_Search']);
$sql = "SELECT * FROM elever WHERE Telefon = '$telefon_Search'";
$resultSet = $connect_DB->query($sql);
if($resultSet->num_rows > 0) {
while($rows = $resultSet->fetch_assoc()) {
$fornavnoutput_Database = $rows['Fornavn'];
$etternavnoutput_Database = $rows['Etternavn'];
}
echo '<script type = "text/javascript">';
echo 'function sayHi() {';
echo 'val1 = document.getElementById("telefon_Input").value;';
echo 'if(val1 == "") {';
echo ' alert("Vennligst skriv inn ditt telefon nummer!");';
echo '}';
echo 'if(val1 !== "") { ';
echo ' document.getElementById("check_Fornavn").value = "<?php echo $fornavnoutput_Database?>";';
echo ' document.getElementById("check_Etternavn").value = "<?php echo $etternavnoutput_Database?>";';
echo '}';
echo '}';
echo '</script>';
} else {
$output = "No results";
}
}
$fornavnoutput_Database2 = "Fornavn";
$etternavnoutput_Database2 = "Etternavn";
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style2.css?<?php echo time(); ?>" />
</script>
<script type = "text/javascript"></script>
<title></title>
</head>
<body>
<?php
include 'connectdb.php';
?>
<form name="form1" action="">
<table id="valgt_skap_tabell" class="bokssvartabell">
<tr>
<td>Valgt skap</td>
</tr>
<tr>
<td>
<input class="bokssvarskjema" type="text" name="Valgt skap" disabled value= <?php
if(isset($_POST["radios"])){
echo $_POST["radios"];
} else {
//header('location: index.php');
} ?>>
</td>
</tr>
</table>
<table id="telefon_tabell" class="bokssvar_tabell">
<tr>
<td>Telefon:</td>
</tr>
<tr>
<td><input type="text" name="telefon_Search" id="telefon_Input" maxlength=8"><br></td>
</tr>
<tr>
<td><button type="button" name ="btn_checkTelefon" id="sjekkTelefon" onclick = "sayHi()">Sjekk</button></td>
</tr>
<div id="d1"></div>
</table>
<table id="opplysninger_tabell" class="bokssvartabell">
<tr>
<td>Fornavn:</td>
<td>Etternavn:</td>
</tr>
<tr>
<td><input type="text" name="Fornavn" disabled id="check_Fornavn"></td>
<td><input type="text" name="Etternavn" disabled id="check_Etternavn"></td>
</tr>
</table>
</form>
<?php echo $output; ?>
</body>
You need to use AJAX for this. Example $.ajax() -> shortcuts $.post(), $("id").load("url"), ... Look it up a lot in depth explanation about these on stackoverflow.
Please never mix JavaScript with php.
Use it in separate file.
Edit: So have you fixed it yet?
The easier way to load paged dynamicaly is with load method + actions. $.post is used if you need to do something with returned data from php. I will give you example of load.
I will give a proper example how to code.
Universal function for a link that look at href value and load HTML parts (form in this case) from PHP dynamicaly to your page, you do need to implement actions or just call your ordinary page if you have only one default action there. I use jQuery library here. This script must be in separate file else it will work but you will get a sync warning in your console.
$(function() {
$('a').on("click", function(e) {
e.preventDefault();
e.stopPropagation();
var URL = $(this).attr('href');
$('body').load(URL, 'form');
})
})
php example
prefered showsomethingfunction in separate file like showfunctions.php
function myLinks() {
echo "<a href='index.php?action=showsomething'>showsomething</a>"
}
index.php + included showfunctions.php
<?php
myLinks();
if(isset($_GET["action"]){
// do your ordinary thing like open connection with database.
switch($_GET["action"]))
{
case "showsomething":
//show showsomething() function with html
break;
//further you can add more action instead of showsomething if you have several links
}
//close database.
}
?>
You need to separate your code else it will be a mess if it gets even more complicated. HTML code must be ONLY in showfunctions.php for example in function to call for actions.
Code is not tested but I think it will work. This code will also work without javascript but then it will just reload pages.
You have to use jQuery $.post() for that.
first You have to create php file which will process Your data.
For example lets create file query.php with the following content:
<?php
if(isset($_POST['telefon_Search'])) {
require 'connectdb.php';
$telefon_Search = $connect_DB->real_escape_string($_POST['telefon_Search']);
$sql = "SELECT * FROM elever WHERE Telefon = '$telefon_Search'";
$resultSet = $connect_DB->query($sql);
if($resultSet->num_rows > 0) {
$row = $resultSet->fetch_assoc();
echo json_encode($row);
}
}
next on Your page You have to create function which will send phone number to our query.php file and which will return Name and Surname if they exist.
$('#sjekkTelefon').click(function (){
$.post('query.php', { telefon_Search : $('#telefon_Input').val() }, function (data){
var user = $.parseJSON(data);
$('#check_Fornavn').val(user.Fornavn);
$('#check_Etternavn').val(user.Etternavn);
});
});
and complete html will looks like:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style2.css?<?php echo time(); ?>" />
<title></title>
</head>
<body>
<table id="telefon_tabell" class="bokssvar_tabell">
<tr>
<td>Telefon:</td>
</tr>
<tr>
<td><input type="text" name="telefon_Search" id="telefon_Input" maxlength=8"><br></td>
</tr>
<tr>
<td><button name ="btn_checkTelefon" id="sjekkTelefon">Sjekk</button></td>
</tr>
<div id="d1"></div>
</table>
<table id="opplysninger_tabell" class="bokssvartabell">
<tr>
<td>Fornavn:</td>
<td>Etternavn:</td>
</tr>
<tr>
<td><input type="text" name="Fornavn" disabled id="check_Fornavn"></td>
<td><input type="text" name="Etternavn" disabled id="check_Etternavn"></td>
</tr>
</table>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$('#sjekkTelefon').click(function (){
$.post('query.php', { telefon_Search : $('#telefon_Input').val() }, function (data){
$('#check_Fornavn').val(data.Fornavn);
$('#check_Etternavn').val(data.Etternavn);
});
});
</script>
</body>
</html>
While browsing a website i found an interesting feature that i like to have in my application.It is like displaying search result on same page without refreshing the page. When someone fills the last field of the form it shows the search result on the same page without refreshing the page.The form has no submit button or any link to submit the form data when someone fills the last field it search result and display the result. I want to implement this feature in my application i built in PHP. I know this can be done by Javascript & Ajax but i don't have enough knowledge of these languages. I just learned HTML,CSS,PHp & MySql. Let me show u my code
HTML:
<form action="do_search.php" method="post"/>
Enter Number:<input type="text" name="roll" id="roll">
<input type="submit" value="search record"/>
do_search.php:
<?php
include 'includes/dbConnect.php';
?>
<?php
$roll = $_POST['roll'];
$result = mysql_query( "SELECT * FROM students WHERE Roll_No = '$roll'" ) or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
?>
<table width="100%" bgcolor="#F7F7F7" border="0">
<?php
if ($num_rows!=0)
{
?>
<tr style="text-align:left;">
<td width="7%" align="center" bordercolor="#CCCCCC" bgcolor="#996600" class="heading"><strong>#</strong></td>
<td width="13%" align="left" bordercolor="#CCCCCC" bgcolor="#996600" class="heading"><strong>Roll No</strong></td>
<td width="29%" align="left" bordercolor="#CCCCCC" bgcolor="#996600"class="heading"><strong>Name</strong></td>
<td width="23%" align="left" bordercolor="#CCCCCC" bgcolor="#996600" class="heading"><strong>Father Name</strong></td>
<td width="16%" align="center" bordercolor="#CCCCCC" bgcolor="#996600" class="heading"><strong>Class</strong></td>
<td width="12%" align="center" bordercolor="#CCCCCC" bgcolor="#996600" class="heading"><strong>Section</strong></td>
</tr>
<?php
//$counter = 1;
while ($get_info = mysql_fetch_row($result))
{
print '<tr class="text-data">';
print '<td align="center" valign="top">' . $get_info[0] . '</td>';
print '<td align="left" valign="top">
<a href="edit_Student.php?roll_no=' . $get_info[1] . '" style="color:#000"><b>' . $get_info[1] . '</b></td>';
print '<td align="left" valign="top">'. $get_info[2] . '</td>';
print '<td align="left" valign="top">' . $get_info[3] . '</td>';
print '<td align="center" valign="top">' . $get_info[4] . '</td>';
print '<td align="center" valign="top">' . $get_info[5] . '</td>';
print '</tr>';
//$counter++;
}
}
else
{
?>
<tr style="text-align:left;">
<td align="center" colspan="7">
No Student Found !
</td>
</tr>
<?php
}
?>
</table>
This is working fine for me and display the record on do_search.php page. I want my form has no submit button option & when someone enter the form field Enter Roll Number it should display the record on same page and display all the record from database. Can anyone please help me for this. Thanks in advance
You can do that with jQuery ajax.
<form action="do_search.php" method="post"/>
Enter Number:<input type="text" name="roll" id="roll">
</form>
<div id="result"></div>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
$("#roll").change(function() {
$.post("do_search.php", { roll:$("#roll").val() })
.done(function( data ) {
$("#result").html(data);
});
});
</script>
Use Jqyery keydown() Method
$("input").keydown(function(){
//Do display logic
// Use Ajax and get the data for the value enter and update your html content
});
http://www.w3schools.com/jquery/event_keydown.asp
sorry for the bulk of code :)
I need to input a client name in the input textfield name clientname then i need to output the client information from my database when i click a button name loadcliinfo. But the problem is i don't know how will i get the data inputted in clientname? so I used $_POST['clientname']. The code is not working.
the code i pasted below is located on the same file.
<tr>
<td width="6"></td>
<td width="155">Client Name<font color="#990000">*</font></td>
<td width="218"><input type="text" name="clientname" id="clientname" required= "required" class="forinput" value="<?php echo $clientname ?>"/></td>
<td width="148"><input type="button" name="loadcliinfo" value="Load Client Info" onClick="loadclientinfo()"/></td>
</tr>
<tr>
<td width="6"></td>
<td width="155">Client Address<font color="#990000">*</font></td>
<td width="218"><p id="clientadd" class="readonly"></p></td>
<td width="148">Contact Name<font color="#990000">*</font></td>
<td width="230"><p id="clientcontactname" class="readonly"></p></td>
<td width="8"></td>
</tr>
<tr class="shade">
<td width="6"></td>
<td width="155">Email Address<font color="#990000">*</font></td>
<td width="218"><p id="clientemail" class="readonly"></p></td>
<td width="148">Contact No<font color="#990000">*</font></td>
<td width="230"><p id="clientaddcontact" class="readonly"></p></td>
<td width="8"></td>
</tr>
my php code
<?php
$name=isset($_POST['clientname'])? $_POST['clientname'] : '';
I get the data using this statement isset($_POST['clientname'])? $_POST['clientname'] : '' and store it in the variable $name but when i output the variable its empty.
$csql="Select address, email, contactperson, contactno from client where name='$name'";
$result=$db->prepare($csql);
$result->execute();
while($resu= $result->fetch(PDO::FETCH_ASSOC))
{
echo '<input type="hidden" id="add" value="'.$resu['address'].'"/>';
echo '<input type="hidden" id="email" value="'.$resu['email'].'"/>';
echo '<input type="hidden" id="contactperson" value="'.$resu['contactperson'].'"/>';
echo '<input type="hidden" id="contactno" value="'.$resu['contactno'].'"/>';
}
?>
My loadclientinfo function
<script>
function loadclientinfo()
{
var add=document.getElementById("add");
var email=document.getElementById("email");
var contactperson=document.getElementById("contactperson");
var contactno=document.getElementById("contactno");
document.getElementById("clientadd").innerHTML = add.value;
document.getElementById("clientemail").innerHTML = email.value;
document.getElementById("clientcontactname").innerHTML = contactperson.value;
document.getElementById("clientcontact").innerHTML = contactno.value;
}
</script>
I think getting data from clientname is the problem. Is there any way i could get the data from clientname on the same page?
thanks :D
$_POST and $_GET get their information from an html form (for the most part). You have the input fields, but no form. You need to have something along the following:
<form method="post" action="myphpscript.php">
<input type="text" .... >
<input type="submit" .... >
</form>
That method attribute is important because that determines what gets put into $_POST and what gets put into $_GET. Then your action will be the php script that it goes to. And lastly, those variables don't get stored into the local POST session (assuming your method is "post") until the submit button is hit. Now if you want to do this whole thing without refreshing the page, then you'll have to do a bit of AJAX work to call the PHP script when the "submit" button is hit.
But that's basically the problem. Nothing is getting put into local storage. Simply having a misc. input field doesn't do anything. If you don't want to use a form, then you'll have to figure out how to get the data some other way, without $_POST. You cooouulldd contruct a GET url then use $_GET but that's not typically recommended as the user can plainly see the variable values in the address bar like: (your url)?clientname=foobar.
More on PHP with POST and GET
Im using ajax to load the testNew.php file into the Cart.html file. It loads the testNew.php file but when i click on the button add which is in the testNew.php, 0 is being entered in the database and as soon as i click on the add button the page refresh by itself. My problem is that i dont want the page to refresh and want the add button to do the same action as in the testNew.php file(which works correctly).
<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.2.js'>
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("#product").click(function() {
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "testNew.php",
dataType: "html", //expect html to be returned
success: function(response){
$("#responsecontainer").html(response);
//alert(response);
}
});
});
});
</script>
</head>
<body>
<table border="1">
<tr>
<td>
<input type="button" id="product" name="product"value="View all products"/>
</td>
</tr>
</table>
<div id="responsecontainer"></div>
Here is the testNew.php which works correctly.
<?php
include'connect.php';
$image = isset($_REQUEST['image']) ? $_REQUEST['image'] : "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "";
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : "";
$price= isset($_REQUEST['price']) ? $_REQUEST['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120'
height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<form method="POST" action="" >
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="name" value="<?php echo $row['name']; ?>" />
<input type="hidden" name="image" value="<?php echo $row['image']; ?>" />
<input type="hidden" name="price" value="<?php echo $row['price']; ?>" />
<input id="submit" type="submit" name="submit" value='Add to cart'
onclick="add()"/>
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
?>
Your error in thought is actually that you are including a form through AJAX of which the HTML is then loaded into your page after which the action attribute on the form refers to the page itself (loading HTML into your page with AJAX does not work the same as an iframe) which does not have the relevant code to actually parse and insert the database.
You need to make a separate page that only accepts a few parameters and inserts those into the database. However, before you do that you need to read this:
I'm going to go off on the safety of your code for a tad.
$_REQUEST refers to both $_GET and $_POST, you really want only $_POST as you just want to deal with what gets submitted through a form.
You never sanitize your input, this way a person could craft an URL with say phpNew.php?id='; DROP TABLE wooptiedoo. This is a simplified example but nevertheless you should take a closer look at the mysql_real_escape_string documentation and possibly some guides on "sql injection".
After that those same variables can be used for XSS which means someone can use your page to serve random HTML to people visiting that site and trusting your domain. I suggest you look up the htmlentities function and/or read up on "cross-site scripting" attacks.
And to end it all, the mysql_* functions are deprecated and you should probably be using mysqli_* functions.
jsfiddle
I'm trying to print the input text field values on nextpage (postdata.php). But it always print the first row result only. I didn't get the remaining row values on next page. I've posted my full codes on jsfiddle.. How do i get the remaining js dynamic row values in php (postdata.php page)?
JS
$(document).ready(function(){
$("span").click(function(){
$("#container").append('<tr><td>Email : </td><td><input type="text" name="email[]" placeholder="Email id" /></td> <td>Name : </td><td><input type="text" name="name[]" placeholder="Your Name "/></td> <td><a title="Delete this row" href="javascript:void(0);" class="remove">Del</a></td></tr>');
});
$("#container").on('click','.remove',function(){
$(this).parent().parent().remove();
});
});
Php
<?php
echo "
<table>
<tr>
<td>
Email :
</td>
<td>
$_POST[email]
</td>
<td>
Name :
</td>
<td>
$_POST[name]
</td>
</tr>
</table>";
?>
Since name of fields you declared is array the $_POST becomes multidimensional array.So try like this
<?php
$size = sizeof($_POST['email']);
echo "<table>" ;
for($i=0; $i<$size;$i++)
{
echo "
<tr>
<td>
Email :
</td>
<td>
".$_POST['email'][$i]."
</td>
<td>
Name :
</td>
<td>
".$_POST['name'][$i]."
</td>
</tr>
";
}
echo "</table>";
?>
also in your html change names of Name and Email field to name[] and email[] respectively.Also you have misplaced the form tag. It starts after <table> and ends after <table>. which was not correct. so place form tag before table tag
When you add square brackets to the name of an input field, PHP will receive it's value as an array. Your JS code is fine, but the PHP code doesn't handle arrays at all. Look at the following:
echo "
<table>";
if(!is_array($_POST['email'])) {
$_POST['email'] = array($_POST['email']);
$_POST['name'] = array($_POST['name']);
}
foreach($_POST['email'] as $key => $email) {
// get the corresponding name to the email
$name = $_POST['name'][$key];
echo "<tr>
<td>
Email :
</td>
<td>
$email
</td>
<td>
Name :
</td>
<td>
$name
</td>
</tr>";
}
echo "</table>";
Note: This code will check whether multiple values were submitted or not and will work in both scenarios.