Pass PHP variable within echo <script> content - javascript

I am trying to pass $php_variable to JavaScript content in echo tag in one php file. It is because I hope to append the query value from old_page_url to new_page_url by using window.location.
For example,
http://example/?data=1234 to http://example/something-else/1234
My code snippet:
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + $php_variable";
echo "</script>";
I have tried:
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + json_decode($php_variable)";
echo "</script>";
and
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + <?php echo $php_variable; ?>";
echo "</script>";
But these two don't work. Any idea?
Update: Final Code Snippet
<?php
$php_variable = "testing";
$toURL = "http://example.com/something-else";
echo "<script>";
echo "window.location = '$toURL/$php_variable'";
echo "</script>";

echo "window.location = 'http://example.com/something-else/$php_variable'";

How about this
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL +" . "'{$php_variable}'";
echo "</script>";

Related

define javascript variable from php variable within php code

I am trying to define javascript variable from php variable in the running php code.
Here is my code:
echo "<script>";
echo "var s1 = '<?php echo isset($s1) ? $s1 : '' ?>';";
echo "getAirdrop();";
echo "</script>";
But this code not defining the javascript variable.
You don't need php tags again.
Try it like this:
echo "<script>";
echo "var s1 = '";
echo isset($s1) ? $s1 : '';
echo "';";
echo "getAirdrop();";
echo "</script>";
You can do it in one line, just concatenate all yours echo :
echo "<script> let s1 = ".isset($s1) ? $s1 : ''." getAirdrop(); </script>"

Form values are not inserting into my database

My form values are printing on this page but they are not inserting into my
database. I don't know what is wrong? If all the values are printing
that means values are reaching on this particular page but not going into
database.Even the columns in the database are sorted, but it still doesn't
work.
<?php
include("connection.php");
if(isset($_POST['continue']))
{
echo $eta_type = $_POST['eta_type'];
echo $firstname = $_POST['firstname'];
echo $lastname = $_POST['lastname'];
echo $title1=$_POST['title1'];
echo $dob=$_POST['dob'];
echo $gender=$_POST['gender'];
echo $nationality=$_POST['nationality'];
echo $cob=$_POST['cob'];
echo $passportnumber=$_POST['passportnumber'];
echo $pid=$_POST['pid'];
echo $ped=$_POST['ped'];
echo $residence=$_POST['residence'];
echo $possesseta=$_POST['possesseta'];
echo $multipleentry=$_POST['multipleentry'];
echo $arrivaldate=$_POST['arrivaldate'];
echo $purpose=$_POST['purpose'];
echo $final_dest=$_POST['final_dest'];
echo $stay_days=$_POST['stay_days'];
echo $add1=$_POST['add1'];
echo $add2=$_POST['add2'];
echo $city=$_POST['city'];
echo $state=$_POST['state'];
echo $pscode=$_POST['pscode'];
echo $country=$_POST['country'];
echo $addlanka=$_POST['addlanka'];
echo $email=$_POST['email'];
echo $alternate_email=$_POST['alternate_email'];
echo $phone=$_POST['phone'];
echo $mobile=$_POST['mobile'];
echo $fax=$_POST['fax'];
$date1 = date('Y-m-d', strtotime($dob));
$date2 = date('Y-m-d', strtotime($pid));
$date3 = date('Y-m-d', strtotime($ped));
$date4 = date('Y-m-d', strtotime($arrivaldate));
$address=$add1.$add2;
$sql= "INSERT INTO
user(applicationtype,
surname,givenname,
title,dob,gender,nationality,
cob,passportnumber,pid,ped,
residenceinsrilanka,processeta,
multipleentryofsrilanka,intentedarrivaldate,
purposeofvisit,finaldestination,staydays,address,
city,state,postalcode,country,addinsrilanka,email,
alternateemail,telephonenos,mobilenos,faxnos)
VALUES('$eta_type','$lastname','$firstname','$title1',
'$date1','$gender','$nationality','$cob',
'$passportnumber','$date2','$date3','$residence',
'$possesseta','$multipleentry','$date4','$purpose',
'$final_dest','$stay_days','$address','$city',
'$state','$pscode','$country','$addlanka','$email',
'$alternate_email','$phone','$mobile','$fax')";
$query = mysqli_query($conn, $sql);
if($query)
{
header("Location: continue-to-pay.php?pno=$passportnumber");
}
else
{
echo "<h4 style='color:red'>Failed.</h4>";
}
}
?>
Incredibly dangerous code...
Read some about query injection....
Any value passed this way can break your query.
You need to escape your values with mysqi_escape_string()
http://php.net/manual/fr/mysqli.real-escape-string.php

