Pass data through JavaScript to popup page while looping - javascript

I need to know how to pass the date while loop through JavaScript to a popup page and put it inside a <P> tag on the popup window?
<div class="header-img" align="center">
<img src="header.png" width="300">
</div>
<div class="maincontent" align="right">
<?php
include 'config.php';
$con->set_charset('utf8');
$sql = "SELECT * FROM de40v_subventions order by id desc";
$names = mysqli_query($con,$sql);
while($row = mysqli_fetch_array ($names)){
?>
<table class="maintable" align="right" width="400px" dir="rtl">
<form name="form" method="post" action="delconfirm.php" onSubmit="return checknone();">
<tr>
<td> <label> <b>الرقم: </b> </label> </td>
<td> <input type="text" value="<?php echo $row['id'] ?>" class="inputid" readonly/> </td>
</tr>
<tr>
<td> <label> <b>التاريخ والوقت: </b> </label> </td>
<td> <p> <?php echo date("Y-m-d H:m:s" , $row['datetime']); ?> </p> </td>
</tr>
<tr>
<td> <label> <b> الإسم كاملاً:</b> </label> </td>
<td> <p id="name"> <?php echo $row['firstname'] . ' ' . $row['fathername'] . ' ' . $row['grandfathername'] . ' ' . $row['familyname'] ?> </p> </td>
</tr>
<tr>
<td> <label> <b> رقم الهوية:</b> </label> </td>
<td> <p id="identity"> <?php echo $row['identity'] ?> </p> </td>
.........
<script type="text/JavaScript">
function openprint(){
var popupwin = window.open("","طباعة البيانات","width=700, height=400");
popupwin.document.writeln('<html><head><title>طباعة البيانات</title></head> <body> <center> <p id="printname"></p> </center> </body> </html>');
popupwin.document.close();
popupname = popupwin.document.getElementById('printname').innerHTML;
name = document.getElementById('name').innerHTML;
popupname = name;
}
<?php } ?>

Actually something is showing up on your popup page, you just haven't passed any actual visible content there. Try this:
popupwin.document.getElementById('printname').innerHTML = 'put your date or content here';
And place it just after your writeln() call.

Related

php notice database array error

