I am trying to print the elements of an array in PHP which was passed from Javascript. I think the js array is being passed but for some reason it is not being printed via php. Sorry if my code is not properly indented. TIA
<?php
$link = mysqli_connect("localhost","xxxx", "xxxxxx","xxxx");
if($_POST['delete']){
$delete= json_decode($_POST['str'], true);
foreach($delete as $x){
echo"<script> alert(".$x.");</script>";
}
}
?>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body >
//This is a table which displays rows from a database.
//The user selects rows and the ids of those rows are
//are stored in a javascript array called 'toDelete'
//I want to pass this array to PHP
<form method="post" id="myForm" >
<table id="secondDiv" >
<tr>
<th >
<form><input class="checkbox"type="checkbox" name="selectAll" id="selecctall" value=""></form>
</th>
<th>Date</th>
<th>Event</th>
<th>Details</th>
<th>Time & Location</th>
</tr>
<?php
$link = mysqli_connect("localhost","xxxx", "xxxxxx","xxxx");
$query = "SELECT * FROM meetings";
if($result = mysqli_query($link,$query)){
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><form ><input class=\"checkbox1\" type=\"checkbox\" name=\"".$row['meetingNo']."\" >
</form></td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['event']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['location']."</td>";
echo "</tr>";
}
}
?>
</table>
<!-- <input type="submit" class="btn btn-warning" value="Edit" /> -->
<input type="hidden" id="str" name="str" value="" />
<input type="submit" id="btn" name="delete" value="Delete" />
</form>
<script>
var toDelete = new Array();
$(document).ready(function() {
$('#selecctall').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
});
// JSON.stringify(toDelete);
$('input:checkbox').change(
function(){
if ($(this).is(':checked')) {
toDelete.push(this.name);
}
return true;
}
);
$(document).ready(function(){
$("#btn").click( function() {
$.post( $("#myForm").attr("action"),
$('#str').val(JSON.stringify(toDelete))
);
});
$("#myForm").submit( function() {
return false;
});
});
</script>
</body>
</html>
As you are printing a string, not a javascript variable, you don't have quotes within alert call.
Change the line:
echo"<script> alert(".$x.");</script>";
With:
echo "<script> alert('$x'); </script>";
Or:
echo "<script> alert('".$x."'); </script>";
Related
I want to create a column that has a text box inside each table row. The user can write any text inside the text box and click the 'Save' button to save it in the database. Additionally, the text box can be edited unlimited times. My code is the following:
index.php
<?php
...
while($row = $result->fetch_assoc()){
echo "<form action= 'search.php' method='post'>";
echo "<form action='' method='get'>";
echo "<tr>
<td><input type='checkbox' name='checkbox_id[]' value='" . $row['test_id'] . "'></td>
<td> ".$row['test_id']." </td>
<td><input type='text' name='name' value='<?NOT SURE WHAT TO INCLUDE HERE ?>'/></td>
<td><input type='submit' value='Save' id='" . $row['test_id'] . "' class='name' /></td>
<td> ".$row['path']." </td>
<td> ".$row['video1_path']." </td>
<td> ".$row['video2_path']." </td>
<td> ".$row['video3_path']." </td>
<td> ".$row['video4_path']." </td>";
if(empty($row["directory"])){
echo "<td></td>";
}else {
echo "<td><div><button class='href' id='" . $row['test_id'] . "' >View Report</button><div></td>";
}
echo " <td style='display: none;'> ".$row['directory']." </td>
</tr>";
}
?>
</table> <br>
<input id= 'select_btn' type='submit' name='submit' value='Submit' class='w3-button w3-blue'/>
</form>
</form>
</div>
<!-- Opens the pdf file from the pdf_report column that is hidden -->
<script type="text/javascript">
$(document).on('click', '.href', function(){
var result = $(this).attr('id');
if(result) {
var dir = $(this).closest('tr').find("td:nth-child(9)").text();
window.open(dir);
return false;
}
});
</script>
<!-- Updates text input to database -->
<script type="text/javascript">
$(document).on('click', '.name', function(){
var fcookie1= 'mycookie1';
var fcookie2= 'mycookie2';
var name = $(this).attr('id');
if(name) {
var text1 = $(this).closest('tr').find("td:nth-child(2)").text();
var text2 = $(this).closest('tr').find("td:nth-child(3)").text();
document.cookie='fcookie1='+text1;
document.cookie='fcookie='+text2;
$.ajax({
url: "name_edit.php",
type:"GET",
success: function() {
// alert("Edited Database");
}
});
}
});
</script>
name_edit.php
<?php include 'dbh.php' ?>
<?php include 'search.php' ?>
<?php
if (isset($_COOKIE["fcookie1"]))
echo $_COOKIE["fcookie1"];
else
echo "Cookie Not Set";
if (isset($_COOKIE["fcookie2"]))
echo $_COOKIE["fcookie2"];
else
echo "Cookie Not Set";
$var1 = $_COOKIE["fcookie1"];
$var2 = $_COOKIE["fcookie2"];
$conn = mysqli_connect($servername, $username, $password, $database);
$sql = "UPDATE test_data SET name='$var2' WHERE id='$var1'";
$query_run= mysqli_query($conn,$sql);
if($query_run){
echo '<script type="text/javascript"> alert(Data Updated)</script>';
} else {
echo '<script type="text/javascript"> alert(Data Not Updated)</script>';
}
?>
My idea was for the user to write any text. Then i will 'grab' the text and its expected id and save it to a cookie, when the save button is clicked. The cookie will then be echoed in name_edit.php and insert it in the sql code which will then update my database.
Im not sure what to include in 'value' inside the form tag. If there is data inside the database then display it inside the text box which can also be edited, else display blank for a text to be inserted.
I am new to coding and I'm a bit confused if my idea is correct or should i approach it another way.
I did some research and found out i did not have to use form but instead use 'contenteditable'. To edit the specific column i changed
<td><input type='text' name='name' value='<?NOT SURE WHAT TO INCLUDE HERE ?>'/></td>
<td><input type='submit' value='Save' id='" . $row['test_id'] . "' class='name' /></td>
to this:
<td class='name' data-id1='" . $row['test_id'] . "' contenteditable='true'>".$row['name']."</td>
and for my jquery i added the following:
<style>
.editMode{
border: 1px solid black;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
// Add Class
$('.name').click(function(){
$(this).addClass('editMode');
});
// Save data
$(".name").focusout(function(){
$(this).removeClass("editMode");
var id = $(this).closest('tr').find("td:nth-child(2)").text();;
var value = $(this).text();
$.ajax({
url: 'name_edit.php',
type: 'post',
data: { value:value, id:id },
success:function(response){
alert('Edits Saved');
return false;
}
});
});
});
</script>
and in the php side, i simply did the following:
<?php include 'dbh.php' ?>
<?php
$conn = mysqli_connect($servername, $username, $password, $database);
$field = $_POST['field'];
$value = $_POST['value'];
$id= $_POST['id'];
$sql = "UPDATE test_data SET name='".$value."' WHERE test_id='".$id."'";
mysqli_query($conn,$sql);
echo 1;
?>
i create a form(form1) to search mysql data into table without reload the page using ajax, it's working fine, but when i want to insert data from the second form(formorder) the submit button not refresh or reload.
this my code
index.php
<html>
<head>
<title>Search Data Without Page Refresh</title>
<script type='text/javascript' src='js/jquery-1.4.2.min.js'></script>
</head>
<body>
<center>
<br>
<?php include("koneksi1.php");
$sql="select * from supplier";
$query=mysqli_query($koneksi,$sql);
?>
<form action="" name = "form1">
<select name="namea" style="width:300px; padding:8px;">
<?php
while($data=mysqli_fetch_assoc($query)){
echo "<option value='".$data['id_supplier']."'>".$data['id_supplier']."</option>";
}
?>
</select>
<input type="submit" value="Search" id="search-btn" style="padding:8px;"/>
</form>
<br>
<div id = "s-results">
<!-- Search results here! -->
</div>
<script type = "text/javascript">
$(document).ready(function(){
$('#s-results').load('search.php').show();
$('#search-btn').click(function(){
showValues();
});
$(function() {
$('form').bind('submit',function(){
showValues();
return false;
});
});
function showValues() {
$.post('search.php', { namea: form1.namea.value },
function(result){
$('#s-results').html(result).show();
});
}
});
</script>
</center>
</body>
</html>
search.php
<?php
include("koneksi1.php");
isset( $_REQUEST['namea'] ) ? $name=$_REQUEST['namea'] : $name='';
$name = mysqli_real_escape_string($koneksi,$name);
if( empty( $name )){
echo '<script> alert("Please search something!")</script>';
}else{
$sql = "select * from barang where id_supplier like '%$name%'";
$rs = mysqli_query($koneksi,$sql ) or die('Database Error: ' . mysql_error());
$num = mysqli_num_rows($rs);
if($num >= 1 ){
echo "<div style='margin:10px; color:green; font-weight: bold;'>$num records found!</div>";
echo "<table width='365' border ='0' cellspacing='5' cellpadding='5'>";
echo"<tr><th>RESULTS</th></tr>";
echo "<div class='table-responsive'>";
echo "<table class='table table-hover'>";
while($data = mysqli_fetch_array( $rs )){
?>
<tbody>
<?php
$kuantitas=0;
$kuantitas++;
?>
<form action="aksi.php" method="post" id="formorder">
<tr>
<td><?php echo $data['nama_barang']?></td>
<td><?php echo $data['stok']?></td>
<td><input type="text" name="kuantitas" size="4"></td>
<td><input type="text" name="harga_satuan" id="harga_satuan" size="10"></td>
<td><input type="text" name="ppn" id="ppn" size="3"> %</td>
<td><button type="submit" class="btn btn-success btn-sm" id="add" name="add">ADD</button></td>
</tr>
</form>
<?php }?>
</tbody>
</table>
</div>
<?php
}else{
echo "<tr><th>NO RESULTS</th></tr>";
}
}
?>
the button submit in formorder not working, what wrong with my code???
How can I disable the link(delete and view) if the checkbox is unchecked.
This is my code
<table><form>
<tr>
<th></th>
<th style="width:100px;"><center>Last Name</center></th>
<th style="width:100px;"><center>First Name</center></th>
<th style="width:auto;"><center>Email</center></th>
<th style="width:100px;"><center>Birthday</center></th>
<th style="width:auto;"><center>Action</center></th>
</tr>
<?php
while($row= mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo "<input type=\"checkbox\" id=\"box\">";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['bdate']."</td>";
echo "";
echo "<td><center>";
echo "DELETE ";
echo "VIEW ";
echo "</td></center></td>";
echo "</tr>";
}
?>
</form></table>
Here is my jQuery script(example if I want to disable VIEW link only)
<script>
jQuery('#box').click(function () {
//check if checkbox is checked
if (jQuery(this).is(':checked')) {
jQuery('#view').removeAttr('disabled'); //enable input
} else {
jQuery('#view').attr('disabled', true); //disable input
}
});
</script>
I want to disable the each view and delete per row if the checkbox is uncheck how can I do it? or my jQuery is wrong? Can you give me an example for checkall box
<input type="checkbox" id="check"/><label for="check">checkbox</label>
Google
Check this link
I don't know much about php but try this:
....
.
.
echo "<input type=\"checkbox\" id=\"box\" onchange="myfunction()">";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['bdate']."</td>";
echo "";
echo "<td><center>";
echo "<input type="button" id="delete" value="Delete"> ";
echo "<input type="button" id="view" value="View"> ";
.
.
.
...
And the JavaScript goes like this:
<script>
function myFunction(){
document.getElementById('delete').disabled = !this.checked;
document.getElementById('view').disabled = !this.checked;
}
</script>
Anchors can't not be disabled only form-related elements can be disabled (input, button, textarea, etc.) What you should do is either remove the href attribute or hide the anchor element...
$(function() {
$("#chkRemove").change(function() {
if ($(this).is(":checked")) {
$("a").attr("href", "javascript:void(0)");
} else {
$("a").each(function(idx, element) {
$(element).attr("href", $(element).data("link"));
});
}
});
$("#chkHide").change(function() {
if ($(this).is(":checked")) {
$("a").hide();
} else {
$("a").show();
}
});
});
.links {
padding: 20px 0;
}
.links a {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input id="chkRemove" type="checkbox" />Remove href attribute
</div>
<div>
<input id="chkHide" type="checkbox" />Hide anchors
</div>
<div class="links">
Google Link
Stackoverflow Link
</div>
Also, as a side note. Never use the attr function to handle the state of the checkbox or a radio button. Always use the prop function instead
$(selector).prop("checked", true);
How do I copy the datepicker date into the PHP variable, $adateStringValue?
When I use " $adateStringValue = $_POST['datepicker']; " I get Undefined variable: adateStringValue error. How do I rectify this problem?
The following is my code.
To select a date:
<script>
$(document).ready(function () {
$("#datepicker").datepicker({
dateFormat: "dd-mm-yy"
});
$('form#dateform').submit(function(){
var aselectedDate = $('#datepicker').val();
localStorage.setItem('adate', aselectedDate);
});
});
</script>
abc.php :
<?php
echo "<form id=\"dateform\" name=\"dateform\" action=\"associate.php\" method=\"POST\"><br><br>
<table>
<tr><td>
<b>Select date<b></td><td>
<input id=\"datepicker\" name=\"datepicker\"value=\"$adateStringValue\"/>
</td></tr>
</table>
?>
I need to check this selected date with a column in the database( this column has dates in the string format).
if(isset($_POST['submit']))
{
$sql1="SELECT event_date from events_schedule";
$getDates_query= mysql_query($sql1,$con);
$adateStringValue = $_GET['datepicker'];
while($fetchdates = mysql_fetch_array($getDates_query)) {
if ($fetchdates['event_date'] == $adateStringValue) {
$message = "You have selected this date";
echo "<script>alert('$message');</script>";
}
else {
$message1 = "Select another date";
echo "<script>alert('$message1');</script>";
}
}
}
The complete code is as follows:
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(document).ready(function () {
$("#datepicker").datepicker({
dateFormat: "dd-mm-yy"
});
$('form#dateform').submit(function(){
var aselectedDate = $('#datepicker').val();
localStorage.setItem('adate', aselectedDate);
});
});
</script>
<?php
$con = mysql_connect("localhost","root","" );
if(!$con){
die("Cannot connect:" .mysql_error());
}
mysql_select_db("trainingschedule",$con);
echo "<form id=\"dateform\" name=\"dateform\" action=\"associate.php\"method=\"POST\"><br><br>
<table>
<tr><td>
<b>Select a date <b></td><td><input id=\"datepicker\" name=\"datepicker\" size=\"15\" value=\"$adateStringValue \" /></td></tr>
</table>
<br>
<input type=\"submit\" name=\"submit\" value=\"Submit\"/>;
</form>";
if(isset($_POST['submit']))
{
$sql1="SELECT event_date from events_schedule";
$getDates_query= mysql_query($sql1,$con);
$adateStringValue = $_POST['datepicker'];
while($fetchdates = mysql_fetch_array($getDates_query)) {
if ($fetchdates['event_date'] == $adateStringValue) {
$message = "You have selected this date";
echo "<script>alert('$message');</script>";
}
else {
$message1 = "Select another date";
echo "<script>alert('$message1');</script>";
}
}
}
mysql_close($con);
?>
Change your PHP to the following so that you are declaring $adateStringValue before it is used. Adjust accordingly to add your MySQL logic back in:
<?php
// Ternary IF statement to set either the $_POST value or a blank string
$adateStringValue = (isset($_POST['datepicker'])) ? $_POST['datepicker'] : '';
// Just use a blank action attribute to POST to the same file.
// Also added a space between the action/method attributes to output valid (X)HTML
echo '
<form id="dateform" name="dateform" action="" method="POST"><br><br>
<table>
<tr>
<td><b>Select a date <b></td>
<td><input id="datepicker" name="datepicker" size="15" value="' . $adateStringValue . '"/></td>
</tr>
</table>
<br>
<input type="submit" name="submit" value="Submit"/>
</form>
';
// Output the value if we have POST data
if (isset($_POST['submit'])) {
$message = "You have selected this date: $adateStringValue";
echo "<script>alert('$message');</script>";
}
?>
<div id='datetimepicker1' >
<input type='text' class="form-control" name="datatime" data-date-format="MM/DD/YYYY" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
I am using jquery to print the following table. Table has two columns. one for the values and other for the check boxes.
Problem I am facing is when I click a checkbox, it is triggering the function which is supposed to be triggered only when the value clicked.
Can somebody tell me how to stop triggering the function when a check box is clicked?
PHP code that prints the table
<table border='1' cellspacing='12' cellpadding='4' style='border-collapse: collapse' width='700' id=topictable >
<?php while ($row = mysql_fetch_assoc($results)) {
$topic = $row['topic'];
?>
<tr bgcolor='white'>
<td class='value'><a href='#'><?php $topic ?></a></td>
<td bgcolor='white'width='5%'><input type=checkbox name=chekboxTopic[] value= <?php $topic ?> </td>
</tr>
<?php
}
?>
</table>
Java script
$(document).on("click","#topictable td", function() {
// to set the session variable`enter code here`
var strSelectedTopic = ($( this ).text());
alert(strSelectedTopic);
var switchval = "setsessiontopic";
var param = "switchval=" + switchval + "&topic=" + strSelectedTopic;
ajaxSend(param);
var form = document.getElementById("abcd");
form.action = "design.php"
form.target = "_blank";
form.submit();
return false;
});
Couple things.
Close off your inputs
Change what is supposed to be clicked; make it more specific
Demo: http://jsfiddle.net/dL3s3acn/
<table border='1' cellspacing='12' cellpadding='4' style='border-collapse: collapse' width='700' id=topictable >
<?php
while ($row = mysql_fetch_assoc($results)) {
$topic = $row['topic'];
?>
<tr bgcolor='white'>
<td class="value"><?php $topic ?></td>
<td bgcolor="white" width="5%"><input type="checkbox" name="chekboxTopic[]" value="<?php $topic ?>" /></td>
</tr>
<?php
}
?>
</table>
<script>
// Click on the class ".value" not "td", that is too broad
$(document).on("click","#topictable .value", function() {
// to set the session variable`enter code here`
var strSelectedTopic = ($(this).text());
alert(strSelectedTopic);
var switchval = "setsessiontopic";
var param = "switchval="+switchval+"&topic="+strSelectedTopic;
ajaxSend(param);
var form = document.getElementById("abcd");
form.action = "design.php"
form.target = "_blank";
form.submit();
return false;
});
</script>