cant echo javascript alert from php

I've looked for an answer already, but I understand how to achieve an alert from php I just don't know what I am doing wrong on this particular piece of code.
I had this working until I added the if statement.
if ($errors) {
echo "<script type='text/javascript'>";
echo "alert('Records Were Uploaded');";
echo "window.location.href = 'EmployeePicker.php';";
echo "</script>";
} else {
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
echo "window.location.href = 'csvUpload.php';";
echo "</script>";
}
It worked fine when it was just .
echo "<script type='text/javascript'>";
echo "alert('Records Were Uploaded');";
echo "window.location.href = 'EmployeePicker.php';";
echo "</script>";
If comment everything out and just do
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
echo "window.location.href = 'csvUpload.php';";
echo "</script>";
this will not work. I'm so confused. It doesn't make sense why the second alert doesn't work.
I forgot to mention, In the if statement from above the first alert will work, it's that second alert that I can't get to fire.
Sorry about the confusion, the $error is a bool. If it is true a filw was uploaded, if false it wasn't.
I think you are doing the opposite of what you want:
Your if case doesn't match the action performed inside.
Maybe you should do:
if (!$errors) {
echo "<script type='text/javascript'>";
echo "alert('Records Were Uploaded');";
echo "window.location.href = 'EmployeePicker.php';";
echo "</script>";
} else {
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
echo "window.location.href = 'csvUpload.php';";
echo "</script>";
}
and you can also save up on echo words - and make it more readable - doing sth like:
echo <<<HTML
<script type='text/javascript'>
alert('Records Were Uploaded');
window.location.href = 'EmployeePicker.php';
</script>
HTML;
just if you want :)
[EDIT] Note that according to what $errors hold (either integer or array), you can check that there is no error by either !$errors if integer or !count($errors) if array.
[EDIT] Trying your second piece of code as a standalone
If you need to trace what is wrong you need to isolate bit by bit!
first try code 1 as a new php file if it does not alert and redirect on your system then something is wrong in your configs.
If it works something is wrong in your code logic.
I could also run code 2 with no pb by first setting the $errors var to 1 or 0.
code 1
<?php
$errors = 1;
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
echo "window.location.href = 'csvUpload.php';";
echo "</script>";
?>
code 2
<?php
$errors = 1;
if ($errors) {
echo "<script type='text/javascript'>";
echo "alert('Records Were Uploaded');";
// echo "window.location.href = 'EmployeePicker.php';";
echo "</script>";
} else {
echo "<script type='text/javascript'>";
echo "alert('There was a problem with your file');";
// echo "window.location.href = 'csvUpload.php';";
echo "</script>";
}
?>
And about your $errors var, if true means no error you should definitely rename it to sth less tricky like $uploadSuccessful. ;)
It should echo out good. Try checking your php error log file to see if there are any clues as to the issue in there.
Just a small most like likely personal preference of mine that may help you is to separate out the logic a bit. Please see below.
<?php
// STATUS
$errors = true;
// OUTPUTS
$upload_success = "<script type='text/javascript'>";
$upload_success .= "alert('Records Were Uploaded');";
$upload_success .= "window.location.href = 'EmployeePicker.php';";
$upload_success .= "</script>";
$upload_fail = "<script type='text/javascript'>";
$upload_fail .= "alert('There was a problem with your file');";
$upload_fail .= "window.location.href = 'csvUpload.php';";
$upload_fail .= "</script>";
//LOGIC
if ($errors) {
echo $upload_success;
} else {
echo $upload_fail;
}
?>

