Trying to fill fields with Javascript, echoed by PHP - php

Here's my code. Is there some reason this should not work? I'm getting all of the fields from MySQL.
Basically what I want is to send information back from a page, with an id number, and this should be used to select the row number for MySQL.
Here's my code from the first page:
$org = $_POST['organization'];
header('Location: '.$admin.'?org='.$org);
And then my code on main page:
ECHO '<script>';
ECHO 'document.getElementById("orgid").value="'.$org_id.'"';
ECHO 'document.getElementById("orgname").value="'.$org_name.'"';
ECHO 'document.getElementById("add1").value="'.$add_1.'"';
ECHO 'document.getElementById("add2").value="'.$add_2.'"';
ECHO 'document.getElementById("city").value="'.$city.'"';
ECHO 'document.getElementById("state").value="'.$state.'"';
ECHO 'document.getElementById("zip").value="'.$zip.'"';
ECHO 'document.getElementById("url").value="'.$url.'"';
ECHO 'document.getElementById("email").value="'.$email.'"';
ECHO 'document.getElementById("phone").value="'.$phone.'"';
ECHO 'document.getElementById("contact").value="'.$contact.'"';
ECHO 'document.getElementById("hours").value="'.$hours.'"';
ECHO 'document.getElementById("file").value="'.$file.'"';
ECHO 'document.getElementById("notes").value="'.$notes.'"';
ECHO 'document.getElementById("description").value="'.$description.'"';
ECHO '</script>';
And here's the code to communicate with MySQL:
if (isset($_GET["org"]) && ($_GET['org'] !== '')) {
$org = $_GET['org'];
$resorgfull = mysql_query("SELECT org_id, org_name, add_1, add_2, city, state, zip, url, email, phone, contact, hours, file_loc, notes, description FROM organization WHERE org_id=".$org.");
if (!$resorgfull) {
die('Invalid query: ' . mysql_error());
}

One thing definitely wrong is the way you're echoing the scripts, it should be more like this:
echo "<script type='text/javascript'>\n";
echo "document.getElementById('orgid').value='$org_id';\n";
...
Or preferably...
// Close the PHP tag and output straight HTML with embedded PHP values:
?>
<script type="text/javascript">
document.getElementById('orgid').value='<?php echo $org_id; ?>';
...
If this doesn't fix it, you'll have to give us more info regarding what is not working.
I don't see the code where you take the query result and fetch each row.. are you doing that?

Related

Insert into mysql using div codes and php

the below to functions contain the code to insert into the sql database but sadly the db is still unable to load it to the database.
if (isset($_POST['register'])){
if(registerNewUser($_POST['inv_amount_expected'],$_POST['uname'],$_POST['passwo rd'],$_POST['email'])){
echo "You can now log-in to your account.
<a href='./index.php'>Click here to login.</a>
";
}else {
echo "Registration failed! Please try again.";
show_registration_form();
}
} else {
// has not pressed the register button
show_registration_form();
}
function registerNewUser($inv_amount_expected,$uname,$password,$email)
{
$sql = sprintf("insert into borrow (inv_amount_expected,uname,password,email) value ('&inv_amount_expected','&uname','&password','&email')",
mysql_real_escape_string($username), mysql_real_escape_string(sha1($password . $seed))
, mysql_real_escape_string($email), mysql_real_escape_string($code));
if (mysql_query($sql))
{
$id = mysql_insert_id();
return true;
}
else
{
return false;
}
return false;
}
could you help me out in where i am going wrong since im unable to understand.
i'm still an amature in php so please help me out.
I am putting my ideas together and at the moment i have the following:
-a mysql database with 2 tables.
-the first table "downloads" contains 3 rows, each with a unique id that and each represent the type of file being downloaded. e.g. application or theme.
-the second table contains the information about each download, these fields are the models supported by the download, the title, a picture, a brief description and a download link. each download has a unique id.
Now what i am trying to do is insert the data into a set of divs, these divs are as follows:
<div class="dlcontainer">
<div class="dlitem">
<div class="dltitle"><php $row['title'] ?></div>";
<div class="dlimage"><php $row['image'] ?></div>";
<div class="dldescription"><php $row['description'] ?></div>";
<div class="dllink"><php $row['downlink'] ?></div>";
</div>
</div>
As you can see i have been trying to populate the divs with information called from the database and that was my first attempt.
I didn't feel like i was going about this with the right approach, so i ended up with this, which also does not seem to work:
<?php
$con = mysql_connect("test","test","test");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM `content` WHERE pid = '2' AND models = 'all' OR models = $chosen");
while($row = mysql_fetch_array($result))
{
echo "<div class=\\"dlcontainer\\">";
echo "<div class=\\"dlitem\\">";
echo "<div class=\\"dltitle\\">$row['title']</div>";
echo "<div class=\\"dlimage\\">$row['image']</div>";
echo "<div class=\\"dldescription\\">$row['description']</div>";
echo "<div class=\\"dllink\\">$row['downlink']</div>";
echo "</div>";
echo "</div>";
}
mysql_close($con);
?>
Once i get this initial download working, i can then set up a loop that will display all the contents that match belong to a specific "pid" and either match a "models" value of "all" or one that has been selected by the user.

Have I written the INSERT query correctly? [duplicate]

This question already has an answer here:
entered form data is not saving in mysql db?
(1 answer)
Closed 9 years ago.
Here is my code:
<?php
include('admin/class.php');
Here is my db connection:
$link = mysqli_connect("localhost", "root", "", "timesheet1234");
Here is the action for save button:
if(isset($_POST['save']))
{
$user=$_SESSION['user'];
$sel =$_POST["selpro"];
$mon =$_POST["mon"];
$tue =$_POST["tue"];
$wed =$_POST["wed"];
$thu =$_POST["thu"];
$fri =$_POST["fri"];
$sat =$_POST["sat"];
$sun =$_POST["sun"];
Checking whether the $user is in db are not:
$sql=mysqli_query($link,"select * from emp
where username='".$_SESSION['user']."'");
$res=mysqli_num_rows($sql);
Here it checks whether to insert are not:
if($res==0)
{
$sql1 = mysqli_query($link,"INSERT INTO emp SET username='$user',
project code='$sel',mon=$mon,tue=$tue,wed=$wed,
thu=$thu,fri=$fri,sat=$sat,sun=$sun");
Here the problem comes:
if($sql1){
echo "<script type='text/javascript'>";
echo "alert('TimeSheet Saved..!')";
echo "</script>";
echo "<script type='text/javascript'>";
echo "window.location='my_tm.php'";
echo "</script>";
}
else
{
echo "<script type='text/javascript'>";
echo "alert('Some Error Occured ! Retry..!')";
echo "</script>";
echo "<script type='text/javascript'>";
echo "window.location='my_tm.php'";
echo "</script>";
}
}
}
?>
Isnt the problem with if($res==0)? Because you check if the session user is in the database and store the number of rows in $res. So I would guess that you want to insert the query when $res==1 (one user) and not when there is no user.
Also normally you have complete control over your session variables. So once you store a user in there, you can trust that its there. Unless ofcourse you made mistakes on the authentication process.
Also in your javascript code you want to use window.location.href='my_tm.php to change the url and not just window.location
Try this as your insert query
$sql1 = mysqli_query("INSERT INTO emp SET username='$user',
`project code`='$sel',mon='$mon',tue='$tue',wed='$wed',
thu='$thu',fri='$fri',sat='$sat',sun='$sun'",$link);
The project code column has space

PHP won't grab email addresses from MySQL database to display in html doc

I have a weird question. Is there any reason I can't display an email address I polled from a MySQL database?
Polling the database:
$AE_item = mysqli_query($dblink, 'SELECT AEmail FROM heac2013 WHERE cid =' . $heac);
if (!$AE_item) {
$AEmail = 'Error reading data: ' . mysqli_error($dblink);
}
while ($AErow = mysqli_fetch_array($AP_item)) {
$AEmail = $AErow['AEmail'];
}
same code works when polling a field that does not contain an email address, such as querying for a phone number
$AP_item = mysqli_query($dblink, 'SELECT APhone FROM heac2013 WHERE cid =' . $heac);
if (!$AP_item) {
$APhone = 'Error reading data: ' . mysqli_error($dblink);
}
while ($AProw = mysqli_fetch_array($AP_item)) {
$APhone = $AProw['APhone'];
}
at the end of all these polls there is an html document containing echo tags encased in
<?php echo $AEmail; ?> and <?php echo $APhone; ?>
I'm confused. Ever other bit of data shows in the html page (name, address, phone, image of person), but the email addresses are being returned as blank.
When checking the database with PHPmyadmin the fields are there, and the sql query works, but within my php/html documents the emails don't display.
Is there something this n00b is missing?
<?php echo AEmail; ?> and <?php echo APhone; ?>
Should be:
<?php echo $AEmail; ?> and <?php echo $APhone; ?>
Try this :
$AE_item = mysqli_query($dblink, 'SELECT AEmail FROM heac2013 WHERE cid =' . $heac);
to
$AE_item = mysqli_query($dblink, 'SELECT AEmail FROM heac2013 WHERE cid ="' . $heac . '"');
for both queries.
Reason :
your variable $heac may or may not be a number. (depending on your SQL design)
(I assumed that the error pointed by j08691 and nickhar weren't the solution)

Webpage Title user name

I want to put the name of the loggedin user as the title of the page. But I've no idea how to do it. I've tried several ways but each one show a parse error. here is my php code for logging in the user and then showing his details.
<?php
//if the login session does not exist therefore meaning the user is not logged in
if(strcmp($_SESSION['uid'],"") == 0){
//display and error message
echo "<center>You need to be logged in to user this feature!</center>";
}else{
//otherwise continue the page
//this is out update script which should be used in each page to update the users online time
$time = date('U')+50;
$update = mysql_query("UPDATE `employer` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
$display_query = mysql_query("SELECT * FROM employer WHERE `id` = '".$_SESSION['uid']."'");
echo "<table id='pageTable'><tbody><th>Your Details</th>";
echo "<tbody>";
while($row = mysql_fetch_array($display_query)){
echo "<tr><td>Name: </td><td>".$row['name']."</td><tr>";
$titlename = $row['name'];
echo "<tr><td>E-Mail ID: </td><td>".$row['email']."</td><tr>";
echo "<tr><td>Contact No.: </td><td>".$row['contact']."</td><tr>";
echo "<tr><td>Company: </td><td>".$row['company']."</td><tr>";
echo "<tr><td>Designation: </td><td>".$row['designation']."</td><tr>";
}
echo "</tbody>";
echo "</table>";
echo "<table><tr><td>";
echo '<div class="button">Logout</td></tr></table>';
//make sure you close the check if they are online
}
?>
You need to get the data you want before you output the <head> section of the page, then include a <title> element in it.
<title><?php echo htmlspecialchars($myTitle); ?></title>
Make sure you properly markup your page: doctype, html-head-body etc. You can do that and while the body is still 'open', state your php code by simply starting with <?php followed by your script.
Then, the relevant part of your loginname-as-title code:
<head>
<title><?php echo $loginName ?></title><!-- thanks to Berry Langerak for noting 'echo' was missing -->
</head>
<body>
<?php
where $loginName is of course your var for the login ID you want to show.
use below code to display loginname as title
echo '<script language="javascript">';
echo 'document.title = \''.$row['name'].'\'';
echo '</script>'

Is it possible to Query a Mysql database from a field selected from dropdown menu populated from a Query in php

Hello i am new to php and i have tried to find a piece of code that i can use to complete the task i need, i currently have a page with a form set out to view the criteria of a course. also i have a dropdown menu which currently holds all the course codes for the modules i have stored in a database. my problem is when i select a course code i wish to populate the fields in my form to show all the information about the course selected. The code i am trying to get to work is as follows:
<?php
session_start();
?>
<? include ("dbcon.php") ?>
<?php
if(!isset($_GET['coursecode'])){
$Var ='%';
}
else
{
if($_GET['coursecode'] == "ALL"){
$Var = '%';
} else {
$Var = $_GET['coursecode'];
}
}
echo "<form action=\"newq4.php\" method=\"GET\">
<table border=0 cellpadding=5 align=left><tr><td><b>Coursecode</b><br>";
$res=mysql_query("SELECT * FROM module GROUP BY mId");
if(mysql_num_rows($res)==0){
echo "there is no data in table..";
} else
{
echo "<select name=\"coursecode\" id=\"coursecode\"><option value=\"ALL\"> ALL </option>";
for($i=0;$i<mysql_num_rows($res);$i++)
{
$row=mysql_fetch_assoc($res);
echo"<option value=$row[coursecode]";
if($Var==$row[coursecode])
echo " selected";
echo ">$row[coursecode]</option>";
}
echo "</select>";
}
echo "</td><td align=\"left\"><input type=\"submit\" value=\"SELECT\" />
</td></tr></table></form><br>";
$query = "SELECT * FROM module WHERE coursecode LIKE '$Var' ";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("No modules match your currently selected coursecode. Please try another coursecode!");
} ELSE {
Coursecode: echo $row['coursecode'];
Module: echo $row['mName'];
echo $row['mCredits'];
echo $row['TotalContactHours'];
echo $row['mdescription'];
echo $row['Syllabus'];
}
?>
however i can only seem to get the last entry from my database any help to fix this problem or a better way of coding this so it works would be grateful
Thanks
The main error is in your final query, you're not actually fetching anything from the query, so you're just displaying the LAST row you fetched in the first query.
Some tips:
1) Don't use a for() loop to fetch results from a query result. While loops are far more concise:
$result = mysql_query(...) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
...
}
2) Add another one of these while loops to your final query, since it's just being executed, but not fetched.
For me i would use some javascript(NOTE: i prefer jQuery)
An easy technique would be to do this(going on the assumption that when creating the drop downs, your record also contains the description):
Apart from creating your dropdown options like this <option value="...">data</option>, you could add some additional attributes like so:
echo '<option value="'.$row['coursecode'].'" data-desc="'.$row['description'].'">.....</option>
Now you have all your drop down options, next is the javascript part
Let's assume you have included jQuery onto your page; and let's also assume that the description of any selected course is to be displayed in a <div> called description like so:
<div id="course-description"> </div>
<!--style it how you wish -->
With your javascript you could then do this:
$(function(){
$("#id-of-course-drop-down").change(function(){
var desc = $(this).children("option").filter("selected").attr("data-des");
//now you have your description text
$("#course-description").html(desc);
//display the description of the course
}
});
Hope this helps you, even a little
Have fun!
NOTE: At least this is more optimal than having to use AJAX to fecch the description on selection of the option :)

Categories