2012-04-12 11 views
10

उद्देश्य: किसी फ़ाइल से डेटा पढ़ने और उसे फिर से लिखने के लिए एक PHP फ़ंक्शन को कॉल करें। मैंने केवल इस उद्देश्य के लिए PHP का उपयोग किया - फ़ाइलियो - और मैं PHP के लिए नया हूं।एचटीएमएल घटना पर क्लिक करने के बाद एक PHP फ़ंक्शन को कॉल करें

समाधान? मैंने कई मंचों के माध्यम से प्रयास किया और पता था कि हम इसे सामान्य तरीके से प्राप्त नहीं कर सकते हैं: ऑनक्लिक ईवेंट> कॉल फ़ंक्शन। हम यह कैसे कर सकते हैं, क्या अन्य तरीके हैं, खासकर मेरे मामले में? मेरा HTML कोड और PHP कोड एक ही पृष्ठ पर है: Admin.php। यह HTML हिस्सा है:

function saveContact() 
{ 
    $datafile = fopen ("data/data.json", "a+"); 
    if(!$datafile){ 
     echo "<script>alert('Data not existed!')</script>"; 
    } 
    else{ 
     ... 
     $contact_list = $contact_list . addNewContact(); 
     ... 
     file_put_contents("data/data.json", $contact_list); 
    } 

    fclose($datafile); 
} 

function addNewContact() 
{ 
    $new = '{'; 
    $new = $new . '"fullname":"' . $_GET['fullname'] . '",'; 
    $new = $new . '"email":"' . $_GET['email'] . '",'; 
    $new = $new . '"phone":"' . $_GET['phone'] . '",'; 
    $new = $new . '}'; 
    return $new; 
} 

, इन कोड पर एक नज़र डालें मैं saveContact कॉल करने के लिए जब लोगों को संपर्क जोड़ें बटन पर क्लिक करें:

<form> 
    <fieldset> 
     <legend>Add New Contact</legend> 
     <input type="text" name="fullname" placeholder="First name and last name" required /> <br /> 
     <input type="email" name="email" placeholder="[email protected]" required /> <br /> 
     <input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br /> 
     <input type="submit" name="submit" class="button" value="Add Contact" onClick="" /> 
     <input type="button" name="cancel" class="button" value="Reset" /> 
    </fieldset> 
</form> 

यह पीएचपी हिस्सा है। यदि आवश्यक हो तो हम पृष्ठ को फिर से लोड कर सकते हैं। एफवाईआई, मैं पेज में JQuery, HTML5 का भी उपयोग करता हूं। धन्यवाद,

+2

यू ajax का उपयोग करना होगा। आप किसी क्लाइंट साइड इवेंट हैंडलर द्वारा कॉल किए गए सर्वर साइड फ़ंक्शन को निष्पादित नहीं कर सकते –

+2

आपका जावास्क्रिप्ट कहां है ??? आप PHP फॉर्म HTML ईवेंट जैसे ऑनक्लिक पर कॉल नहीं कर सकते ... आपको AJAX को आपके लिए चाल करने की आवश्यकता है – Baba

+0

याहू सहमत हैं, लेकिन मैं AJAX एक के बगल में एक और समाधान ढूंढना चाहता हूं। नीचे देखो और आप कुछ मूल्यवान कोशिश कर सकते हैं :) @RPM – Shinigamae

उत्तर

9

दो तरीके हैं। सबसे पहले सामान्य फॉर्म सबमिशन

//your_page.php 

<?php 

$saveSuccess = null; 
$saveMessage = null; 

