why does this one jquery work and the other does not - javascript

I am creating a cms and am using php to dynamically create jquery on the fly. The first below works fine when the php renders the jquery inlce in my code but the second only works if I place on my js function script which is called into the header of the page:
# start index video row
echo '<script type="text/javascript">';
echo "$('#indexVid').fancybox({"; echo " \n";
echo "'width' : '75%',"; echo " \n";
echo "'height' : '75%',"; echo " \n";
echo "'autoScale' : true,"; echo " \n";
echo "'transitionIn' : 'elastic',"; echo " \n";
echo "'transitionOut' : 'elastic',"; echo " \n";
echo "'type' : 'iframe'"; echo " \n";
echo "});"; echo " \n";
echo '</script>';
# open help
echo '<script type="text/javascript">';
echo '$("#open_testing").click( function(){ $("#info_testing").css("display","block"); } );'; echo " \n";
echo '$("#close_testing").click( function(){ $("#info_testing").css("display","none"); } );'; echo " \n";
echo '</script>';
$name = 'testing';
$this_label = 'Learn about Site Settings';
$info = 'Site Settings are variable aspects of the site which are common to all or most all of the pages on the site such as copyright information and such, or they are miscellaneous variables necessary to a specific aspects of the site such as the send mail limitation. To find out more about each click on the info icon.';
<p id="open_'.$name.'" class="info-link"><i class="icon-info-sign"></i> '.$this_label.'</p>
<div id="info_'.$name.'" class="hide info-wrap">
<h3>'.$this_label.'</h3>
echo $info;
<p class="center"><a id="close_'.$name.'" class="button small" title="Close Info Panel">
<i class="icon-remove-sign"> </i> Close</a></p>
</div>
The html is above
$("#open_testing").click( function(){ $("#info_testing").css("display","block"); } );
$("#close_testing").click( function(){ $("#info_testing").css("display","none"); } );
which is working if the above jquery is in the header but not if inline in the code
can some explain why? thanks

Related

Can't pass php variables in parameter in a HTML onclick

I am trying to pass more then 1 php variable in parameter in a onclick.
<button type="button" class="btn btn-dark bmd-btn-fab bmd-btn-fab-sm"
onclick="<?php echo 'analyse(\'' . $name . '\', \''. $type .'\')'; ?>">
<img src="../assets/add.png" />
</button>
For only 1 parameter it worked with this :
onclick=<?php echo "analyse('$name')"; ?>"
and this:
onclick="<?php echo 'analyse(\'' . $name . '\')'; ?>"
I would put this as a comment but the placeholder text in comments says to not put answers in comments...
First, analyse has to be a JS function, not a PHP function.. Also, you don't need to put analyse inside of the <?php code block..
<button onclick="analyse('<?php echo $name ?>', '<?php echo $type ?>')">
Full demo PHP file:
<?php
$name = "John Smith";
$type = "Best Type"
?>
<div>
<button onclick="analyze('<?php echo $name ?>', '<?php echo $type ?>')">Show Name</button>
<p id="results"></p>
</div>
<script>
function analyze(name, type) {
document.getElementById("results").innerHTML = "<b>NAME:</b> " + name + " <b>TYPE:</b> " + type;
}
</script>
To elaborate, just in case you wanted to use a PHP function to return a name or type, you would use it like so:
<?php
$name = "John Smith";
$type = "Best Type";
function get_Name($nameToGet) {
return $nameToGet;
}
function get_Type($typeToGet) {
return $typeToGet;
}
?>
<div>
<button onclick="analyze('<?php echo get_Name($name) ?>', '<?php echo get_Type($type) ?>')">Show Name</button>
<p id="results"></p>
</div>
<script>
function analyze(name, type) {
document.getElementById("results").innerHTML = "<b>NAME:</b> " + name + " <b>TYPE:</b> " + type;
}
</script>

how to prompt a delete confirmation inside the php echo with an image inside an a tag link

