CSS Template Dropdown Menu Breaks When Adding PHP - javascript

I'm using a mobile template for a personal site and I'm using it because of the menu format. The source and demo can be found here http://mobifreaks.com/free-mobile-website-templates/galaxy-mobi/ and all I have done to it is strip some of the content out of the page.
The menu is a CSS menu that is using jquery. I am using PHP to interact with a MySQL database so it is necessary to for me to use a PHP page. If I just save the page as a PHP page it works fine but when I add any PHP code to the page(in the body) both menu items are displayed like a normal list of buttons instead of a dropdown menu.
I add the following PHP to a post-content div:
<?php
while($rows=mysql_fetch_array($result) or die(mysql_error())){
?>
<table align="center" border="4" cellspacing="2" cellpadding="3" width="100%">
<tr><td>Date</td><td>Cateogry</td><td colspan="4">Decription</td><td>Credit</td><td>Debit</td></tr>
<tr><td class="datetable"><? echo $rows['date']; ?></td><td class="category"><? echo $rows['category']; ?></td><td class="description" colspan="4"><? echo $rows['description']; ?></td><td class="amount"><? echo $rows['amount']; ?></td><td class="amount"><? echo $rows['amount']; ?></td></tr>
<tr><td colspan="2" align="center">Back To Top</td></tr>
</table>
<br>
<?php
}
?>
I'm curious if there's any way around this. If you would like any of my code or any more clarification, I'm happy to provide it. Thanks for all of the help.