If I press on my post button the data of my form will be send to the database but I get 2 Array to string error's. I don't know what I have to change to fix these errors.
Here is also a picture of the data in my database where it is showing arrays.
https://gyazo.com/f7463825b6ae2e44812d5149d55e9011
I hope someone can help me! I am learning :P
Here is my code example.
<html>
<title>HOTEL</title>
<center>
<head>
<h2>
<?php echo "Welkom terug " . $_SESSION["username"] . ""?>
</br>
<a href='logout.php'>Log uit</a></br>
</h2>
<hr>
<h1>Film toevoegen</h1>
<hr>
</head>
<body>
<form method="POST">
<table>
<tr>
<td>Film:</td>
<td><input type="text" name="txtFilm"></td>
</tr>
<tr>
<td>Genre:</td>
<td>
<select name="Genre">
<option value="Actie">Actie</option>
<option value="Fantasie">Fantasie</option>
<option value="Horror">Horror</option>
<option value="Avontuur">Avontuur</option>
<option value="Komedie">Komedie</option>
<option value="Romantiek">Romantiek</option>
<option value="Historisch">Historisch</option>
</select>
</td>
</tr>
<tr>
<td>Film omschrijving:</td>
<td><input type="text" name="txtFilmomschrijving"></td>
</tr>
<tr>
<td>Datum van uitkomst: </td>
<td><input type="date" name="uitkomstdate">
</td>
</tr>
<tr>
<td>Datum uit bioscoop: </td>
<td><input type="date" name="BiosDate">
</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td>Film draai dagen: </td>
<td>
<input type="checkbox" value="Maandag" name="draaidag[]">Maandag
<input type="checkbox" value="Dinsdag" name="draaidag[]">Dinsdag
<input type="checkbox" value="Woensdag" name="draaidag[]">Woensdag
<input type="checkbox" value="Donderdag" name="draaidag[]">Donderdag
<input type="checkbox" value="Vrijdag" name="draaidag[]">Vrijdag
<input type="checkbox" value="Zaterdag" name="draaidag[]">Zaterdag
<input type="checkbox" value="Zondag" name="draaidag[]">Zondag
</td>
</tr>
<tr>
<td>Film tijd dagen: </td>
<td>
<input type="checkbox" value="12uur" name="tijddagen[]">12:00
<input type="checkbox" value="14uur" name="tijddagen[]">14:00
<input type="checkbox" value="16uur" name="tijddagen[]">16:00
<input type="checkbox" value="18uur" name="tijddagen[]">18:00
<input type="checkbox" value="20uur" name="tijddagen[]">20:00
<input type="checkbox" value="22uur" name="tijddagen[]">22:00
<input type="checkbox" value="24uur" name="tijddagen[]">24:00
</td>
</tr>
<tr>
<td>Zaal: </td>
<td>
<select name="zaal">
<option value="normaal">Normaal</option>
<option value="groot">Groot</option>
</select>
</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<td></td>
<td>
<input type="submit" value="Film toevoegen" name="btnHuur" />
</td>
</tr>
</table>
</form>
<hr>
<?php
if(isset($_POST['btnHuur']))
{?>
<table>
<th>
<h2>Toegevoegde film:</h2>
</th>
<?php
if(isset($_POST['btnHuur']))
{
?>
<tr>
<td>
<?php echo 'Film: '; ?>
</td>
<td>
<?php echo $_POST['txtFilm']; ?>
</td>
</tr>
<tr>
<td>
<?php echo 'Genre: '; ?>
</td>
<td>
<?php echo $_POST['Genre']; ?>
</td>
</tr>
<tr>
<td>
<?php echo 'Film omschrijving: '; ?>
</td>
<td>
<?php echo $_POST['txtFilmomschrijving'];?>
</td>
</tr>
<tr>
<td>
<?php echo 'Datum van uitkomst: ';?>
</td>
<td>
<?php echo $_POST['uitkomstdate']; ?>
</td>
</tr>
<tr>
<td>
<?php echo 'Datum uit bioscoop: ';?>
</td>
<td>
<?php echo $_POST['BiosDate']; ?>
</td>
</tr>
<tr>
<td>
<?php echo 'Film draai dagen: ';?>
</td>
<td>
<?php
if(!empty($_POST['draaidag']))
{
foreach($_POST['draaidag']as$check)
{
echo " ".$check;
$draaidag = " ".$check;
}
?>
</td>
</tr>
<tr>
<td>
<?php echo 'Film tijd dagen: ';?>
</td>
<td>
<?php
if(!empty($_POST['tijddagen']))
{
foreach($_POST['tijddagen']as$check)
{
echo " ".$check;
$tijddagen = " ".$check;
}
}
?>
</td>
</tr>
<tr>
<td>
<?php echo 'zaal: '; ?>
</td>
<td>
<?php echo $_POST['zaal']; ?>
</td>
</tr>
<?php
}else
{
echo "Geen";
}
include("loginDB.php");
?>
</br>
</br>
</br>
<?php
$film = $_POST['txtFilm'];
$genre = $_POST['Genre'];
$Filmomschrijving = $_POST['txtFilmomschrijving'];
$Datumvanuitkomst = $_POST['uitkomstdate'];//$_POST['selDag'] + '-' + $_POST['selMaand'] + '-' + $_POST['selJaar'];
$Datumuitbioscoop = $_POST['BiosDate'];//$_POST['selDag'] + '-' + $_POST['selMaand'] + '-' + $_POST['selJaar'];
$Filmdraaidagen = $_POST['draaidag'];
$Filmtijddragen = $_POST['tijddagen'];
$Zaal = $_POST['zaal'];
?>
<tr></tr>
<tr>
<td>
<?php
if($film && $genre && $Filmomschrijving && $Datumvanuitkomst && $Filmdraaidagen && $Filmtijddragen && $Zaal)
{
$query = "INSERT INTO films VALUES (0, '$film', '$genre', '$Filmomschrijving', '$Datumvanuitkomst', '$Datumuitbioscoop', '$Filmdraaidagen', '$Filmtijddragen', '$Zaal')";
mysqli_query($db, $query);
echo "Bestelling voltooid!";
}
}
?>
</td>
</tr>
</table>
<hr>
<?php } ?>
</body>
</center>
</html>
There is some mistake in your Php Code,
Html You had define name as Array (draaidag[] && tijddagen[])
<tr>
<td>Film draai dagen: </td>
<td>
<input type="checkbox" value="Maandag" name="draaidag[]">Maandag
<input type="checkbox" value="Dinsdag" name="draaidag[]">Dinsdag
<input type="checkbox" value="Woensdag" name="draaidag[]">Woensdag
<input type="checkbox" value="Donderdag" name="draaidag[]">Donderdag
<input type="checkbox" value="Vrijdag" name="draaidag[]">Vrijdag
<input type="checkbox" value="Zaterdag" name="draaidag[]">Zaterdag
<input type="checkbox" value="Zondag" name="draaidag[]">Zondag
</td>
</tr>
<tr>
<td>Film tijd dagen: </td>
<td>
<input type="checkbox" value="12uur" name="tijddagen[]">12:00
<input type="checkbox" value="14uur" name="tijddagen[]">14:00
<input type="checkbox" value="16uur" name="tijddagen[]">16:00
<input type="checkbox" value="18uur" name="tijddagen[]">18:00
<input type="checkbox" value="20uur" name="tijddagen[]">20:00
<input type="checkbox" value="22uur" name="tijddagen[]">22:00
<input type="checkbox" value="24uur" name="tijddagen[]">24:00
</td>
</tr>
You need to use implode() while accessing from $_POST. to convert it into string.
$Filmdraaidagen = implode("," ,$_POST['draaidag']);
$Filmtijddragen = implode(",",$_POST['tijddagen']);

