Continue Executing PHP after Ajax Call - php

I have this block of HTML:
<div class="box-body" id="mcontent">
<?php
if (isset($_POST["id"])){
$id = $_POST["id"];
$query=mysqli_query($con,"select complain from tbcomplains where cid = '$id';");
if (mysqli_num_rows($query) > 0){
while($row=mysqli_fetch_assoc($query)){
echo $row["complain"];
}
}
}
echo "<br /> <p />";
echo "<p> Copy this link and paste in the mail for user's responses <br />";
echo "http://" . $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];
?>
</div>
Which is replaced with this Ajax call, I include the the script below:
$('a').on('click', function(e){
var rex = e.target.id; //$('#this').data('id');
$('#mcontent').load("doload.php", {id:$('#' + rex ).data("id")});
});
The above code works perfectly. Issue is, how do I force this part of HTML to execute after the Jquery has finished.
I want this part of the PHP/HTML to execute:
echo "<br /> <p />";
echo "<p> Copy this link and paste in the mail for user's responses <br />";
echo "http://" . $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];
Thank you in anticipation.

Knowing the fact that Javascript is a client-side language, you won't be able to "force" PHP to wait for an AJAX response.
The only possibility to modify your existing DOM after the AJAX call would be to use javascript when the query has responded.

Related

form submit without refreshing page using ajax

i have old code, that has jquery v1.9.
now for some new edit, i need to submit form without refreshing page using ajax. but it dose not work.
and when i change version of jquery, old script dose not work.
my code is here:
days.php:
<script>
$(document).ready(function(){
$('#formSubmit').click(function(){
$.post("day2.php",
{name: $('#name').val()},
function(data){
$('#response').html(data);}
);});});
</script>
<div>
<input type="text" id="name">
<button id="formSubmit">send</button>
<textarea id="response"></textarea>
</div>
and day2.php:
$name = $_POST['name'];
echo "response: " +$name;
i dont know where is problem!
try:
echo "response: " . $name;
instead of:
echo "response: " +$name;
Actual problem is in second file where in
echo "response: " +$name;
you have used + as string concatenation operator instead of '.'
so actual code is like this
echo "response: " .$name;

variable in header location doesnt work in iframe?