if($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // if form has been posted process data 

    // you dont need the addContact function you jsut need to put it in a new array 
    // and it doesnt make sense in this context so jsut do it here 
    // then used json_decode and json_decode to read/save your json in 
    // saveContact() 
    $data = array(
    'fullname' = $_POST['fullname'], 
    'email' => $_POST['email'], 
    'phone' => $_POST['phone'] 
); 

    // always return true if you save the contact data ok or false if it fails 
    if(($saveSuccess = saveContact($data)) { 
    $saveMessage = 'Your submission has been saved!';  
    } else { 
    $saveMessage = 'There was a problem saving your submission.'; 
    } 
} 
?> 

<!-- your other html --> 

<?php if($saveSuccess !== null): ?> 
    <p class="flash_message"><?php echo $saveMessage ?></p> 
<?php endif; ?> 

<form action="your_page.php" method="post"> 
    <fieldset> 
     <legend>Add New Contact</legend> 
     <input type="text" name="fullname" placeholder="First name and last name" required /> <br /> 
     <input type="email" name="email" placeholder="[email protected]" required /> <br /> 
     <input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br /> 
     <input type="submit" name="submit" class="button" value="Add Contact" onClick="" /> 
     <input type="button" name="cancel" class="button" value="Reset" /> 
    </fieldset> 
</form> 

<!-- the rest of your HTML --> 

का उपयोग करके पृष्ठ को पूरी तरह रीफ्रेश करना है दूसरा तरीका AJAX का उपयोग करना होगा।

// process.php

$response = array(); 

if($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // if form has been posted process data 

    // you dont need the addContact function you jsut need to put it in a new array 
    // and it doesnt make sense in this context so jsut do it here 
    // then used json_decode and json_decode to read/save your json in 
    // saveContact() 
    $data = array(
    'fullname' => $_POST['fullname'], 
    'email' => $_POST['email'], 
    'phone' => $_POST['phone'] 
); 

    // always return true if you save the contact data ok or false if it fails 
    $response['status'] = saveContact($data) ? 'success' : 'error'; 
    $response['message'] = $response['status'] 
     ? 'Your submission has been saved!' 
     : 'There was a problem saving your submission.'; 

    header('Content-type: application/json'); 
    echo json_encode($response); 
    exit; 
} 
?> 

और फिर अपने html में/js नहीं

<form id="add_contact" action="process.php" method="post"> 
     <fieldset> 
      <legend>Add New Contact</legend> 
      <input type="text" name="fullname" placeholder="First name and last name" required /> <br /> 
      <input type="email" name="email" placeholder="[email protected]" required /> <br /> 
      <input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br /> 
      <input id="add_contact_submit" type="submit" name="submit" class="button" value="Add Contact" onClick="" /> 
      <input type="button" name="cancel" class="button" value="Reset" /> 
     </fieldset> 
    </form> 
    <script type="text/javascript"> 
    $(function(){ 
     $('#add_contact_submit').click(function(e){ 
      e.preventDefault(); 
      $form = $(this).closest('form'); 

      // if you need to then wrap this ajax call in conditional logic 

      $.ajax({ 
       url: $form.attr('action'), 
       type: $form.attr('method'), 
       dataType: 'json', 
       success: function(responseJson) { 
       $form.before("<p>"+responseJson.message+"</p>"); 
       }, 
       error: function() { 
       $form.before("<p>There was an error processing your request.</p>"); 
       } 
      }); 
     });   
    }); 
    </script> 
+1

... और दूसरा AJAX कॉल का उपयोग करना है। – inhan

+1

आपके पास अपने फॉर्म में एक्सएसएस है 'action = "" –

+0

@ लॉरेंस: ठीक है, यह उत्पादन कोड नहीं होना चाहिए था, लेकिन मैंने इसे आपके लिए बदल दिया :-) – prodigitalson

3

आप कार्य करें: पूरी तरह से एक अलग फ़ाइल में प्रपत्र प्रसंस्करण Seprate को आप चाहते हैं कि ऐसा करने के लिए ऐसा करने के लिए जावास्क्रिप्ट की आवश्यकता है। बस onClick हटा सकते हैं और php Admin.php फ़ाइल को इस प्रकार लिख:

<!-- HTML STARTS--> 
<?php 
//If all the required fields are filled 
if (!empty($GET_['fullname'])&&!empty($GET_['email'])&&!empty($GET_['name'])) 
{ 
function addNewContact() 
    { 
    $new = '{'; 
    $new .= '"fullname":"' . $_GET['fullname'] . '",'; 
    $new .= '"email":"' . $_GET['email'] . '",'; 
    $new .= '"phone":"' . $_GET['phone'] . '",'; 
    $new .= '}'; 
    return $new; 
    } 

function saveContact() 
    { 
    $datafile = fopen ("data/data.json", "a+"); 
    if(!$datafile){ 
     echo "<script>alert('Data not existed!')</script>"; 
     } 
    else{ 
     $contact_list = $contact_list . addNewContact(); 
     file_put_contents("data/data.json", $contact_list); 
     } 
    fclose($datafile); 
    } 

// Call the function saveContact() 
saveContact(); 
echo "Thank you for joining us"; 
} 
else //If the form is not submited or not all the required fields are filled 

