send data through ajax use value as paragraph php - 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.

Related

How to fix ajax that loads a class on a page?

I made a comment column by putting process.php in the page header, but when I want to load a #comment-list, 1 page entered into the div.
I've tried changing ajax.url and it works, but it will affect the entire system that was created.
I want to load this part when ajax is successful
HTML
<div id="comment-list" class="widget-body">
<?php news_comment($server_conn,$id_news); ?>
</div>
FORM
<form action="<?php echo FullURL() ?>" method="post" id="newcomment">
<div class="form-group">
<label for="comment">* Comment:</label>
<textarea class="form-control" rows="3" name="text_comment" placeholder="Your Comment" required></textarea>
</div>
<button type="submit" class="btn btn-primary" id="submit-comment" name="submit-comment" value="<?php echo $row['id_news']; ?>">Post Comment</button>
</form>
AJAX
$("#submit-comment").click(function(event){
event.preventDefault();
var submit = $(this).attr('value');
var post_url = $("#newcomment").attr("action");
var request_method = $("#newcomment").attr("method");
var form_data = $("#newcomment").serialize();
$.ajax({
url : post_url,
type: request_method,
data : form_data + '&submit-comment=' + submit,
success: function(){
$("#newcomment")[0].reset();
$("#comment-list").load(post_url + "#comment-list");
}
})
});
When Ajax responds successfully, I want #comment-list loads automatically, without entering a 1-page display

Jquery and AJAX not even triggering

I have this piece of code.
It's a input for a newsletter.
<form id="newsletter-form" name="newsletter-form" method="post" action="/newsletter">
<div class="form-group">
<label for="exampleInputEmail1"><span>Cadastre-se em nossa newsletter</span></label>
<input type="email" class="form-control" id="newsletter-email" name="newsletter-email" placeholder="e-mail" value="">
</div>
<button class="btn btn-secondary btn-block" type="button"><i class="fas fa-paper-plane fa-fw mr-1"></i>Cadastrar</button>
</form>
Quite simple!
And I have this JS:
$(function()
{
var form = $('#newsletter-form');
// ----------
$(form).submit(function(form_response)
{
form_response.preventDefault();
var formData = $(form).serialize();
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response)
{
$('#newsletter-email').removeClass('is-invalid');
$('#newsletter-email').addClass('is-valid');
$('#newsletter-email').val(response);
})
.fail(function(data)
{
$('#newsletter-email').removeClass('is-valid');
$('#newsletter-email').addClass('is-invalid');
if (data.responseText !== '')
{
$('#newsletter-email').val(data.responseText);
}
else
{
$('#newsletter-email').val('Ocorreu um erro!');
}
});
});
});
My PHP check if email is valid, if already is inserted, etc, etc.
The PHP returns codes and echos correctly(I use it on other parts of my website)
The thing is:this form is not even triggering the JS to run the PHP.
It should run the PHP and add a is-valid or is-invalid class on the inpute and show the feedback as value or placeholder.
Any ideas why it's not working?
Just to add it as answer your button type was button not submit so eaither use input type submit or change button type to submit :D
This is your original code. var form = $('#newsletter-form');
Since you have put the form tag into a variable, you don't need to use $. Change $(form) into form. The code will look like this.
form.submit( /* some code */ );
or if you don't want to store it into variable, you can directly call the function from jquery.
$('#newsletter-form').submit( /* some code */ );

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

Saving boostrap WYSIWYG Submitted URI too large using AJAX and 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.">

How to get result from php to html use ajax?

Updated: It still not work after I add "#".
I am new to ajax. I am practicing to send value to php script ,and get result back.
Right now, I met one issue which I can not show my result in my html page.
I tried serves answers online, but I still can not fix this issue.
My index.html take value from the form and send form information to getResult.php.
My getResult.php will do calculation and echo result.
How do I display result into index.html?
Hers is html code
index.html
<html>
<body>
<form name="simIntCal" id="simIntCal" method="post"
>
<p id="Amount" >Amount(USD)</p>
<input id="amount_value" type="text" name="amount_value">
<p id="annual_rate" >Annual Rate of Interest
(%)</p>
<input id="rate_value" type="text" name="rate_value">
<p id="time_years" >Time (years)</p>
<input id="time_value" type="text" name="time">
<input id="calculate" type="submit" value="Calculate">
</form>
<p id="amount_inteCal" >The Amount (Acount
+ Interest) is</p>
<input id="result" type="text">
</body>
</html>
ajax script :
<script>
$('#simIntCal').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'getResult.php',
data: $('#simIntCal').serialize(),
success: function (result) {
$("#result").text(result);// display result from getResult.php
alert('success');
}
});
});
</script>
getResult.php
<?php
if ($_SERVER ["REQUEST_METHOD"] == "POST") {
//do some calculation
$result=10;//set result to 10 for testing
echo $result;
}
?>
You are missing the '#' in front of your css selector for result.
$("result").text(result);// display result from cal.php
Should be
$("#result").text(result);// display result from cal.php
index.php
----php start---------
if(isset($_POST['name'])){
echo 'Thank you, '.$_POST['name']; exit();
}
----php end ---------
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
function test(){
var formDATA = {'name': $('#input_name').val()}
$.ajax({
type: 'POST',
url: 'index.php',
data: formDATA,
success: function(response){
$('#result').html();
}
});
}
</script>
<input id="input_name" type="text" value="">
<button onclick="test();">Test Ajax</button>
<div id="result"></div>
Try something simple, this is a very basic version of ajax and php all in one page. Since the button triggers the function you don't even need a form (doesn't mean you shouldn't use one). But i left it simple so you could follow everything.
Sorry when i added php open and closing tags it didn't show up as code.
Also don't forget to include your jquery resources.
In your html file where you want the result to display, you probably want to be using a div.
Currently your code is using an input field:
<input id="result" type="text">
And what you probably want is something like this:
<div id="result"></div>
Unless you were intending to have the result show up in an input field inside your form, and in that case, the input field isn't actually inside your form code.

Categories