Hi guys i am doing a personal project which i want to use on my small business, i am not that rich thats why i cant afford to hire a developer to do it for me :( my problem is that variable seems not to work if its called on an iframe, ok just like i said im not a pro developer so even on terms im not sure what to use so i will just explain on whatever i can and i hope some one can catch up with me.
an iframe is called on uikit modal, url on iframe have parameters
file is called firstreminder.php and this is the code
<?php
require_once 'layout/header.php';
if(!empty($_GET['message'])){
$sms_msg = 'https://www.experttexting.com/ExptRestApi/sms/json/Message/Send?username=xpertdotmedia&password=c61iQO1E&api_key=4aw0l0noipli133&from=LocalVerify&to=63'.urlencode($_GET['mobile']).'&text=' . urlencode($_GET['message']) . '&type=text';
$msg_json = file_get_contents($sms_msg);
$msg_array = json_decode($msg_json, true);
}
$url = $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
parse_str($parts['query'], $query);
$clientid = $query['userid'];
$getinvid = $query['invid'];
?>
<form action="">
<div class="uk-margin">
<textarea rows="6" cols="50" name="message" class="uk-textarea"/><?php
echo 'Greetings,
Just a friendly reminder for your upcoming due on '.$query['date'].'
Thank you
*auto msg do not reply';
?></textarea>
</div>
<div class="uk-margin">
<?php echo '<input type="text" name="mobile" class="uk-input" value="'.$query['userphone'].'" />';?>
</div>
<div class="uk-margin">
<button class="uk-button uk-button-primary" type="submit">Send</button>
</div>
<?php
if(!empty($msg_array)){
$status = $msg_array['Status'];
if($status!="1"){
header('Location: sent.php?id='.$getinvid);
}
}
?>
</form>
<?php
require_once 'layout/footer.php';
?>
so far its working good as i can fetch value of each parameters, example i can call $query['date']whenever i want and its actually working but problem lies on header location as it doesnt seem to fetch the value of the variable, header location is called whenever the script successfully sent the sms message and then it redirect to sent.php as i want to remind the user that sms was successfully sent and also update the database and this is the code of the sent.php file
<?php
require_once 'layout/header.php';
$senturl = $_SERVER['REQUEST_URI'];
$invURL = parse_url($senturl);
parse_str($invURL['query'], $smsquery);
$invoID = $smsquery['id'];
$updateSentTo = "UPDATE ffcko_invoices_invoices SET last_sent=NOW() WHERE ffcko_invoices_invoices.id=$invoID;";
if (mysqli_query($DBcon, $updateSentTo)) {
echo '<h1 class="uk-h3 uk-text-success">Message Sent <span uk-icon="icon: check; ratio: 1.5"></span></h1>';
echo '<script>window.top.location.reload();</script>';
} else {
echo "Error: " . $updateSentTo . "<br>" . mysqli_error($DBcon);
}
mysqli_close($DBcon);
require_once 'layout/footer.php';
?>
it should go to sent.php?id=(id) so that i can successfully update the database but i always end up with this error
i tried all the combinations i can, i even transfered the code from sent.php to firstreminder.php inorder for it to have a direct control of the variable but still problem occurs as if the $getinvid is being ignored
i think it has something to do with the iframe? you think so? cause if I enter the url directly to my browser
http://localhost/sms/sent.php?id=3
adding the id manually it works as it should and it updates the database so i wonder what im doing wrong?
hope you guys can help me, thank you so much and i hope i have explained it well as english is not my native language so i really really apologize.

Trouble sending and Interpreting form data sent via ajax through dynamically generated content

Sorry for the overly complicated title, not sure how else to describe it. So, here is what I have.
I am dynamically creating div's for each of a databases row content to act as a frame work for a gui. That part works. It generates a new div and properly displays it for each row of the db and propagates the requested data. When you click on the generated gui it sends the data to another page for processing which is displayed in another div. This all works. What I am having trouble with is on the page which receives the send form data from the gui. It either displays the entirety of the db's contents from the requested or if I add a where to the select statement the fetch request doesn't seem to fire off at all.
<?php
$con=mysqli_connect("localhost","base","password","util");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM recipes";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo"
<script>
$(document).ready(function() {//start document ready
$('#front').click(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://localhost/pages/receivingpage.php',
data: $(\"f2\").serialize(),
success: function(d){
$(\"#content-disp\").html(d);
}
});
});
});//end document ready
</script>
<div id=\"covera\">
<div id=\"holder\" class=\"holder\">
<div id=\"disp\" class=\"disp\">
<div class=\"click diagonal panel\">
<div id=\"front\" class=\"front\">
<form id=\"" . $row['recipe'] ."\" name=\"f2\">
<input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
<h2>" . $row['recipe'] ."<br></h2>
</form>
</div>
<div class=\"back\">
<div class=\"pad\">
<h2>" . $row['recipe'] ."</h2>
<p>" . $row['id'] ."</p>
<p>Number of Servings " . $row['servings'] ."</p>
</div>
</div>
</div></div></div></div>";
}
mysqli_close($con);
?>
Here is the code for the receiving page. The gui code is on the main page and the receiving page is located in a div within the main page but within no other div.
If I add a where to the select statement the fetch request doesn't seem to fire off at all. The echo 2 will display but the requested result wont.
$r = $_POST["f2"];
$sql = "SELECT * FROM recipes WHERE recipe ='".$r."'";
$result = mysqli_query($con,$sql);
echo " 2 ";
while ($row = mysqli_fetch_array($result)) {
echo " " . $row['recipe'] ." ";
}
mysqli_close($con);
?>
You are closing your mysqli_connect() in the while loop, try moving the line mysqli_close($con); after the while loop in the second php script.

