php echo file get content on textarea when press button - php

Hello i have code like this
<html>
<div class="col-xs-3">
<div class="form-group">
<button type="button" id="get_sock" name="get_sock" class="btn-info">Get Socks5</button>
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080"><?php
if (isset($_POST['get_sock'])) {
$grab = file_get_contents("http://smansara.net/fake/grab.php");
echo $grab;
}
?></textarea>
</div>
</div>
</div>
</html>
but i have a problem when i click the button, nothing happens when i click the button, please review my code sir

Add Form tag Before button then it works correctly
<html>
<div class="col-xs-3">
<div class="form-group">
<form action="" method="POST">
<input type="submit" id="get_sock" name="get_sock" class="btn-info" value="get_sock">
</form>
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080">
<?php
if (isset($_POST['get_sock'])) {
$grab = file_get_contents("http://smansara.net/fake/grab.php");
print_r($grab);
}
?></textarea>
</div>
</div>
</div>
</html>

You need to create an Ajax function on this button click then also need to create an php file which will simply read file content and echo them.
So when you Ajax will called on button click then in response you will get file content the you can add that response to your text area in success.
If you don't know about Ajax then search on internet ,.,here i have given an demo example
$(".yourbuttonclass").click(function(){
$.ajax({url: "phpfilename.php", success: function(result){
$("#textarea_id").html(result);
}});
});

You should probably wrap your input inside a form and then change button into submit type in order to make the $_POST['get_sock'] available for your if statement. For Example (Somehow can't make the </form> tag appears at the end or my snippet)
<html>
<form method="post">
<div class="col-xs-3">
<div class="form-group">
<input type="submit" id="get_sock" name="get_sock" class="btn-info" value="Get Sock">
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080"><?php
if (isset($_post['get_sock'])) {
$grab = file_get_contents("http://smansara.net/fake/grab.php");
echo $grab;
}
?></textarea>
</div>
</div>
</div>

$_post is not correct. Use $_POST
<html>
<div class="col-xs-3">
<div class="form-group">
<button type="button" id="get_sock" name="get_sock" class="btn-info">Get Socks5</button>
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080">
<?php if(isset($_POST['get_sock'])){
$grab = file_get_contents("http://smansara.net/fake/grab.php");
echo $grab;
} ?>
</textarea>
</div>
</div>
</div>

You can use input type='button' so that form won't submit if you have.Form is not necessary for ajax call.Try like this:
<html>
<div class="col-xs-3">
<div class="form-group">
<input type="button" id="get_sock" name="get_sock" class="btn-info" value='Get Socks5'>
<div>
<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080"></textarea>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
$('#get_sock').on('click',function(){
var contents='<?=nl2br(file_get_contents("http://smansara.net/fake/grab.php"))?>';
var content = contents.split('</br>').join('\n');
$('#socks').html(content);
});
//ajax
$.ajax({
type:'POST',
url :'filename.php',//returns the file_get_contents
success:function(data){
$('#socks').html(data);
}
});
});
</script>
</html>

<html>
<div class="col-xs-3">
<div class="form-group">
<form action="" method="POST">
<input type="submit" id="get_sock" name="get_sock" class="btn-info" value="get_sock">
</form>
<div>
<?php
$tareas='<textarea name="socks" id="socks" class="form-control" rows="7" placeholder="127.0.0.1:8080">';
$tareae='</textarea>';
(isset($_POST['get_sock']))?$grab=file_get_contents("http://smansara.net/fake/grab.php"):$grab='';
echo $tareas,$grab,$tareae;?>
</div>
</div>
</div>
</html>

Related

Deleting aMySQL database with a button in PHP HTML