{ ?> 

<form> 
    <fieldset> 
     <legend>Add New Contact</legend> 
     <input type="text" name="fullname" placeholder="First name and last name" required /> <br /> 
     <input type="email" name="email" placeholder="[email protected]" required /> <br /> 
     <input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br /> 
     <input type="submit" name="submit" class="button" value="Add Contact"/> 
     <input type="button" name="cancel" class="button" value="Reset" /> 
    </fieldset> 
</form> 
<?php } 
?> 
<!-- HTML ENDS --> 

सोचा मैं PHP सा पसंद नहीं है। क्या आप वास्तव में संपर्कों के लिए एक फाइल बनाना चाहते हैं? यह बहुत एक mysql डेटाबेस का उपयोग करने के लिए बेहतर होगा। साथ ही, उस फ़ाइल में कुछ ब्रेक जोड़ना भी अच्छा होगा ...

अन्य विचार, आईई प्लेसहोल्डर का समर्थन नहीं करता है।

+0

मुझे या तो। अगर मुझे अपने वेबिस्ट में सर्वर-साइड भाषा का उपयोग करने के लिए मजबूर होना पड़ा, तो मैं एएसपी.नेट की कोशिश करूंगा। लेकिन यह एक छोटा सा प्रदर्शन है, इसलिए मैं अपने सरल वातावरण (एक्सएएमपीपी, ड्रीमवेवायर, किए गए) के लिए PHP चुनता हूं। मुझे यद्यपि MySQL के बारे में कोई जानकारी नहीं है। आपके विचार के लिए धन्यवाद, @ फ्रैंक प्रेसेनिया फैंडोस, मैं अक्सर कोड में ब्रेक जोड़ना भूल जाता हूं। वर्तमान में बेहतर HTML5 समर्थन के लिए क्रोम/फ़ायरफ़ॉक्स पर परीक्षण/डेमो :) – Shinigamae

+0

मैं वास्तव में आपको mysql के बारे में मूल बातें सीखने की सलाह देता हूं। यह सबसे ज्यादा उपयोग किया जाता है और मूल बातें सीखना वास्तव में आसान है। जब आप इसे कार्यान्वित करते हैं, तो आपकी साइट के लिए mysql [this] (http://www.w3schools.com/php/php_mysql_insert.asp) जितना आसान हो सकता है, दूसरा उदाहरण। –

+0

फिर से धन्यवाद, @ फ्रैंक प्रेसेनिया फैंडोस, मैं इसे सीखने जा रहा हूं। – Shinigamae

6
<div id="sample"></div> 
<form> 
     <fieldset> 
      <legend>Add New Contact</legend> 
      <input type="text" name="fullname" placeholder="First name and last name" required /> <br /> 
      <input type="email" name="email" placeholder="[email protected]" required /> <br /> 
      <input type="text" name="phone" placeholder="Personal phone number: mobile, home phone etc." required /> <br /> 
      <input type="submit" name="submit" id= "submitButton" class="button" value="Add Contact" onClick="" /> 
      <input type="button" name="cancel" class="button" value="Reset" /> 
     </fieldset> 
    </form> 

<script> 

    $(document).ready(function(){ 
     $("#submitButton").click(function(){ 
      $("#sample").load(filenameofyourfunction?the the variable you need); 
     }); 
    }); 

</script> 
+0

सरल और साफ, धन्यवाद @ केथेरिन, मैं इसे पहले कोशिश करूंगा :) – Shinigamae

1
cell1.innerHTML="<?php echo $customerDESC; ?>"; 
cell2.innerHTML="<?php echo $comm; ?>"; 
cell3.innerHTML="<?php echo $expressFEE; ?>"; 
cell4.innerHTML="<?php echo $totao_unit_price; ?>"; 

यह एक आकर्षण की तरह काम कर रहा है, जावास्क्रिप्ट एक php के अंदर है, जबकि पाश

+0

$ ग्राहक डीईएससी, @ कॉम आदि फ़ंक्शन हैं जिन्हें हम कॉल करना चाहते हैं, और कक्ष बटन जैसे प्रेषक तत्व हैं? – Shinigamae