Jquery val() cannot read html tags received by php echo

this is my code, i have send value to input :
<script type="text/javascript">
Drupal.behaviors.devenirClientDepuisServiceform = {
attach: function (context, settings) {
jQuery(document).ready(function($) {
//$('.webform-component--nom-abnonnement>input').val('<?php echo $_GET['name']; ?>');
//$('.webform-component--produits-choisie>input').val('<?php echo $produitnid ?>');
jQuery('.webform-component--nom-abnonnement>input').val('<?php echo $produitname ?>');
jQuery('.webform-component--produits-choisie>input').val('<?php echo $produitnid ?>');
jQuery('.webform-component--type-de-client>input').val('<?php echo $type ?>');
jQuery('.webform-component--contrat>input').val('<?php echo $imageUrl; ?>');
});
}
};
//
</script>
$imageUrl is a text html
but jquery cannot understand and give me this error :
SyntaxError: unterminated string literal
try with using these quotes ` and removing line break of last one
jQuery(document).ready(function($) {
//$('.webform-component--nom-abnonnement>input').val('<?php echo $_GET['name']; ?>');
//$('.webform-component--produits-choisie>input').val('<?php echo $produitnid ?>');
jQuery('.webform-component--nom-abnonnement>input').val(`<?php echo $produitname ; ?>`);
jQuery('.webform-component--produits-choisie>input').val(`<?php echo $produitnid ; ?>`);
jQuery('.webform-component--type-de-client>input').val(`<?php echo $type ?>`);
jQuery('.webform-component--contrat>input').val(`<?php echo str_replace(array("\r", "\n"), '', $imageUrl); ?>`);
});
You forgot to write ";".. add ; after every echo statement
jQuery(document).ready(function($) {
//$('.webform-component--nom-abnonnement>input').val('<?php echo $_GET['name']; ?>');
//$('.webform-component--produits-choisie>input').val('<?php echo $produitnid; ?>');
jQuery('.webform-component--nom-abnonnement>input').val('<?php echo $produitname; ?>');
jQuery('.webform-component--produits-choisie>input').val('<?php echo $produitnid; ?>');
jQuery('.webform-component--type-de-client>input').val('<?php echo $type; ?>');
jQuery('.webform-component--contrat>input').val('<?php echo $imageUrl; ?>');
});

How to call bootstrap modal in php

I'm trying to call a modal that I have in a html file which is hidden alongside another modal. One of them appears as the button is clicked but the other one is to be called by php as part of a verification process.
Here is my php code:
$File = include("index2.html");
$Msg = 'Welcome ';
$search=$_POST['search'];
$query = $pdo->prepare("SELECT * FROM students WHERE IDs LIKE '%$search%' LIMIT 0 , 10"); //OR author LIKE '%$search%'
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo '<script type="text/javascript">';
echo 'alert("Verification Complete");';
echo '</script>';
while ($results = $query->fetch()) {
$Studname = $results['Studentname'];
echo '<script type="text/javascript">';
echo 'alert("'.$Msg.$Studname.'")';
echo '</script>';
echo '<script type="text/javascript">';
echo '$(File).ready(function() {';
echo '$("#myAdvert").modal("show");';
echo '});';
echo '</script>';
}
} else {
echo '<script language="javascript">';
echo 'alert("Sorry your ID was not found");';
echo 'window.location.href = "/Koleesy/index.html";';
echo '</script>';
}
?>
After the user is welcomed I want the hidden modal from my index.html to be called so the user can continue submitting the advert.
I've tried using the include function but that doesn't seem to work in my favor.
How do I do this?
Every answer is appreciated.
Try this:
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#myAdvert').modal('show');
});
</script>";

Categories

Resources