I wanted to delete a row in mysql phpmyadmin database through a button in AssetApprovalForm.php, but when I clicked on the decline button in AssetApprovalForm.php nothing happens.
This is the image for AssetApprovalForm.php
This is my code in AssetApprovalForm.php
<form action='delete.php?SN="<?php echo $SerialNumber; ?>"' method="post">
<div class="row">
<div class="col-sm-6">
<div class="inputfield">
<input type="text" id="decline" name="decline" value="Decline" class="btn" >
</div>
</div>
</form>
<div class="col-sm-6">
<div class="inputfield">
<input type="text" id="submit" name="accept" value="Approve" class="btn">
</div>
</div>
</div>
I have included JavaScript in my AssetApprovalForm.php to perform a pop up after the button is clicked.
<script>
$("#decline").click(function(){
swal("Bruh!", "This ticket has been declined", "error");
});
</script>
<script>
$("#submit").click(function(){
swal("Nice!", "This ticket has been approved", "success");
});
</script>
This is my code for delete.php
<?php
include 'db_connection.php';
$SerialNumber = $_GET["serial"];
$query = "DELETE * FROM waiting_approval WHERE SerialNumber = '" . $SerialNumber . "'";
if(mysqli_query($conn, $query))
{
mysqli_close($conn);
header('Location: AssetApprovalList.php');
exit;
}
else
{
echo "Error declining request";
}
?>
Current:-
<div class="col-sm-6">
<div class="inputfield">
<input type="decline" id="decline" name="decline" value="Decline" class="btn">
</div>
</div>
<div class="col-sm-6">
<div class="inputfield">
<input type="text" id="submit" name="accept" value="Approve" class="btn">
</div>
</div>
I think your input type is wrong it should be type="text" not type="decline"
and for submit button it should be type="submit" not type="text"

Hiding "write reply" textfield under each comment