Dynamic Rows Form Submit

I have a decent sized form on my website which has an insert row button at the bottom, this adds rows to the bottom of the form to which I can also click the corresponding delete button to delete that single row to.
Screenshot of the bottom of the form
Now, the issue arises when I click the "Send" button. If I have added a few rows to the form and then click the "Send" button, the rows disappear from the form and so do their values. I want it so once I've added the rows and click the send button, the form keeps its state properly and keeps the rows that I previously added.
HTML Code can be found here:
<form name="contact" action="request-a-quote.php" method="POST">
<table class="form">
<thead>
<?php if (isset($form_error_message)) { echo $form_error_message; } ?>
<tr>
<th colspan="3">Company Information</th>
</tr>
</thead>
<tbody>
<tr>
<th class="desktop_only">Company Name *</th>
<td colspan="2"><input name="company_name" type="text" <?php if ($error_company_name=='Y' ) { echo 'class="error_on_field"'; } ?>
<?php if ($detect->isMobile()) { echo 'placeholder="Company Name"'; } ?> maxlength="45" value="
<?php if (isset($company_name)) echo $company_name; ?>"></td>
</tr>
</tbody>
<thead>
<tr>
<th colspan="3">Contact Information</th>
</tr>
</thead>
<tbody>
<tr>
<th class="desktop_only">Contact person name *</th>
<td colspan="2"><input name="contact_person_name" type="text" <?php if ($error_contact_person_name=='Y' ) { echo 'class="error_on_field"'; } ?>
<?php if ($detect->isMobile()) { echo 'placeholder="Contact Person Name"'; } ?> maxlength="45" value="
<?php if (isset($contact_person_name)) echo $contact_person_name; ?>"></td>
</tr>
<tr>
<th class="desktop_only">Contact person e-mail *</th>
<td colspan="2"><input name="contact_person_email" type="email" <?php if ($error_contact_person_email=='Y' ) { echo 'class="error_on_field"'; } ?>
<?php if ($detect->isMobile()) { echo 'placeholder="Contact Person Email"'; } ?> size="40" value="
<?php if (isset($contact_person_email)) echo $contact_person_email; ?>" /></td>
</tr>
<tr>
<th class="desktop_only">Contact address *</th>
<td colspan="2"><input name="contact_address" type="text" <?php if ($error_contact_address=='Y' ) { echo 'class="error_on_field"'; } ?>
<?php if ($detect->isMobile()) { echo 'placeholder="Contact Address"'; } ?> maxlength="45" value="
<?php if (isset($contact_address)) echo $contact_address; ?>"></td>
</tr>
<tr>
<th class="desktop_only">Contact person phone *</th>
<td colspan="2"><input name="contact_person_phone" type="text" <?php if ($error_contact_person_phone=='Y' ) { echo 'class="error_on_field"'; } ?>
<?php if ($detect->isMobile()) { echo 'placeholder="Contact Person Phone"'; } ?> maxlength="45" value="
<?php if (isset($contact_person_phone)) echo $contact_person_phone; ?>"></td>
</tr>
</tbody>
<thead>
<tr>
<th colspan="3">Boiler Information</th>
</tr>
</thead>
<tbody>
<tr>
<th class="desktop_only">Boiler brand / manufacturer *</th>
<td colspan="2"><input name="boiler_brand_manufacturer" type="text" <?php if ($error_boiler_brand_manufacturer=='Y' ) { echo 'class="error_on_field"'; } ?>
<?php if ($detect->isMobile()) { echo 'placeholder="Boiler brand / Manufacturer"'; } ?> maxlength="45" value="
<?php if (isset($boiler_brand_manufacturer)) echo $boiler_brand_manufacturer; ?>"></td>
</tr>
<tr>
<th class="desktop_only">Average annual fuel consumption (kW or £)</th>
<td colspan="2"><input name="average_annual_fuel_consumption" type="text" <?php if ($detect->isMobile()) { echo 'placeholder="Average annual fuel consumption (kW or £)"'; } ?> size="60" value="
<?php if (isset($average_annual_fuel_consumption)) echo $average_annual_fuel_consumption; ?>" ></td>
</tr>
<tr>
<th class="desktop_only">Current cost per kW (Pence)</th>
<td colspan="2"><input name="current_cost_per_kw" type="text" <?php if ($detect->isMobile()) { echo 'placeholder="Current cost per kW (Pence)"'; } ?> size="60" value="
<?php if (isset($current_cost_per_kw)) echo $current_cost_per_kw; ?>" ></td>
</tr>
<tr>
<th>Type of fuel</th>
<td colspan="2">
<select name="type_of_fuel">
<option value="0">Please Select...</option>
<option <?php if ($type_of_fuel == "Gas") echo "selected=selected" ?>>Gas</option>
<option <?php if ($type_of_fuel == "Oil") echo "selected=selected" ?>>Oil</option>
<option <?php if ($type_of_fuel == "LPG") echo "selected=selected" ?>>LPG</option>
<option <?php if ($type_of_fuel == "Other") echo "selected=selected" ?>>Other</option>
</select>
</td>
</tr>
</tbody>
<thead>
<tr>
<th colspan="3">Boiler Capacity</th>
</tr>
</thead>
<tbody>
<tr>
<th>Type of boiler</th>
<td colspan="2">
<select name="type_of_boiler">
<option value="0">Please Select...</option>
<option <?php if ($type_of_boiler == "Thermostatically Controlled") echo "selected=selected" ?>>Thermostatically Controlled</option>
<option <?php if ($type_of_boiler == "CHP") echo "selected=selected" ?>>CHP</option>
<option <?php if ($type_of_boiler == "Steam") echo "selected=selected" ?>>Steam</option>
</select>
</td>
</tr>
<tr>
<th class="desktop_only">Total capacity</th>
<td colspan="2"><input name="total_capacity" type="text" <?php if ($detect->isMobile()) { echo 'placeholder="Total Capacity"'; } ?> size="60" value="
<?php if (isset($total_capacity)) echo $total_capacity; ?>" ></td>
</tr>
<tr>
<th class="desktop_only">Boiler capacity</th>
<td colspan="2"><input name="boiler_capacity" type="text" <?php if ($detect->isMobile()) { echo 'placeholder="Boiler Capacity"'; } ?> size="60" value="
<?php if (isset($boiler_capacity)) echo $boiler_capacity; ?>" ></td>
</tr>
</tbody>
<thead>
<tr>
<th colspan="3">Required Measurements (up to 4 meters)</th>
</tr>
</thead>
<tbody class="row">
<tr>
<td colspan="3" class="center">
<img src="images/required-measurements.jpg" alt="Required Measurements" />
</td>
</tr>
</tbody>
</table>
<table id="addrows">
<thead>
<tr>
<th></th>
<th>Circumference</th>
<th>Length</th>
<th></th>
</tr>
</thead>
<tbody class="row">
<tr>
<th>Measurement 1</th>
<td><input name="circumference_1" type="text" <?php if ($detect->isMobile()) { echo 'placeholder="Circumference 1"'; } ?> size="60" value="
<?php if (isset($circumference_1)) echo $circumference_1; ?>" ></td>
<td><input name="length_1" type="text" <?php if ($detect->isMobile()) { echo 'placeholder="Length 1"'; } ?> size="60" value="
<?php if (isset($length_1)) echo $length_1; ?>" ></td>
<td style="width:200px;"></td>
</tr>
</tbody>
</table>
<p><input type="button" id="insert_row" value="Insert row"></p>
<script>
$('#addrows').on('click', 'input[type="button"]', function() {
$(this).closest('tr').remove();
})
$('p input[type="button"]').click(function() {
$('#addrows').append('<tr><th>Measurement 2</th><td><input type="text" class="fname" /></td><td><input type="text" class="fname" /></td><td><input type="button" value="Delete" /></td></tr>')
});
</script>
<table class="form">
<tr id="send_holder">
<td class="calculate" colspan="3">
<input type="submit" id="send" value="Send">
<input type="hidden" name="submitted" value="TRUE">
</td>
</tr>
</table>
</form>
PHP Code can be found here:
<?php
// Taken from http://php.net/manual/en/function.mail.php - Example #4 Sending HTML email
// PHP script triggers if SEND button has been clicked
if (isset($_POST['submitted'])) {
// Set up Fasthosts mandatory settings to enable email sending
$email_from = 'EMAIL'; // Must be an email set-up in Fasthosts, mailbox or forwarder
ini_set('SMTP','smtp.fasthosts.co.uk' );
ini_set('sendmail_from', $email_from);
$website_name = 'NAME'; // Change to the name of the website in which the form is
$form_name = 'Request a Quote'; // Change to the name of the form
// Populate PHP variables from $_POST array
$company_name = htmlentities(trim($_POST['company_name']),ENT_QUOTES,"ISO-8859-15");
$contact_person_name = htmlentities(trim($_POST['contact_person_name']),ENT_QUOTES,"ISO-8859-15");
$contact_person_email = htmlentities(trim($_POST['contact_person_email']),ENT_QUOTES,"ISO-8859-15");
$contact_address = htmlentities(trim($_POST['contact_address']),ENT_QUOTES,"ISO-8859-15");
$contact_person_phone = htmlentities(trim($_POST['contact_person_phone']),ENT_QUOTES,"ISO-8859-15");
$boiler_brand_manufacturer = htmlentities(trim($_POST['boiler_brand_manufacturer']),ENT_QUOTES,"ISO-8859-15");
$average_annual_fuel_consumption = htmlentities(trim($_POST['average_annual_fuel_consumption']),ENT_QUOTES,"ISO-8859-15");
$current_cost_per_kw = htmlentities(trim($_POST['current_cost_per_kw']),ENT_QUOTES,"ISO-8859-15");
$type_of_fuel = htmlentities(trim($_POST['type_of_fuel']),ENT_QUOTES,"ISO-8859-15");
$type_of_boiler = htmlentities(trim($_POST['type_of_boiler']),ENT_QUOTES,"ISO-8859-15");
$total_capacity = htmlentities(trim($_POST['total_capacity']),ENT_QUOTES,"ISO-8859-15");
$boiler_capacity = htmlentities(trim($_POST['boiler_capacity']),ENT_QUOTES,"ISO-8859-15");
$circumference_1 = htmlentities(trim($_POST['circumference_1']),ENT_QUOTES,"ISO-8859-15");
$length_1 = htmlentities(trim($_POST['length_1']),ENT_QUOTES,"ISO-8859-15");
// Check for errors and update error variables, e.g. $error_on_name, etc.
if (empty($company_name)) { $error_company_name = 'Y'; }
if (empty($contact_person_name)) { $error_contact_person_name = 'Y'; }
if (empty($contact_person_email)) { $error_contact_person_email = 'Y'; }
if (empty($contact_address)) { $error_contact_address = 'Y'; }
if (empty($contact_person_phone)) { $error_contact_person_phone = 'Y'; }
if (empty($boiler_brand_manufacturer)) { $error_boiler_brand_manufacturer = 'Y'; }
$errors_exist = $error_company_name.$error_contact_person_name.$error_contact_person_email.$error_contact_address.$error_contact_person_phone.$error_boiler_brand_manufacturer;
// No errors exist - Set up and send emails and redirect
if (!empty($errors_exist)) {
$form_error_message = '<tr><td colspan="2"><p id="form_error_message">There are a few pieces of information that we need from you before this form comes through to us. They are highlighted below in red, so please fill those in again and click the <strong>Submit</strong> button again.</p></td></tr>';
// Else, i.e. if $errors array is empty
} else {
// Set up email recipient(s)
$to1 = $contact_person_email; // First email is sent to the person who submitted the form; $email
$to2 = 'email_address'; // Second email is sent to the website owner; change this to the relevant email address
// Set up email subject(s)
$subject1 = 'Thanks for your submission - '.$website_name.' - '.$form_name.' form'; // Do not change
$subject2 = 'Website submission - '.$website_name.' - '.$form_name.' form'; // Do not change
// Set up email message(s)
$message1 = '
<div style="font-family: arial, tahoma, sans serif; font-size: small; color: #666;">
<p>Hi '.$contact_person_name.',</p>
<p>Thanks for completing our '.$form_name.' form on the '.$website_name.' website. We\'ll be in touch as soon as we can!</p>
<p>With kind regards<br />'.$website_name.'</p>
</div>';
$message2 = '
<div style="font-family: arial, tahoma, sans serif; font-size: small; color: #666;">
<p>The following submission has been made via the '.$form_name.' form:</p>
<h2>Company Information</h2>
<p>Company Name: '.$company_name.'</p>
<h2>Contact Information</h2>
<p>
Contact Person Name: '.$contact_person_name.'
<br />Contact Person Email: '.$contact_person_email.'
<br />Contact Address: '.$contact_address.'
<br />Contact Person Phone: '.$contact_person_phone.'
</p>
<h2>Boiler Information</h2>
<p>
Boiler Brand/Manufacturer: '.$boiler_brand_manufacturer.'
<br />Average Annual Fuel Consumption: '.$average_annual_fuel_consumption.'
<br />Current Cost per kW: '.$current_cost_per_kw.'
<br />Type of Fuel: '.$type_of_fuel.'
</p>
<h2>Boiler Capacity</h2>
<p>
Type of Boiler: '.$type_of_boiler.'
<br />Total Capacity: '.$total_capacity.'
<br />Boiler Capacity: '.$boiler_capacity.'
</p>
</div>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers can be set here; Change From to website owner
$headers .= 'From: NAME <' . $email_from . '>' . "\r\n";
// Send emails
mail($to1, $subject1, $message1, $headers);
mail($to2, $subject2, $message2, $headers);
// Errors exist - populate $form_error_message ready for output to browser
}
} // END-OF-IF (isset($_POST['submitted']))
// Stage M3. Add <p> to end of $form_error_message
$form_error_message = $form_error_message.'</p>';
?>
Once you're doing a form submit (instead of an Ajax call) it's pretty easy.
In your form:
<input type="text" name="measure[]" value="This input is fixed">
Each time you add a new input (from the group "measure") it should has the same name as an array: name="measure[]" like the input above.
Then in PHP you just need to run into this array to get all fields.
<?php
$total_elements = count($_POST['measure']);
for ( $i=0;$i<$total_elements;++$i ) {
echo $_POST['measure'][$i];
}
?>
Hope it helps.

