اختيار من قائمة مرتبطة بقائمة اخرى

afnan • منذ 7 سنوات

السلام عليكم ،

انا عندي قائمتين مرتبطات ببعض، وابي انه المستخدم اذا اختار من القائمة الاولى راح يطلع له خيارات في القائمة الثانية بناء على ما اختاره، 

واذا كان الخيار مو موجود بالقائمة يستطيع اضافة الخيار اللي يبي، 

مثلا انا عندي قائمة باسماء الدول وعندي قائمة باسماء المدن ،

فانا هنا اريد امكانية اضافة دولة جديدة وايضا تحديد المدينه التي داخله فيها،

ايضا اريد امكانية اضافة مدينة جديدة  فمثلا لو اخترت السعودية وكانت تحتوي على الدمام الرياض بريدة، استطيع اضافة جدة مثلا ،

وشكرا .

وهذا الكود المستخدم 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  <head> 
    <meta http-equiv="content-type" content="text/xhtml; charset=utf-8" /> 
    <title>Dynamic Select Statements</title> 
<script type="text/javascript">
 //<![CDATA[ 
 // array of possible countries in the same order as they appear in the country selection list 
 var countryLists = new Array(4) 
 countryLists["empty"] = ["Select a Country"]; 
 countryLists["North America"] = ["Canada", "United States", "Mexico"]; 
 countryLists["South America"] = ["Brazil", "Argentina", "Chile", "Ecuador"]; 
 countryLists["Asia"] = ["Russia", "China", "Japan"]; 
 countryLists["Europe"]= ["Britain", "France", "Spain", "Germany"]; 
 /* CountryChange() is called from the onchange event of a select element. 
 * param selectObj - the select object which fired the on change event. 
 */ 
 function countryChange(selectObj) { 
 // get the index of the selected option 
 var idx = selectObj.selectedIndex; 
 // get the value of the selected option 
 var which = selectObj.options[idx].value; 
 // use the selected option value to retrieve the list of items from the countryLists array 
 cList = countryLists[which]; 
 // get the country select element via its known id 
 var cSelect = document.getElementById("country"); 
 // remove the current options from the country select 
 var len=cSelect.options.length; 
 while (cSelect.options.length > 0) { 
 cSelect.remove(0); 
 } 
 var newOption; 
 // create new options 
 for (var i=0; i<cList.length; i++) { 
 newOption = document.createElement("option"); 
 newOption.value = cList[i];  // assumes option string and value are the same 
 newOption.text=cList[i]; 
 // add the new option 
 try { 
 cSelect.add(newOption);  // this will fail in DOM browsers but is needed for IE 
 } 
 catch (e) { 
 cSelect.appendChild(newOption); 
 } 
 } 
 } 
//]]>
</script>
</head>
<body>
  <noscript>This page requires JavaScript be available and enabled to function properly</noscript>
  <h1>Dynamic Select Statements</h1>
  <label for="continent">Select Continent</label>
  <select id="continent" onchange="countryChange(this);">
    <option value="empty">Select a Continent</option>
    <option value="North America">North America</option>
    <option value="South America">South America</option>
    <option value="Asia">Asia</option>
    <option value="Europe">Europe</option>
  </select>
  <br/>
  <label for="country">Select a country</label>
  <select id="country">
    <option value="0">Select a country</option>
  </select>
</body>
 </html>
كلمات دليلية:

ساعد بالإجابة

"إن في قضاء حوائج الناس لذة لا يَعرفها إلا من جربها، فافعل الخير مهما استصغرته فإنك لا تدري أي حسنة تدخلك الجنة."

الإجابات (6)

afnan • منذ 7 سنوات

عندي استفسار جدا مهم هنا هل أستطيع مقارنه القيمة المختارة من القائمة مع قاعدة البيانات؟

عن طريق اتصل mysql_query ؟؟ أم لابد من PDO؟؟ لانه لايوجد value للاختيارات في القائمة ! صحيح ام لا 

 

afnan • منذ 7 سنوات

جربت PDO ايضا لا يعمل اتوقع السبب لانه القيمة هنا لكل المخرجات من قاعدة البيانات هو id حاولت أغير لتكون نفس الاسم وابحث عن طريقة

للربط بid ما بين الدولة والمدينه ولكن لم اجد.

أرجوا الرد بأقرب فرصة وشكرا جزيلا

Ali Majrashi • منذ 7 سنوات

معليش بس مافهمت عليك اذا ممكن توضيح اكثر ايش المطلوب لان PDO تشتغل وتمام وبدون مشاكل معي 

afnan • منذ 7 سنوات

جزاك الله خيرا أتمنى تفيديني هذا الكود الذي طلبته منك  هو جزء من الكود الاساسي

الأن بعد الاضافة  للمدن والدول أو الاختيار أريد عمل query 

ولكن لاتعمل وكان القيم لكل خيار بالقائمه يختاره المستخدم اساسا ليس له قيمه تساوي الاسم 