jQuery .ajax call with javascript onclick event not working

I have this piece of code and it works fine while isolated into the 2 small scripts I have below. Basically I push a button which makes a call to the database to delete a row.
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script>
function removeNo(){
$.ajax({url: "removeno.php",
type: "post",
});
};
</script>
</head>
<body>
<input class='mybutton' type='submit' onclick='removeNo()' value='Remove' />
</body>
</html>
removeno.php
<?php
include 'config.php';
$sql = "DELETE FROM comments WHERE id='24453305255'";
$query = mysql_query($sql);
?>
Now when I take the HTML button part and put it into a larger PHP script it does not work. The entry that I would like to delete is still in the database. I'm still calling the same PHP file 'removeno.php'. The Javascript is in the header just the same as it is above. All files are in the same directory, and I've made sure to include the 'config.php' connection file in the PHP script.
I've not posted my full PHP scripts as it's several hundred lines, however the whole page evaluates to this piece, and evaluates through the "if" maze I have below. It even displays the button as it should with the value of "remove". It would display "submit" if it didnt evaluate correctly. Am I calling this function correctly within this PHP script?
echo "<td>"; //Main Button
if($isleadership == 2) {
if($reviewed == "Yes") {
echo "<input class='mybutton' type='submit' onclick='removeNo()' value='Remove' />";
} else if ($reviewed == "No") {
echo "<input class='mybutton' type='submit' name='".$a."' value='Submit' />";
}
}

click a link pass a session variable to the next page

I have a page where I am using a PHP while loop to echo the names and addresses of our dealers. For the email link, I want the user to be able to click the link, which would then take him to a form that emails that person.
I know that you can't mix Javascript and PHP, so how do I tell which link the user has clicked on so that I can pass that variable to the form on the next page? Here is my code:
<?php while($row = mysql_fetch_array($result)) {
if ($row['active'] == 1) {
?>
<div id="dealer">
<div id="dealername">
<h3 style="float:left;"><?php echo $row['company']; ?></h3><br/>
<p><?php echo $row['address1']; ?><br/>
<?php if($row['address2'] || $row['address3']) {
echo $row['address2'] . ' ' . $row['address3'] . '<br/>'; } ?>
<?php echo $row['city'];
if ($row['state']) {
echo ', ' . $row['state'];
}
echo ' ' . $row['zip']; ?>
<br />
<?php echo $row['country']; ?>
<br />
<br />
Phone: <strong><?php echo $row['phone1']; ?></strong><?php if ($row['phone2']) {
echo ' or ' . $row['phone2']; } ?><br/>
<?php if($row['fax']) { echo 'Fax: ' . $row['fax']; } ?><br />
</p>
</div>
<?php if ($row['email']) {
echo '<div class="dealerEmail">'. // need help here . '</div>';
} ?>
<br class="clearall" />
</div>
<?php }} ?>
The page that this code appears on is pulling data from a database using a GET statement based on a URL similar to "index.php?state=az".
I've tried making the email link use a "?email=..." format but when I go to the dealer-email.php page there are no parameters in the URL, which I don't get. That's why I'm trying to find some other way to pass some kind of variable to the next page, based on which link the user clicks on.
Hope this is clear. Thanks in advance for your help.
this should work fine:
contact dealer
and then you retrieve the data in the somepage.php page:
if(isset($_GET['email']))
$email = $_GET['email'];
else
//redirect somewherelse
I know that you can't mix Javascript
and PHP
This is not true. Javascript is a Client-Side Scripting language, and PHP is Server-Side scripting. They run at different times, on different machines, and will run just fine.
Mixing Javascript and PHP might be a mental challenge and subject to pitfalls, but it definitely can be done, and often is done.
You might skip the email form altogether and include a mailto attribute in your link.
contact dealer
This will open the user's default email client new message window with the address filled in. I understand you may want to use a custom form to prevent someone from spamming all your contacts, but if this is a restricted area for logged in users, or an intranet site, this might work.

Categories