I'm working on my school project which is gallery, a clone of flickr and unsplash really.
Right now I was working on comments system which works rather well all things considered, but I also want to add replies under those comments and having textfields under each comment seems clunky, so I wrote small jQuery script for it:
This is test file for this part of the code, as I don't want to ruin something in the main project if something happens.
PHP/HTML
<?php
require_once 'header.php';
if(isset($_POST['submit-comment']))
{
require_once 'includes/dbh.inc.php';
$userUid = "Bob";
$comment = $_POST['comment-input'];
$pageId = 24;
$parentId= -1;
$date = date("Y-m-d H:m:s");
$sql = "INSERT INTO comments(commentsPageId, commentsParentsId, commentsUseruid, commentsContent, commentsDate) VALUES (?,?,?,?,?);";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql))
{
echo "OPPSIE!";
}
else
{
mysqli_stmt_bind_param($stmt, "iissi", $pageId, $parentId, $userUid, $comment, $date);
mysqli_stmt_execute($stmt);
}
mysqli_stmt_close($stmt);
}
$sqlSec = "SELECT commentsId, commentsUseruid, commentsContent, commentsDate FROM comments WHERE commentsPageId=24;";
$stmtSec = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmtSec, $sqlSec))
{
echo "OPPSIE!";
}
else
{
mysqli_stmt_execute($stmtSec);
$result = mysqli_stmt_get_result($stmtSec);
echo '
<div class="comments_container">
<div class="comment-post">
<form action="" method="post">
<textarea name="comment-input" cols="100" rows="4" style="resize: none;" placeholder="Write your comment..."></textarea>
<button type="submit" name="submit-comment">Post comment</button>
</form>
</div>
<div class="comments">
';
while($row = mysqli_fetch_assoc($result))
{
$commentId = $row["commentsId"];
echo '
<div class="comment_'.$commentId.'"?
<div class="header">
<p class="name" style="color: black;">'.$row["commentsUseruid"].'</p>
<span class="date">'.$row["commentsDate"].'</span>
</div>
<p class="content" style="color: black;">'.$row["commentsContent"].'</p>
<div class="reply_'.$commentId.'">
<form action="" method="post">
<textarea id="reply" name="comment-input" cols="80" rows="2" style="resize: none;" placeholder="Write your comment..."></textarea>
<button type="submit" name="submit-comment">Post comment</button>
</form>
</div>
<button class="replybtn_'.$commentId.'">Reply</button>
';
}
echo '</div>';
echo '</div>';
}
?>
jQuery
var id= '<?php echo $commentId; ?>';
var reply = ".reply_";
var selectorReply = reply.concat(id);
var replybtn = ".replybtn_";
var selectorBtn = replybtn.concat(id);
$(selectorBtn).click(function(){
$(selectorReply).toggle()
});
This kinda works, but while($row = mysqli_fetch_assoc($result)), just spins through all id's and stops at the last one, so all reply buttons only work on the last comment.
Question is how do I apply this script to all comments, instead of only the last so that reply buttons show textfields that are appropriate?
Instead of writing jquery code for all button individually you can write your event handler like this $("button[class*=replybtn_]").click(function() { so this event will get called when any button has class i.e :replybtn and anything after that word. As you have use _ to attach id as well you can use split("_")[1] this will give you value after _ then using this you can toggle your reply div.
Demo Code:
$("button[class*=replybtn_]").click(function() {
console.log($(this).attr("class").split("_")[1])
var ids = $(this).attr("class").split("_")[1];
$(".reply_" + ids).toggle()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="comments_container">
<div class="comments">
<div class="comment_123">
<div class="header">
<p class="name" style="color: black;">123</p>
<span class="date">12/10/2002</span>
</div>
<p class="content" style="color: black;">All Goood..</p>
<div class="reply_123">
<form action="" method="post">
<textarea id="reply" name="comment-input" cols="80" rows="2" style="resize: none;" placeholder="Write your comment..."></textarea>
<button type="submit" name="submit-comment">Post comment</button>
</form>
</div>
<button class="replybtn_123">Reply</button>
</div>
----------------------------------------------
<div class="comment_231">
<div class="header">
<p class="name" style="color: black;">231</p>
<span class="date">12/10/2001</span>
</div>
<p class="content" style="color: black;">All Goood or not..</p>
<div class="reply_231">
<form action="" method="post">
<textarea id="reply" name="comment-input" cols="80" rows="2" style="resize: none;" placeholder="Write your comment..."></textarea>
<button type="submit" name="submit-comment">Post comment</button>
</form>
</div>
<button class="replybtn_231">Reply</button>
</div>
---------------------------------------
<div class="comment_456">
<div class="header">
<p class="name" style="color: black;">456</p>
<span class="date">12/10/1992</span>
</div>
<p class="content" style="color: black;">All Aweomese Goood..</p>
<div class="reply_456">
<form action="" method="post">
<textarea id="reply" name="comment-input" cols="80" rows="2" style="resize: none;" placeholder="Write your comment..."></textarea>
<button type="submit" name="submit-comment">Post comment</button>
</form>
</div>
<button class="replybtn_456">Reply</button>
</div>
</div>

Adding dataType: "json" in Ajax won't work

I'm trying to retrieve data from my database then display it into a modal, however, when I add the dataType:"json" in ajax, it seems like it no longer performs my php file.I am new to ajax and json so I don't really have an idea where I am having problem. BTW, I am trying to achieve a CRUD function that is why I am trying to load data into a modal for updating. I am using the same modal for create and update.
This is my html code.
<div class="modal fade" id="addmodal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3>Add Product</h3>
</div>
<div class="modal-body">
<form class="" action="add.php" method="post" id="insert_form" enctype="multipart/form-data">
<div class="form-group input-width center-block">
<input class="form-control" type="file" name="img" id="img" value="" required/>
</div>
<div class="form-group input-width center-block">
<label>Product Name:</label>
<input class="form-control" type="text" name="pnametb" id="pnametb" placeholder="Product Name" required>
</div>
<div class="form-group input-width center-block">
<label>Price:</label>
<input class="form-control" type="text" name="pricetb" id="pricetb" placeholder="Price" required>
</div>
<div class="form-group input-width center-block">
<label>Category:</label>
<select style="" class="action form-control" name="category" id="category" required>
<option value="" disabled selected>Select a category:</option>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<!-- Separated HTML and PHP -->
<option value="<?php echo $row['category']?>"><?php echo $row['category']?></option>
<?php
}
?>
</select>
</div>
<div class="form-group input-width center-block">
<label>Description:</label>
</div>
<div class="form-group input-width center-block">
<textarea style="color:black" name="destb" id="destb" class="message-box" placeholder="Description" rows="5" id="comment" required></textarea>
</div>
<input type="hidden" name="id" id="id">
<input class="btn btn-success" type="submit" name="add" value="Add" id="add">
</form>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" name="button" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
My ajax code:
$(document).on('click', '.edit', function(){
var id = $(this).attr("id");
alert(id);
$.ajax({
url:"fetch.php",
method:"post",
data:{id:id},
dataType: "json",
success:function(data){
$('#img').val(data.img);
$('#pnametb').val(data.productname);
$('#pricetb').val(data.price);
$('#category').val(data.category);
$('#destb').val(data.description);
$('#id').val(data.id);
$('#add').val("Edit");
$('#addmodal').modal('show');
}
});
});
And here is my php file.
<?php
//fetch.php
$connect = mysqli_connect("localhost", "root", "", "testing");
if(isset($_POST["id"]))
{
$query = "SELECT * FROM productsa WHERE id = '".$_POST["id"]."'";
$result = mysqli_query($connect, $query);
$row = mysqli_fetch_array($result);
echo json_encode($row);
}
?>
Tell me if I need to clarify more things, I'm not really good in expressing what I want to happen. Any help will be highly appreciated.
First add a JSON header to your PHP script like so:
header("Content-Type: application/json", true);
Next instead of echo $row you do:
echo json_encode($row);
And after that you probably have to change your succes: function of your Ajax call to fix printing out the data, but i will leave that up to you :)
Further reading and a more detailed explanation here:
jQuery $.ajax request of dataType json will not retrieve data from PHP script

How to submit form on one page to a different page using ajax call in jquery

I have a page called page2.php. It has a form that allows you to search via jquery ajax call. The code is below. My question is, I have a different form on page1.php. How can I submit the form on page1.php to go to page2.php and have it execute the page2.php code below using the data from the form on page1.php? I know this is most likely simple, having a brain fart right now.
$(function() {
$.validate({
form: '#form_search',
validateOnBlur: false,
errorMessagePosition: 'top',
onSuccess: function(form) {
var formval = $(form).serialize();
var formurl = '/page2.php?a=search';
$('#form_results').html('<div class="form_wait_search">Searching, please wait...<br><img src="/images/search-loader.gif"></div>');
$.ajax({
type: 'POST',
url: formurl,
data: formval,
success: function(data){
var json = $.parseJSON(data);
$('#form_errors').html(json.message);
$('#form_results').html(json.results);
}
});
return false;
}
});
});
UPDATE
Here is the forms Im referring to.
On page1.php this is like a module on the right side bar. Just a form that I want to post to page2.php
<div class="scontent_box1">
<strong class="box_title"><i class="fa fa-search fa-flip-horizontal"></i> Find Locations</strong>
<form method="post" action="/page2.php">
<div class="form-group">
<label>Street Address</label>
<input type="text" class="form-control" name="ad" placeholder="Enter Street Address...">
</div>
<div class="form-group">
<label>City, State or Zip Code</label>
<input type="text" class="form-control" name="ct" placeholder="Enter City, State or Zip Code...">
</div>
<button type="submit" class="btn btn-default blue_btn">Search</button>
</form>
</div>
Now here is the form on page2.php that executes the ajax code above. I want page1.php to submit to page2.php and envoke the same jquery code above.
<div class="row no_gutters">
<div class="search_page">
<div id="form_errors"></div>
<form method="post" id="form_search">
<div class="form-group">
<label for="ad">Street Address<span class="reqfld">*</span></label>
<input type="text" class="form-control" data-validation="required" id="ad" name="ad" placeholder="Enter Street Address..." value="">
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="ct">City, State or Zip Code<span class="reqfld">*</span></label>
<input type="text" class="form-control input-sm" data-validation="required" id="ct" name="ct" placeholder="Enter City Name..." value="">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-12">
<button type="submit" class="btn btn-default blue_btn btn-block">Search Locations</button>
</div>
<div class="col-md-8"></div>
</div>
</form>
</div>
<div id="form_results"></div>
</div>

form is sending variables by get method

i got a form.
<form id="site-contact-form">
<div>
<div class="wrapper"><span>Ձեր անունը:</span>
<div class="bg">
<div>
<input type="text" class="input" name="contactname" id="contactname" />
</div>
</div>
</div>
<div class="wrapper"><span>Ձեր E-mail-ը:</span>
<div class="bg">
<div>
<input type="text" class="input" name="email" id="email" />
</div>
</div>
</div>
<div class="textarea_box"><span>Տեկստ:</span>
<div class="bg">
<div>
<textarea cols="1" rows="1" name="message" id="message"></textarea>
</div>
</div>
</div>
<div style="clear:both;"></div>
<button id="sub" name="submit">ուղարկել</button>
</div>
</form>
As you see i got no action on it, no method. But when im clicking on button, its refreshing the page (like when it have action) and adding to URL ?contactname=&email=&message=&submit= ... I never met this problem before, why it is sending variables? I dont have any php on page yet...
Beacause the default method is GET and the URL is the same of the page. Use:
<form method="POST" action="/reactor">
<!-- .... -->
</form>
Actually, the action property is mandatory.
Please correct the code with:
<form id="site-contact-form" method="post">
And the submitted values will not be displayed on the URL.
If you do not specify the form method, it will "get" by default.
This is happening here.
Change the type of button as button
<button id="sub" name="submit" type="button">ուղարկել</button>

Categories