Jump to content

Echo's, Values, Forms, Javascript problems


c-o-d-e

Recommended Posts

I have a form, looks fine, ain't been able to test it.

I use PHP, and Javascript. (Not sure on the section)

I have an echo, which uses html, and javascript inside.

When I click a value in Category, it's suppose to pass that value to a query to get the sub-categories for that chosen category. I'm not sure where it gets caught up, but It doesn't get the sub-categories.

The method would probably be called, Passing a value with an unsubmitted form.

 

I shall note that "onchange="htmlData("subcategories.php", "ch="+this.value)" " Was origionally;

onchange="htmlData('subcategories.php', 'ch='+this.value)"

Though because of the echo using ' ', I could not have them within htmlData()

*That is used in Tutorials.php*

 

Here is my code below.

 

tutorials.php

*The file ajax_req.js is set at the top of the page which isn't shown here*

case 'submit':
include 'config.php';

if(isset($_POST['submit'])){
	$category = mysql_real_escape_string($_POST['category']);
	$subcategory = mysql_real_escape_string($_POST['subcategory']);
	$tutname = mysql_real_escape_string($_POST['tutname']);
	$tutorial = mysql_real_escape_string($_POST['tutorial']);
	$description = mysql_real_escape_string($_POST['description']);

	if(empty($category)){
		$error['category'] = '<strong>This field is required</strong><br />';
	}
	if(empty($subcategory)){
		$error['subcategory'] = '<strong>This field is required</strong><br />';
	}
	if(empty($tutname)){
		$error['tutname'] = '<strong>This field is required</strong><br />';
	}
	if(empty($tutorial)){
		$error['tutorial'] = '<strong>This field is required</strong><br />';
	}
	if(empty($description)){
		$error['description'] = '<strong>This field is required</strong><br />';
	}

	if(!isset($error)){
		$by = $_SESSION['s_username'];
	    $date = date("d/m/Y H:i:s", time());
	    $ip = $_SERVER['REMOTE_ADDR'];
		$query = mysql_query("INSERT INTO Tutorials (By, Tutname, Tutorial, Description, Category, Subcategory, Date, IP) VALUES ('$by','$tutname','$tutorial','$description','$category','$subcategory','$date','$ip')") or trigger_error("Query failed: " . mysql_error());

	if(($query)){
		$success = '<strong>Your Tutorial has been submitted to the team. We will analyze your tutorial and approve it or disapprove if necessary.</strong><br />';
	}
	else
	{
	    $success = '<strong>Something has gone wrong please try again.</strong><br />';	
	}
}
}
echo '<p>Submitting Tutorials</p>';
echo '<p>If your desired category and/or sub category is not here, please <a href="listing.php">Submit</a> them before you continue!</p>';
echo $success;
echo '<form method="post" action="">';
    $q = mysql_query("SELECT Category FROM Categories ORDER BY Category ASC") or trigger_error('Query failed: '. mysql_error());

    echo 'Category<br />'.$error['category'].'<select name="category" onchange="htmlData("subcategories.php", "ch="+this.value)" />  ';
echo '<option></option>';
    while($row = mysql_fetch_array($q))
    {
    echo '<option>'. $row['Category'] .'</option>';
    }
    echo '</select><br />';

echo 'Sub-Category<br />'.$error['subcategory'].''; ?>
<div id="txtResult"> <select name="subcategories"><option></option></select></div><br />
<?php
    echo 'Tutorial Name<br />'.$error['tutname'].'';
    echo '<input name="tutname" type="text" id="tutname" size="50" maxlength="40"><br />';
    echo 'Tutorial<br />'.$error['tutorial'].'';
    echo '<textarea name="tutorial" id="tutorial" cols="65" rows="20" maxlength="50"></textarea><br />';
    echo 'Brief Tutorial Description<br />'.$error['description'].'';
    echo '<textarea name="description" id="description" cols="65" rows="3"></textarea><br />';
    echo '<input type="submit" name="submit" id="submit" value="Submit"></form>';

break;

 

ajax_req.js

function GetXmlHttpObject(handler)
{
   var objXMLHttp=null
   if (window.XMLHttpRequest)
   {
       objXMLHttp=new XMLHttpRequest()
   }
   else if (window.ActiveXObject)
   {
       objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
   }
   return objXMLHttp
}

function stateChanged()
{
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
           document.getElementById("txtResult").innerHTML= xmlHttp.responseText;
   }
   else {
           //alert(xmlHttp.status);
   }
}

// Will populate data based on input
function htmlData(url, qStr)
{
   if (url.length==0)
   {
       document.getElementById("txtResult").innerHTML="";
       return;
   }
   xmlHttp=GetXmlHttpObject()
   if (xmlHttp==null)
   {
       alert ("Browser does not support HTTP Request");
       return;
   }

   url=url+"?"+qStr;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET",url,true) ;
   xmlHttp.send(null);
}

 

subcategories.php

<?php
include("config.php");
$category = $_GET['ch'];
$q = mysql_query("SELECT Subcategory FROM Subcategories WHERE Category = '$category' ORDER BY Subcategory ASC") or trigger_error('Query failed: '. mysql_error());

if ($category) {
   ?>
<select name="subcategories">
<?php while($row = mysql_fetch_array($q))
      { ?>
<option><?php echo $row['Subcategory'] ?></option>
<?php } ?>
</select>
<?php } ?>

 

Not too sure on the problem.

 

I'm sorry if this is in the wrong section, it could be a problem with the echo, or with the js. I have no idea.

 

Thanks if you can help.

Link to comment
https://forums.phpfreaks.com/topic/184827-echos-values-forms-javascript-problems/
Share on other sites

Sorry for the bump, it's an update.

 

If I changed the echo to..

    echo 'Category<br />'.$error['category'].''; ?>

    <select name="category" onchange="htmlData('subcategories.php', 'ch='+this.value)" />

This works, how could I do it so it's all in one? becaues I use ' ' on the htmlData, and this doesn't work with speech marks.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.