مثال هناadd.php ملف بعد التعديل

 

 // Config for database connection
    $username = 'root';
    $password = 'root';
    $db = '3alampro-help';

    // Check connetion
try {
    $pdo = new PDO ('mysql:host=localhost;dbname=' . $db, $username, $password); 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e->getMessage();
    die();
}

if (isset($_POST['new-country']) and !empty($_POST['new-country'])) {
    // query to add new country to DB
    // prepare sql and bind parameters
    $stmt = $pdo->prepare("INSERT INTO countries (name) VALUES (:name)");
    $stmt->bindParam(':name', $name);

    // insert a row
    $name = $_POST['new-country'];
    $stmt->execute();
}

if (isset($_POST['country']) and !empty($_POST['country']) and !empty($_POST['new-city'])) {
    // query to add new city to DB
    // prepare sql and bind parameters
    $stmt = $pdo->prepare("INSERT INTO cities (name, country_id) VALUES (:name, :country_id)");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':country_id', $country_id);

    // insert a row
    $name = $_POST['new-city'];
    $country_id = $_POST['country'];
    $stmt->execute();
}

pdo=null;

 require 'conDB.php';

if (isset($_POST['country']) and !empty($_POST['country'])and !empty($_POST['city']) and empty($_POST['new-country']) and empty($_POST['new-country'])) {

 $city = $_POST['new-city'];
 $country=$_POST['country'];


echo '<table border="1" >';
echo '<tr id="firstrow" style=" background-color: rgb(200.70,2);"><th> Name</th><th>phone number</th><th>country</th><th>city</th><</tr>';

    $query=  mysql_query("select * from DataTable where City ='".$city."' and Country ='".$country."'"); 

  while ($row= mysql_fetch_array($query) ) {  
             echo" <tr><td>".$row['Name']."</td><td>".$row['N.phone']."</td><td>".$row['Country']."</td><td>".$row['City']."</td></tr>" ;  
}

echo '</table>';
}
Ali Majrashi • منذ 7 سنوات

الكود كله اخطاء ياريت استخدام زر اضافة الأكواد بالمحرر لاضافة الاكواد بدل النسخ واللصق من جهازك مباشره add-code.png

بالنسسبة لل query الخاصة بالاستعلام هل هي لجلب معلومات الدول والمدن او في جدول آخر مثل DataTable بناء على اختيار مدينه محددة لعرض البيانات

afnan • منذ 7 سنوات

Config for database connection
    $username = 'root';
    $password = 'root';
    $db = '3alampro-help';

    // Check connetion
try {
    $pdo = new PDO ('mysql:host=localhost;dbname=' . $db, $username, $password); 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e->getMessage();
    die();
}

if (isset($_POST['new-country']) and !empty($_POST['new-country'])) {
    // query to add new country to DB
    // prepare sql and bind parameters
    $stmt = $pdo->prepare("INSERT INTO countries (name) VALUES (:name)");
    $stmt->bindParam(':name', $name);

    // insert a row
    $name = $_POST['new-country'];
    $stmt->execute();
}

if (isset($_POST['country']) and !empty($_POST['country']) and !empty($_POST['new-city'])) {
    // query to add new city to DB
    // prepare sql and bind parameters
    $stmt = $pdo->prepare("INSERT INTO cities (name, country_id) VALUES (:name, :country_id)");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':country_id', $country_id);

    // insert a row
    $name = $_POST['new-city'];
    $country_id = $_POST['country'];
    $stmt->execute();
}

pdo=null;

 require 'conDB.php';

if (isset($_POST['country']) and !empty($_POST['country'])and !empty($_POST['city']) and empty($_POST['new-country']) and empty($_POST['new-country'])) {

 $city = $_POST['new-city'];
 $country=$_POST['country'];


echo '<table border="1" >';
echo '<tr id="firstrow" style=" background-color: rgb(200.70,2);"><th> Name</th><th>phone number</th><th>country</th><th>city</th><</tr>';

    $query=  mysql_query("select * from DataTable where City ='".$city."' and Country ='".$country."'"); 

  while ($row= mysql_fetch_array($query) ) {  
             echo" <tr><td>".$row['Name']."</td><td>".$row['N.phone']."</td><td>".$row['Country']."</td><td>".$row['City']."</td></tr>" ;  
}

echo '</table>';

نعم هذا الاستعلام يعمل ع جدول ثالث غير جدول الدول والمدن 

الهدف عرض اسماء وارقام وما الى ذلك بناءقيمة  post  of country /city  اللي بالقائمه

أول الجديدة  post  of new-country /new- city

  هنا كان السبب القيمة ل post  of country /city فارغة 

لايوجد ما يقارن معه !!

لايوجد لديك حساب في عالم البرمجة؟

تحب تنضم لعالم البرمجة؟ وتنشئ عالمك الخاص، تنشر المقالات، الدورات، تشارك المبرمجين وتساعد الآخرين، اشترك الآن بخطوات يسيرة !