unable to load database value in php in ajax

I am entry level developer in PHP and have no knowledge in PHP, i am trying to call database values on selecting a drop down menu, have done this so far with the help of tutorial,.. the problem is when i select 1st drop down the second drop down is getting values from DB, but when i select option from 2nd drop down menu, i am not getting any data displayed on page. Nothing Happens after selecting an option from 2nd drop down.
<?php
include '../connect.php';
$unm=htmlentities($_SESSION['username']);
?>
<html>
<head>
<title> User Panel</title></head>
<body background="../image/pencilback.jpg">
<div id="welcome"><?php
echo '<align="right">'.'<h3>'."Welcome ".$unm.'</h3>';
?></div>
<link rel="stylesheet" type="text/css" href="../css/heading.css">
<table border="0" width="50%" height="150px" align="center" >
<tr width="100%" height="200px" > <td colspan="3"> <img src="../image/banner.jpg"> </td> </tr>
</table>
<table id="tableborder" border="0" width="83%" height="40px" align="center" bgcolor="#BDBDBD" >
<td align="center"><b> Take a Test</b> </td>
<td align="center"><a href="myaccount.php"><b> My Account Details</b> </td>
<td align="center"><a href="testhistory.php"><b> View Test History </b> </td>
<td align="center"><a href="feedback.php"><b> Give Feedback</b> </td>
<td align="center"><a href="../logout.php"><b> Logout</b> </td> </td> </tr>
</table>
<form method="POST" action="">
<div id="viewset"><center>
Select Desired Test </center> <br></div><br><br><br><br><br><br>
<table border="1px" align="center" cellspacing="10px" cellpadding="10px">
<tr> <td> <select name="tstcat" id="countrydd" onChange="change_country()">
<option> Select Test Category</option>
<?php
$res=mysqli_query($connection,"select * from sub");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["subname"]; ?>"> <?php echo $row["subname"]; ?> </option>
<?php
}
?>
</select>
</td>
<td>
<div id="state" align="center">
<select id="details" onChange="display_details()">
<option>Select Test</option>
</select>
</div>
</tr> </td></table>
<br>
<div id="display">
<table border="1px" height="100px" width="30%" align="center">
<tr style=color:maroon> <th> Test Name </th> <th> No. of Questions </th> <th> Duration </th> </tr>
<tr> <td> </td> <td> </td> <td> </td> </tr>
</table>
</div>
<center> <input type="submit" name="submit" value="Go"></center>
<?php
if(isset($_POST['submit']))
{
$tstct=$_POST['tstcat'];
$tnm=$_POST['testid'];
$_SESSION['testname']=$tnm;
$_SESSION['testcatg']=$tsc;
header('location:teststart.php');
}
?>
</div>
</form>
<script type="text/javascript">
//========== FOR DROPDOWN SELECTION
function change_country()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?category="+document.getElementById("countrydd").value,false);
xmlhttp.send(null);
document.getElementById("state").innerHTML=xmlhttp.responseText;
}
//========== TO LOAD TEST DETAILS
function display_details()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax2.php?testname="+document.getElementById("details").value,false);
xmlhttp.send(null);
document.getElementById("display").innerHTML=xmlhttp.responseText;
}
</script>
</body>
</html>
AJAX.PHP
<?php
include '../connect.php';
$category=$_GET["category"];
if($category!="")
{
$res=mysqli_query($connection,"select * from test_detail where test_category='$category'");
echo "<select name='testid' onchange='change_test()'>";
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["test_name"];?>"> <?php echo $row["test_name"];?> </option>
<?php
}
echo "</select>";
}
?>
AJAX 2.PHP
<?php
include '../connect.php';
$tstname=$_GET["testname"];
if($tstname!="")
{
$res1=mysqli_query($connection,"select * from test_detail where test_name='$tstname'");
echo "<table>";
while($row1=mysqli_fetch_array($res1))
{
?>
<tr> <td> <?php echo $row1["test_name"];?> </td>
<td> <?php echo $row1["total_ques"];?> </td>
<td> <?php echo $row1["test_dur"];?> </td> </tr>
<?php
}
echo "</table>";
}
?>
I would recommend you to check three things.
in the select that is inside to the div with id #state, you have an onChange calling the javascript function 'display_details()' which you use to load the table element with detail data. But when you return the select from AJAX.PHP the onchange event has another function 'change_test()', it should be 'display_details()'
When you insert dynamically new html elements in a document, normally you have to use dynamic event binding to attach the event handlers. For this I recommend you to use jquery, this link explain how Event binding on dynamically created elements
to make to ajax request jquery is very good library, but if you want to use the XMLHttpRequest object , It will be a good idea check the status of the response:
if (xmlhttp.status == 200){
document.getElementById("state").innerHTML=xmlhttp.responseText;
}else{
// Handle the error;
}

