This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I'm developing irctc similar website similar project as college assignment. I'm providing user to search any train running on given date and validating from database. I'm using php and mysql and getting no error as well as invalid response from code. Please look at the code:
if (isset($_POST['btn_no'])) {
$current_date=date("Y-m-d");
$selected_date=$_POST['date'];
$no=$_POST['train_no'];
if ($selected_date>=$current_date) {
$query="SELECT train_no FROM `trains` WHERE train_no='$no'";
$found=$db->query($query);
$rows=$found->num_rows;
if ($rows==1) {
$_SESSION['train_id']=$no;
$_SESSION['date']=$_POST['date'];
header("Location: showtrain.php");
}
elseif ($rows==0) {
?>
<script>alert("Train no. invalid !")</script>
<?php
}
}
elseif ($selected_date<$current_date) {
?>
<script>alert("Wrong date");</script>
<?php
}
}
Every time <script> is executed.
From what I can tell you have an error in you query
$query="SELECT train_no FROM `trains` WHERE train_no='$no'";
WHERE train_no='$no'" will be looking for the string '$no' instead of the variable $no.
Consider trying something like to make use of the value of the variable instead of having MYSQL interprate $no as a string.
$query="SELECT train_no FROM `trains` WHERE train_no=$no";
or
$query="SELECT train_no FROM `trains` WHERE train_no=" . $no;
Related
This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 7 years ago.
I have to reload automatically to my home after done following coding
if($sql)
{
echo "Updated successfully";
}else {
echo "Con not update" . mysql_error();
}
this is process.php page here I am writing some coding after executing my query I need to redirect to my home.php page with pop up message if $sql updated successfully message(successfully) if not error message.
Also, in pure PHP, without writing a piece of JavaScript code, you can do this:
Header('Location: home.php');
This code is plenty functional on PHP an it redirects you to the requested page.
You need to use JavaScript for both alert and redirection.
if($sql) {
echo "<script>alert('Updated successfully');
window.location.href='home.php';
</script>";
}
else {
echo "Con not update" . mysql_error();
}
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I am facing issue regarding passing javascript variables to php within same function. My code looks like this
else if(msg_type[i] == 'code' ){
var code_action = 'test';
<?php
function foobar_func(){
return "<script>document.writeln(action[i]);</script>";
}
add_shortcode( 'foobar', 'foobar_func' );
?>
}
what i am doing is passing that code_action variable of javascript and returning it to function without using ajax or jquery...is there any possible method to do so...??
any possibilities will be appreciated. Thank you
When you make a request to invoke this page, your <?PHP ?> block is executed in the server. It creates a processed output in the web page (in your code) and send it to the browser. Now the browser executes your <script> </script> blocks.
I hope you understand that passing variables from <script></script> to <?PHP ?> is impossible since the latter happened in the past.
But you can use it other way around by trying to pass variables from the PHP to JS.
When you write
<?PHP
$fromServer = "from php";
echo "<script> var fromServer = " . $fromServer . " </script>
?>
.. it passes data from your PHP to JS.
You can, however, use HTTP POST or GET method to pass data from JS to PHP to be used in the next session.
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
I want to display the javascript alert and the redirect using php. The logical error is that it won't display the javascript alert.
here's my code:
if($sql_update==true){
echo "<script type='text/javascript'>alert('Updated');</script>";
header("location:?tag=student_entry&opr=upd&rs_id=".$_POST['stud_id_txt']."");
}
if your doing embedding js inside php for alert then also use JS redirect inside it
$redirectURL="Your URL";
print("<script>");
print("alert('Updated');");
print("var t =setTimeout(\"window.location='".$redirectURL."';\", 3000);");
print("</script>");
You can make an javascript function to help you alert the user and redirect like this:
function alertAndRedirect(studId) {
alert('Updated');
window.location = "?tag=student_entry&opr=upd&rs_id="+studId;
}
and in php you call it like this:
if($sql_update==true){
echo "<script type='text/javascript'>alertAndRedirect(".$_POST['stud_id_txt'].");</script>";
}
You can not provide output before header ,even if you set output buffer on , you will not be able to alert . So do redirection through js.
if($sql_update==true){
echo "<script type='text/javascript'>alert('Updated');location.href=?tag=student_entry&opr=upd&rs_id=".$_POST['stud_id_txt']."script>";
}
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i would like to do a script something like this:
<script>
function termektorol(kod)
{
<?php $parancs="delete from mex_telefon where id='?>kod<?php'";
if(mysql_query($parancs))
{
?> alert (kod); <?php
} ?>}
</script>
If i replace the 'kod' with just an actual id it deletes the thing,but i cant get it to work with the 'kod' from the function.I think its only synthax error,but i dont know how to fix it.
A possible solution is to reload page and add this parametre to the request
window.location = ...var='+kod
and in php
<?php
$parancs="delete from mex_telefon where id='.$_REQUEST["var"].'";
?>
Other solution is to use AJAX : send request from Javascript to PHP
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I've been trying to find out the response for this because I think it's not difficult to do, but I cannot find it.
I basically want to "recall" a mysql query that works. I have an HTML button and a span so I call a function in javascript which has a php code with the Mysql query and the result that is store in my span, works fine, but... Why I cannot click the button again to get the update results from Mysql table if I changed it ?
The syntax is something like ( in a single php file for test):
<script type="text/javascript">
function test(){
<?php include 'dbconnect.php';
$query = 'SELECT * FROM data';
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) {
$data = $data . "row[name]<br>";
}
mysql_close();
?>
testspan.innerHTML = <?php echo $data ?>';
}
</script>
<HTML><BODY>
<input type='button' onclick='test();'/><br>
<span id="testspan"></span
</BODY></HTML>
Resuming: when I click the first time the data appears but not the second time onwards so the Mysql query can be call just first time? anything related with the variables ?
Php is ServerSide... JS is ClientSide... you can't do code php in code JS you must use AJAX :https://api.jquery.com/jQuery.ajax/