Page 1 of 1

how to run php file inside a javascript code/json/jquery

Posted: Tue Oct 07, 2014 5:57 pm
by adsegzy
Hello Guys,

I have a registration form I want to submit to my database. I am not good in javascript for very good in php. I have a form like this;

[text]
<form action="" class="idealforms" method="post">

<div class="idealsteps-wrap">

<!-- Institution's Detail -->

<section class="idealsteps-step">
<div class="field">
<label class="main">Organization:</label>
<input name="organization" id="organization" placeholder="Eg. ABC Int'l Ltd, Institute of Information" type="text" maxlength="70"><br/>
<span class="error"></span>
</div>

<div class="field">
<label class="main">Username:</label>
<textarea name="ursername" cols="20" rows="5"></textarea>
<span class="error"></span>
</div>

<div class="field buttons">
<label class="main">&nbsp;</label>
<button type="button" class="prev">&laquo; Prev</button>
<button type="submit" class="submit" name="submit_agm" id="submit_agm">Submit</button>
</div>
</form>[/text]

I have the php file which will insert the form into mysql

Code: Select all

$organization = $_POST['organization'];
$org_address = $_POST[org_address];
$submit = mysqli_query($mysqli, "INSERT INTO meeting (page_name, organization, submitted_on) VALUES('$page', '$organization', CURRENT_TIMESTAMP)");
I have s javasctipt like this

[text]
<script>

$('form.idealforms').idealforms({

silentLoad: false,

rules: {

},

errors: {
'username': {
ajaxError: 'Username not available'
}
},

onSubmit: function(invalid, e) {
e.preventDefault();
$('#invalid')
.show()
.toggleClass('valid', ! invalid)
.text(invalid ? (invalid +' invalid fields') : 'Submitted Successfully!');

<?php includes('path_to_the_file.php'); ?>
}
});

$('form.idealforms').find('input, select, textarea').on('change keyup', function() {
$('#invalid').hide();
});

$('form.idealforms').idealforms('addRules', {
'comments': 'required minmax:50:200'
});
</script>
[/text]

with the above php inside d javascript, if the content of path_to_the_file.php
$submit = mysql_query("INSERT INTO agm (page_name, organization, submitted_on) VALUES('Page', 'Organization', CURRENT_TIMESTAMP)");
it will be submitted to my database. but if it is
$submit = mysqli_query($syntaxit, "INSERT INTO agm (page_name, organization, submitted_on) VALUES('$page', '$organization', CURRENT_TIMESTAMP)");
it will not.

I also tried this method but didn't submitted anything at all to the database.

[text]
<script>

$('form.idealforms').idealforms({

silentLoad: false,

rules: {

},

errors: {
'username': {
ajaxError: 'Username not available'
}
},

onSubmit: function(invalid, e) {
e.preventDefault();
$('#invalid')
.show()
.toggleClass('valid', ! invalid)
.text(invalid ? (invalid +' invalid fields') : 'Submitted Successfully!');
}
});
</script>
<script>
$(function () {
$('form').bind('submit', function () {
$.ajax({
type: 'post',
url: 'sub.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
return false;
});
});
</script>
[/text]

Please which other way can i use to process my string php file in javascrpit/json/jquery and will work fine?

Thanks20

Re: how to run php file inside a javascript code/json/jquery

Posted: Tue Oct 07, 2014 7:13 pm
by Celauran