Saving boostrap WYSIWYG Submitted URI too large using AJAX and PHP - php

I am trying to save my bootstrap WYSIWYG content by copying the div editor to a hidden textarea, but when i tried to submit it using ajax, i got a error "Submitted URI too large!" when checked i found out the image that i am trying to add was encoded into a base64. how can i get the image URL so i can process it using PHP.
My HTML
<div id="editor" name="editor" data-target="content">
</div>
<form>
<textarea type="text" name="content" id="content"></textarea>
<button type="submit" class="btn btn-danger copyeditor">Save</button>
</form>
My jquery and ajax script
<script>
$(".copyeditor").on("click", function() {
var targetName = $("#editor").attr('data-target');
$('#'+targetName).val($('#editor').html());
$.ajax({
type: "POST",
url: "data.php",
data: $('#content').html(),
success: success,
dataType: dataType
});
});
</script>
Here is what i am dealing with.
The goal is to make this
<img style="width: 640px;" src="data:image/jpeg;base64,/9j/4Qv6RXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAeAAAAcgEyAAIAAAAUAAAAkIdp...............>
Look like this
<img src="/images/blah/blah.jpg.">

Related

When using ajax POST for form in codeigniter, not passing through data

I am trying to post data through my website via ajax, however for some reason it seems to echo the success message but does not get the mobile field data. it's just blank. When i inspect with firebug, it gives the response, but the POST tab is empty.
My controller contains the following :
function submit()
{
//set validation rule
// get post data
$emailid = $this->input->post('mobile');
// write your database insert code here
echo "<div class='alert'>Thanks for Subscribing! Please stay tuned to get awesome tips...</div> here is $emailid";
}
And my view contains :
<label>Mobile</label>
<input type="number" name="mobile" id="mobile" class=" form-control" />
<button type ="submit" id="submit" name="submit" class="btn btn-info btn-block" /> NEXT </button>
The JS
<script type="text/javascript">
$("#submit").click(function(e) {
e.preventDefault();
var mobile = $("mobile").val();
$.ajax({
url: "https://cheddarplatform.com/complete/submit",
method: "POST",
data: {mobile: mobile},
success: function(data) {
$("#message").html(data);
},
error: function() {
alert("Please enter valid email id!");
}
});
});
</script>
$("mobile") is not a proper selector, as you do not have a HTML element of that type anywhere. Probably you want to use $("#mobile") and have a look at your browser's network tab to find this error easier the next time

send data through ajax use value as paragraph php

I'm really inexperienced when it comes to PHP, and I've been trying to write my own kind of "adminpage", a really simple one. The problem is when I'm passing a value with ajax from the adminpage to the index page, the value is not appearing in the paragraph.
html adminpage.php (page which ajax request is sent from):
<div class="col-lg-6">
<textarea class="form-control textarea" rows="5" placeholder="Tak text 1"></textarea>
<button class="btn btn-primary text-change1">Submit</button>
<textarea class="form-control textarea" rows="5" placeholder="Tak text 2"></textarea>
<button class="btn btn-primary text-change2">Submit</button>
</div>
index.php, page which is receiving the request
<?php
$text = $_POST['text'];
?>
<div class="desc-text">
<p class="roof">
<?= $text ?>
</p>
</div>
Ajax:
$(".text-change1").click(function(){
if($(this).attr("class").indexOf("text-change1") != -1){
$.ajax({
type:"POST",
url:"index.php",
data:{ text:text },
success:function(){
alert("message sent!");
}, error:function(){
alert("Something went wrong");
}
});
});
The goal here is to add the data sent through ajax into the mentioned paragraphs html.
This:
success:function(){
you never capture the output/return value of the the PHP script, and therefore don't insert anything into your page. You need something more like this:
success: function(response) {
$('#place_to_insert_response').html(response);
}
I sent the text to a textfile with ajax then I read the text file after $(document).ready to update the paragraph from the admin page.

Sending images from form with AJAX

I've been working on a form where you upload an image and a comment. I got it working in PHP, but now I'd like to do it in AJAX. The reason I'd like to do it in AJAX is because the user has to type all of their text again when the upload fails (due to conditions like: fields can't be empty or the aspect ratio of the image is off). However whatever I do, I fail to do it in AJAX.
I've tried to do it in FormData, with $.post and with $.ajax. I've also looked up a lot of guides, and other posts about this, but nothing seems to work for me.
Below you will find the HTML form, the jQuery AJAX call and the PHP code from the PHP page the AJAX call calls.
HTML FORM
<form action="uploadpost.php" id="postForm" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="postImage">Upload image</label>
<input type="file" id="fileToUpload" name="postImage" class="form-control">
</div>
<div class="form-group">
<label for="description">Write a caption</label>
<textarea name="description" id="postMessage" cols="30" rows="5" class="form-control"></textarea>
</div>
<input type="submit" id="postSubmit" value="Create post" name="upload_image" class="btn btn-primary">
</form>
The AJAX call
<script type="text/javascript">
$(document).ready(function(){
$("#postSubmit").on("click", function(e){
var formData = new FormData($("#postForm"));
//var message = $("#postMessage").val();
$.ajax({
url: "ajax/postUpload.php",
type: "POST",
data: formData,
contentType: "multipart/form-data",
cache: false,
processData:false,
success: function(data)
{
console.log(data);
}
});
e.preventDefault();
});
});
The PHP code in ajax/postUpload.php
<?php
include_once("../classes/Post.class.php");
$post = new Post();
var_dump($_FILES);
if(!empty($_POST)){
$file_tmp_name = $_FILES['postImage']['tmp_name'];
$result = $post->createPost($file_tmp_name);
if($result){
$uploadCheck['check'] = "success";
}else{
$uploadCheck['check'] = "error";
}
header('Content-Type: application/json');
echo json_encode($uploadCheck);
}
?>
Output from console.log(data) on ajax call return
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=0)</i>
<i><font color='#888a85'>empty</font></i>
</pre>
The problem is that I can't send the image nor the message over to that PHP page, which doesn't allow me to do createPost().
var formData = new FormData($("#postForm"));
The argument to FormData should be a DOM form object, not a jQuery object.
new FormData($("#postForm")[0])
Setting the content type manually:
contentType: "multipart/form-data",
… will fail to set the boundary data in it. Say:
contentType: false,
… so that jQuery won't try to set it at all and the XHR object will generate it from the FormData object.