Elements of the file called from post method not accessible in jquery

Please help me. I have a php file that will be called by post method of jQuery. The html elements from this php file cannot be accessible in jquery.
Here comes my code snippet.
In query:
var getFinishOptions = $.post("ajax_finish_options.php",
{ event_id: event_name},
function(data)
{
$("#TimeToFinish").append(data);
}
);
HTML:
<div id="TimeToFinish"> </div>
PHP:
<tr style="width: 100%">
<th style="width: 100%" colspan="2"><?php echo $FinishCategory[0]; ?> </th>
<th style="width: 100%" colspan="3"><?php echo $FinishCategory[1]; ?> </th>
<th style="width: 100%" colspan="3"><?php echo $FinishCategory[2]; ?> </th>
</tr>
<tr>
<td> Option 1 </td>
<td> Option 2 </td>
<td> Option 1 </td>
<td> Option 2 </td>
<td> Option 3 </td>
<td> Option 1 </td>
<td> Option 2 </td>
<td> Option 3 </td>
</tr>
<tr>
<td> <input type="radio" class="FinishOptions" name="FinishOptions" value="<?php echo $FinishOption[$FinishCategory[0]][$event_id][0]; ?>" /> <?php echo $FinishOption[$FinishCategory[0]][$event_id][0]; ?> </td>
<td> <input type="radio" class="FinishOptions" name="FinishOptions" value="<?php echo $FinishOption[$FinishCategory[0]][$event_id][1]; ?>" /> <?php echo $FinishOption[$FinishCategory[0]][$event_id][1]; ?> </td>
<td> <input type="radio" class="FinishOptions" name="FinishOptions" value="<?php echo $FinishOption[$FinishCategory[1]][$event_id][0]; ?>" /> <?php echo $FinishOption[$FinishCategory[1]][$event_id][0]; ?> </td>
<td> <input type="radio" class="FinishOptions" name="FinishOptions" value="<?php echo $FinishOption[$FinishCategory[1]][$event_id][1]; ?>" /> <?php echo $FinishOption[$FinishCategory[1]][$event_id][1]; ?> </td>
<td> <input type="radio" class="FinishOptions" name="FinishOptions" value="<?php echo $FinishOption[$FinishCategory[1]][$event_id][2]; ?>" /> <?php echo $FinishOption[$FinishCategory[1]][$event_id][2]; ?> </td>
<td> <input type="radio" class="FinishOptions" name="FinishOptions" value="<?php echo $FinishOption[$FinishCategory[2]][$event_id][0]; ?>" /> <?php echo $FinishOption[$FinishCategory[2]][$event_id][0]; ?> </td>
<td> <input type="radio" class="FinishOptions" name="FinishOptions" value="<?php echo $FinishOption[$FinishCategory[2]][$event_id][1]; ?>" /> <?php echo $FinishOption[$FinishCategory[2]][$event_id][1]; ?> </td>
<td> <input type="radio" class="FinishOptions" name="FinishOptions" value="<?php echo $FinishOption[$FinishCategory[2]][$event_id][2]; ?>" /> <?php echo $FinishOption[$FinishCategory[2]][$event_id][2]; ?> </td>
</tr>
Final piece of jquery:
$("input[type=radio][name=FinishOptions]").click(function() {
alert($(this).val());
});
All i want is, to get the value of any of one checked radio button.
Please give your valuable comment.
The reason why you cant get the values is that, upon your final piece of jquery, upon execution, you are binding and element which does not yet exist (remember, the radio buttons inside the table are loaded dynamically, thru your ajax call).
Therefore, it will not work. You must use .on() in order to catch that after it has been loaded. (its like .live())
Consider this example:
// change the final piece of jquery
$('#TimeToFinish').on('click', 'input[type="radio"][name="FinishOptions"]', function() {
var value = $(this).val();
alert(value);
});

how to insert multiple rows from array values with input

I have my table named i_table with columns:
id name req_qty req_date rcv_qty rec_date
1 metal 1 2014-03-04
2 spring 5 2014-03-04
in my html/php:
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
how can I insert in multiple arrays from an input according to their unique ID from db? pls help me here... huhu
In your form, add the id as the key -
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id[<?php echo $result2['id'];?>]" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name[<?php echo $result2['id'];?>]" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
Then on your insert.php where you post your form, get the id in your foreach -
<?php
if(isset($_POST['id'])){
foreach($_POST['id'] as $id=>$value){
$sql = "UPDATE `i_table` SET `name` = '".$_POST['name'][$id]."', `rcv_qty` = '".$_POST['rcv'][$id]."', `rec_date` = '".$_POST['name'][$id]."' WHERE `id` = ".$id."";
mysql_query($sql,$con);
}
}
?>
note the foreach() is just an example. You will want to sanitize your data before inserting/updating to prevent sql injection. see How can I prevent SQL injection in PHP?

Categories