The or die statement in your while loop is causing your PHP code to stop executing when there are no longer any results left to retrieve. The or die statement executes whenever mysql_fetch_array returns false (the end of your data set).
If you replace it with while(($rows = mysql_fetch_array($result)) !== false) {, then your PHP code will continue to run and should allow everything to execute correctly.
I see you are using the mysql_* functions. Please note that these have been deprecated for quite some time. I would highly recommend you look into MySQLi or PDO MySQL.

Related

How can I send SQL variable from a HTML table to JS popup and then to PHP variable without jquery? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 1 year ago.
I'm trying to make software that creates a job board where you can add jobs and maintenance guys can check them off as they go. My problem is getting the request_ID of my table to my SQL query at the bottom in deleteData(). I've tried so much, any tips will be a lot of help.
I have to be making this hard than it needs to be. Im literally just trying to click a button, pop up a form, ask if the user has completed the job, then remove it from the table.
<!DOCTYPE html>
<html>
<head>
<script>
function complete() {
document.getElementById("myForm").style.display = "block";
}
function closeForm() {
document.getElementById("myForm").style.display = "none";
}
</script>
<link rel="stylesheet" href="styles.css">
<title>KCHC Work Orders</title>
</head>
<?php
$host = "localhost";
$user= "";
$password = "";
$database="workorders";
$DBConnect = #new mysqli($host,$user,$password,$database);
if ($DBConnect->connect_error)
echo "The database server is not available at the moment. " .
"Connect Error is " . $DBConnect->connect_errno .
" " . $DBConnect->connect_error . ".";
else{
echo "Connection made";
}
?>
<header>
<div class="menu">
<nav>
Create a Request
</nav>
</div>
</header>
<body>
<?php
$sql = "SELECT request_id, name, date, location, description FROM requests";
$stmt = $DBConnect->query($sql);
if ($stmt->num_rows > 0) {
echo '<div class="request_box">';
echo '<table id="myTable" ><tr><th>ID</th> <th>Name</th> <th>Date</th> <th>Location</th> <th>Description</th> <th>Complete?</th> </tr>';
while (($Row = $stmt->fetch_assoc()))
{
echo "<tr id=";
echo $Row['request_id'] . ">";
echo "<th>" . $Row['request_id'] . "</th> <th>" . $Row['name'] . "</th> <th>" . $Row['date'] . "</th> <th>" . $Row['location'] . "</th> <th>" . $Row['description'] . "</th> <th>" . '<input type="button" onclick="complete()" value="' . $Row["request_id"]. '" />' . "</th></tr>";
}
echo "</table> </div>";
}
else{
echo '<div class="request_error">';
echo "<p> There are no jobs to complete!</p> <br>";
echo "<p> If you think there should be, please contact Matt or his genius son</p>";
echo '</div>';
}
function sendit($i){
$newId = $i;
}
?>
`
<div class="popup" id="myForm" >
<form id="formReal" method="post">
<h1>Would you like to remove this job? </h1>
<input type="submit" class="btn" onclick="" submit="<?php deleteData() ?>" value="Remove from List" />
<button type="button" class="btnCancel" onclick="closeForm()">Cancel</button>
</form>
</div>
</body>
<?php
function deleteData(){
global $DBConnect;
$id_new = $newId;
$sql = "DELETE FROM requests WHERE request_id = 3";
$stmt = $DBConnect->query($sql);
}
?>
You cannot put PHP code inside a javascript event and have it work the way you are intending it to in your code. PHP code is run on the server before the page is sent to the user. PHP can be used to generate javascript code, but PHP cannot be called in-page by javascript. Javascript is run in the browser. If you want javascript code to trigger PHP code, you need to have javascript initiate an HTTP(S) request to a separate PHP script. This can be done either by sending the browser to a new page for that script, or by using AJAX to initiate a background call to that script while keeping the user on the same page. It seems like this latter option is what you are intending.
So what you want here to do this gracefully is the AJAX programming method, which is a whole paradigm of programming that is quite involved; there's no way we could possibly teach you in a single answer. The basic idea is that, in javascript, you need to initiate a request, and then you need event-handling code to listen for the response, do error handling, and then update the HTML on-page to reflect whatever response you got from the script you called.
Then in the PHP script that got called, you put the SQL query to actually delete the appropriate row in the table in the database.
If you look up some basic AJAX tutorials and you feel like you're in over your head, you might want to rethink a simpler way of doing this that is perhaps a bit clunkier or old-fashioned ("Web 1.0") to the user.
You can achieve what you want easily via HTML forms, use a form with action="/path/to/some_script.php" and then send the user to a new page, sending POST variables which are read by the PHP script. And then that page can just redirect back to the original page, displaying a refreshed version of the page after deleting the item. The tradeoff here is that the programming is much easier and less error-prone, and debugging is easier, but you need to send the user around to different pages. So you get less work, but a more clunky user experience.
It's your choice. If I were in your position, and I need to meet a nearby deadline, I would probably put up a quick solution using HTML forms, but I'd start learning AJAX, which is going to take months for you to do well. Then come back and put up a slick AJAX solution when you're ready.

How to make an input value permanent in wordpress or work with global variables

I currently make my first Wordpress website using Java script snippets for a countdown that refreshes itself constantly and allows me to click a button once every 6 hours. So I managed that the time refreshes itself but I need one permanent variable that tells me if I already clicked the button or not (since it should work wether I refresh the page, go to another side or even log in with another user).
Basically I just want one variable that changes between 0 and 1.
I implemented a hidden input field and it works just fine as long as I stay on the side (refreshing the side works as well) but as soon as I change the page it sets the variable back. I tried to implement a global variable in the function.php of my theme (and a function as well) but it still doesn't work.
I tried it like this:
global $x;
echo $x;
And this:
function displayX() {
global $x;
$x = "0";
echo $x;
}
The thing is I don't want to set a value because the variable needs to be changeable any time.
That's my current html:
<input type="text" id="id1" value="<?php echo $x; ?>" <="" input="">
But it just doesn't work.
My second approach was to make the input field permanent (but updateable) - again this approach works as long as I don't change the side.
I tried it like this:
<span id="00">
<input type="text" id="id1">
</span>
Can anybody please help me? Also please specifiy where I have to set the global variable since there are so many function.php files.
THANK YOU!
Easiest way to do that is using of update_option and get_option.
update_option is for save data to database that will be permanent.
get_option is for fetching data from database.
<form method="post">
<input type="text" name="permanent" id="permanent" value="<?php echo get_option('permanent_data'); ?>"/>
<input type="submit" name="save" value="submit"/>
</form>
You can catch form data in backend using an action like this:
in functions.php
add_action('init','save_permanent');
function save_permanent(){
if(isset($_POST['save'])){
update_option('permanent_data', $_POST['permanent']);
}
}
Below code checks that if form is submitted:
if(isset($_POST['save'])){
update_option('permanent_data', $_POST['permanent']);
}
If form submitted it gets value of input text that named permanent
Mentioned code permanent the data in database as name of permanent_data
If you want to fetch stored data you just call get_option(option name) like this:
<?php echo get_option('permanent_data'); ?>
For first question you can do it in such way:
in functions.php
<?php
if(if(isset($_POST['save']))){
update_option('already_clicked', '1');
}
And for fetch stored data you can use:
<?php
if(get_option('already_clicked') == '1'){
//do somthing
}
?>

PHP redirect URL

I have successfully directed my users to a page that contains their information from a table. Using this code:
<?php foreach ($customers as $row) : ?>
<td onclick="window.location='view_customer.php?customer_id=<?php echo $row['id'] ?>&operation=edit';">
</td>
<?php endforeach; ?>
Now they are on view_cutomer.php. The next step would be to redirect the users to another page that also contains their information (the same information). Using a button. The next page is paint.php. I've tried this code, but it does not seem to work. Btw this next page no longer has a table.
<button onclick="window.location='paint.php?customer_id=<?php echo $row['id'] ?>&operation=edit';" class="rbutton">Paint</button>
How do i redirect users to a specific page using their ID?
on the 2nd page the id you want is in the get array (from the url)
<button onclick="window.location='paint.php?customer_id=<?php echo $_GET['customer_id'] ?>&operation=edit';" class="rbutton">Paint</button>

Clicking on a div and saving its value to use with PHP

I have been having some problems with some code.
So I'm developing a website that accesses a mysql database and creates a number of divs accordingly to the number of specialties that are on the database.
<div id="services_panel">
<?php foreach ($specialties as $especialidade_id => $especialidade) { ?>
<div class="service_div" onclick ="highlightLink(this); $('#content_div').show();" value="<?php echo $especialidade_id; ?>"><?php echo $especialidade; ?></div>
<?php } ?>
</div>
<div id="content_div""/>
Now what I wanted to do was set the value of the content div to the value of $especialidade of the div that was clicked, but since javascript runs clientside and php runs server side, I know that I can't do it on onclick()
I'd really hope you guys could help me with this issue
Thanks in advance
Using onclick() inline you could do
onclick ="highlightLink(this); $('#content_div').show().html(this.innerHTML);"
or if you bind it separately using .on() and 'click'
$(function(){
$('.service_div').on('click',function(){
$('#content_div').show().html($(this).html());
});
});

Create a local file from user input and list existing files

I need to use the input from a user to create a filename.html on the server they are attached to. The program normally takes the input and writes it TO an existing html file. In this one case, I need to add the ability to create the file they are writing to before they begin writing to it.
While I am wishing :) I would like to start out by showing the user a directory of the html files are already there in case someone else has already created it.
The logic of the input would be to use the data input to create the file IF the file does not already exist. If it does exist, then they would open to the pre-existing file by that name.
This is an adaptation of "Microchat" php script which normally writes to msg.html.
It works fine as-is but this one project needs the added ability of creating multiple Name.html files and then letting the user continue as normal except they will be writing to the filename they chose or created rather than the using the generic msg.html. I have been working on the assumption that I will have to create a variable of their input to use for creating the file.
The entire script for Microchat is only 144 lines. But too long to post in its entirety
It can be viewed at www.phptoys.com. The area I need help with is this:
function createForm () {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table align="center">
<tr><td colspan="2">Please eneter a nickname to login!</td></tr>
<tr><td>Your name: </td>
<td><input class="text" type="text" name="name" /></td></tr>
<tr><td colspan="2" align="center">
<input class="text" type="submit" name="submitBtn" value="Login" />
</td></tr>
</table>
</form>
<?php
This is normally used to create the users login. I have tried a few variations on the input to achieve a variable for creating a file but sadly, I know my limits. I doubt I will find a way without help.
I am not sure what you are trying to make. Possibly you can use database for the purpose but purpose is not clear to me so I don't know. By the way you can use following approach. Then you can do customization as per your requirements.
<?php
$fileName = "newHTMLFileName";
$path= "../yourFolder/".$fileName.".html";
if (!file_exists ($path))
{
$code = "<!DOCTYPE html><html><body><h1>Information</h1></body></html>";
$handle = fopen ($path, "w");
$write = fwrite ($handle,$code);
fclose($handle);
echo "created";
}
else
{
echo "already created";
}
?>

Categories

Resources