Need to POST a single form to two different pages

I'm trying to post a single form to two pages. The first page is loacally hosted and other one is a remote page which need to be redirected after posting to first page. I can use jquery to post the form but have no idea how to post to the remote server's file.
My form is like:
<form method ='POST' action='https://remotewebsite.com/api/step1.asp' id='payment_form' class='form-horizontal' >
<div class='form-body'>
<input type='hidden' name='group' value='$net_amount'/>
<input type='hidden' name='type' value='$payment_type' />
<div class='modal-footer'>
<button type='button' class='btn default' data-dismiss='modal'>Cancel</button>
<input type='button' id='pay' name='pay' value='pay' class='btn green' />
</div>
</div>
</form>
I can use jquery to post it to my other page say "process.php" but couldn't find any way to submit the form again to the url https://remotewebsite.com/api/step1.asp
<script type="text/javascript">
$('input[type=button]').click(function(e){
e.preventDefault(); // prevent the browser's default action of submitting
$.ajax({
type: "POST",
url: "process.php",
data=$("#payment_form").serialize(),
data: data,
dataType: "html",
});
});
</script>
One solution would be to POST to your process.php script as you currently are.
Then after you've finished processing, fire off a cURL POST request to the remote server (from your process.php script. If you Google for PHP curl post request you should find a load of articles telling you how to do so, e.g: http://davidwalsh.name/curl-post
how about just simply duplicate $.ajax?
<script type="text/javascript">
$('input[type=button]').click(function(e){
e.preventDefault(); // prevent the browser's default action of submitting
$.ajax({
type: "POST",
url: "process.php",
data=$("#payment_form").serialize(),
data: data,
dataType: "html",
});
$.ajax({
type: "POST",
url: "https://remotewebsite.com/api/step1.asp",
data=$("#payment_form").serialize(),
data: data,
dataType: "html",
});
});
</script>

Storing Paragraph into database using PHP without refreshing the page

I have an index.php page that contains a paragraph element & an input button:
<p id="result">Hello this is test paragraph</p>
<input id="insertbutton" type="button" value="insert" />
I would like to store the paragraph content into database without refreshing the page when I click the insert button..
Hope I can get help
Thank you very much
<p id="result">Hello this is test paragraph</p>
<input id="insertbutton" type="button" value="insert" />
//AJAX
$("#insertbutton").click(function(){
var dataToPass=$("#result").html();
$.ajax({
url:'Your_PHP_File_URL_that_will_do_insertion',
data:dataToPass
});
})
Using Ajax with jQuery to make a post request like this should be enough.
$("#insertbutton").click(function(){
var data = "text=" + $("#result").text();
$.ajax({
type: "POST",
url: "post.php",
data: data,
success: function(){
alert("success");
}
});
});

Categories