I have done this code in viewstudents.php the deletion process worked with the image but I'm not able to do the javascript part inside php inside the echo i mean can someone help me out. here is my code:
<?php
session_start();
include("connection.php");
if(!isset($_SESSION['user']))
header("location:login.php");
$qry="SELECT * FROM studentinformation";
$result= mysqli_query($con,$qry) or die (mysqli_error($con));
echo "Welcome ".$_SESSION['user'];
echo "<table border =1 width=80%><tr><td>FullName</td><td>Email</td><td>Password</td></tr>";
while($i = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>".$i['FullName']."</td>";
echo "<td>".$i['email']."</td>";
echo "<td>".$i['Password']."</td> ";
echo "<td><a href=DeleteStudentAction.php?Key=".$i['ID']."onclick="return confirm('Are you sure?')" ><img src = delete-1432400-1211078.png style = width:35px;hight:20px;> </img> </a></td>";
echo "</tr>";
}
echo "</table>";
?>
it worked fine with the image but I want to have a confirmation prompt that's not working can someone help me out please. Here is my php action for deletion from the database in DeleteStudentAction.php
I really appreciate if someone would help me out because I'm new to php. Thanks!
You haven't got quotes around your href, try this:
while($i = mysqli_fetch_array($result)) {
echo '<tr>
<td>' . $i['FullName'] . '</td>
<td>' . $i['email'] . '</td>
<td>' . $i['Password'] . '</td>
<td>
<a href="DeleteStudentAction.php?Key=' . $i['ID'] . '" onclick="return confirm(\'Are you sure?\')">
<img src="delete-1432400-1211078.png" style="width:35px; height:20px;" />
</a>
</td>
</tr>';
}

Dynamically created script not working

I am trying to dynamically create a script but it is not working.
I tried:
echo '<script type="text/javascript">alert("hello!");</script>';
echo '<script>alert("hello!");</script>';
echo '<script type="text/javascript">$( document ).ready(function() {alert("hello!");});</script>';
please help
Upon request here is the full code where the script is dynamically created at:
In fact , there is a php page that has a javascript file that calls an ajax function on press of a button that sends the type and calls this php page , this one will dynamically create the bootstrap carousel with the right pictures (give the right category)
<?php
//the login database info goes here too
$_type= $_POST['Type'];
$_category= $_POST['Category'];
if($_type == "adult")
{
$title = "FOR THE ADULTS";
}
else
$title = "FOR THE KIDS";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT image,imagealt FROM gallery where type='".$_type."' AND category='".$_category."' ";
$result = $conn->query($sql);
$result1 = $conn->query($sql);
?>
<tr>
<td class="GalleryImageInfo">
<div id="parent_back" >
<h1>GALLERY</h1>
<p class="ForAdults"> <?php echo $title ?> </p>
<p class="ButtonGalleryPreview" style="text-align:center"> <?php echo $_category ?> </p>
<?php
$countit = 0;
if ($result->num_rows > 0) {
// output data of each row
?>
<div id="slider_caption" > <div>
<?php
while($row = $result->fetch_assoc()) {
$image_description = $row["imgdesc"];
echo "
<div class=\"carousel-caption caption-".$countit."\">
<h3>Description</h3>
<p>".$image_description."</p></div>";
$countit++;
}
echo "</div></div>";
echo "<p class=\"GalleryImageBack\" style=\"font-family:BandaBold; color:#6c555e; font-size:15px;\"><i class=\"fa fa-arrow-circle-left\" style=\"margin-right:10px; font-size:15px;\" ></i>BACK</p>
</div>
</td>
<td style=\"width:100%\">
<div class=\"container\" style=\"width:100% ; padding:0px !important;\">
<div id=\"myCarousel2\" class=\"carousel slide\" data-ride=\"carousel\">
<!-- Wrapper for slides -->
<div class=\"carousel-inner\">";
$firstitem = true;
while($row = $result1->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
$image_url = $row["image"];
$image_alt = $row["imagealt"];
if($firstitem)
{ echo"<div class=\"item active\">
<img src=\" $image_url \"
alt=\".$image_alt.\" style=\"width:100%;\">
</div>";
$firstitem= false;
}
else
echo"<div class=\"item\">
<img src=\" $image_url \"
alt=\".$image_alt.\" style=\"width:100%;\">
</div>";
}
echo " </div>
<!-- Left and right controls -->
<a class=\"left carousel-control\" href=\"#myCarousel2\" data-slide=\"prev\">
<span class=\"glyphicon glyphicon-chevron-left\"></span>
<span class=\"sr-only\">Previous</span>
</a>
<a class=\"right carousel-control\" href=\"#myCarousel2\" data-slide=\"next\">
<span class=\"glyphicon glyphicon-chevron-right\"></span>
<span class=\"sr-only\">Next</span>
</a>
</div>
</div>
</td>
</tr>";
echo "<script type=\"text/javascript\">$(document).ready(function() {
alert(\"hello!\");
});</script>";
}
else {
echo "0 results";
}
$conn->close();
?>
check if you added the jquery script in this page or not.

PHP forms through loop doesn't send specific session against checked radio button

I am working on an e-testing system in which when a candidate login the test for which he/she was register will be shown to him/her.
I am getting the specific test through session to show the questions containing in that test. This was successfully done with the help of some "if" checks and two while loops(1 for questions and 2nd for answers against that question) having the form of radio buttons for selecting an answer.
Now each time when loop execute it create a form for single question and its answer, that way for multiple questions it results in multiple.
I am getting the checked radio buttons through ajax and posting it to another php file but I need a question id of checked answer as well to be passed to the 2nd php file
I used session but it only get the id of 1st checked and not for the others.
The 2nd php file name is answers.php for the time being i just want to post and get all values and session in answers.php file.
the main problem is with $_SESSION ['qid'] = $id; session
Note: i have used unset and destroy session to free the session and tried to re start it but i am field to do so..
<?php
include ("connection.php");
// $mysql_row = '';
$query_run = null;
$test = $_SESSION ['id'];
var_dump ( $_SESSION ['id'] );
$query1 = "SELECT * FROM question where testid = '" . $_SESSION ['id'] . "'";
if ($query_run = mysql_query ( $query1 )) {
if (mysql_num_rows ( $query_run ) == null) {
print "No result";
} else {
// echo'<form action="ss.php" method="post">';
while ( $mysql_row = mysql_fetch_assoc ( $query_run ) ) {
$id = $mysql_row ['qid'];
$_SESSION ['qid'] = $id;
echo ' <div class="panel-heading" style="background: black; font-weight:bold;">Question </div>';
$data = $mysql_row ['questions'];
?>
<form>
<br>
<div class=" form-control well well-sm">
<?php echo'<div style="font-weight:bold;"> Q: '.$data.' </div> '; ?>
<br>
</div>
<?php
$query1 = "SELECT * FROM `answer` where id='" . $id . "'";
if ($query_run1 = mysql_query ( $query1 )) {
if (mysql_num_rows ( $query_run ) == null) {
} else {
while ( $mysql_row1 = mysql_fetch_assoc ( $query_run1 ) ) {
$data1 = $mysql_row1 ['ans1'];
$data2 = $mysql_row1 ['ans2'];
$data3 = $mysql_row1 ['ans3'];
$data4 = $mysql_row1 ['ans4'];
echo "\n";
?>
<?php
echo '<div class="panel-heading" style="font-weight:bold;">Option1</div>';
?>
<div class="form-control ">
<?php
echo "<input type='radio' value='$data1' name='opt1' onclick='OnChangeRadio (this)'> $data1<br />";
?>
<br>
</div>
<?php
echo '<div class="panel-heading" style="font-weight:bold;">Option2</div>';
?>
<div class="form-control ">
<?php
echo "<input type='radio' value='$data2' name='opt1' onclick='OnChangeRadio (this)'> $data2<br />";
?>
</div>
<?php
echo '<div class="panel-heading" style="font-weight:bold;">Option3</div>';
?>
<div class="form-control ">
<?php
echo "<input type='radio' value='$data3' name='opt1' onclick='OnChangeRadio (this)'> $data3<br />";
?>
</div>
<?php
echo '<div class="panel-heading" style="font-weight:bold;">Option4</div>';
?>
<div class="form-control ">
<?php
echo "<input type='radio' value='$data4' name='opt1' onclick='OnChangeRadio (this)'> $data4<br />";
// echo'</form>';
?>
</div>
<?php
echo '</form>';
}
}
}
// $view_id1 = $view_id;
// echo $view_id1;
}
}
} else {
print mysql_error ();
}
// unset($_SESSION['qid']);
?>
connection.php
<?php
session_start ();
if (! $_SESSION ['user']) {
header ( "Location: index.php" );
// redirect to main page to secure the welcome
// page without login access.
}

PHP Combobox last selected same after changing page

Can all help me with my php. i can keep my combobox not changing after reload. but after im changing a page in my paging. it not work can you help me?
THIS IS MY PHP.
PHP
Category :
>All
>Computer
>Peripheral
>Gadget
";
else
echo "";
echo " Category : ".$baris["Category"]."";
echo " ";
echo " ";
echo " ";
echo " ";
echo " Nama : ".$baris["Nama"]. "";
echo " Harga : Rp. ".$newprice. "";
echo " Keterangan : ".$baris['Keterangan']."";
echo " ";
echo "";
echo "";
$no=$no+1;
}
?>
$i ";
}
?>
</div>
<div class="footer">
(C)
</div>
</div>
</body>
</html>
I think the problem lies on the line (roughly 101;):
for ($i=1;$i<=$jum_page;$i++){
echo "<a href = index.php?page=$i>$i</a> ";
}
The middle line should read:
echo "<a href=index.php?page=$i&choice=$selected_choice>$i</a> ";
This will ensure that the combo box choice is preserved across pages. Is this problem you were experiencing?

Categories

Resources